Exemple #1
0
        public async Task MasterElectionIntegrationTest()
        {
            var nodeA = new Master("A", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(10));
            var nodeB = new Master("B", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(10));
            var nodeC = new Master("C", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(10));
            Link(nodeA, nodeB, nodeC);
            var A = nodeA.GetMaster();
            var B = nodeB.GetMaster();
            var C = nodeC.GetMaster();
            await A; await B; await C;
            //sometimes two different masters get elected for the first two rounds
            //when two nodes startup very close together so wait until we've checked
            //the master a second time
            A = nodeA.GetMaster();
            B = nodeB.GetMaster();
            C = nodeC.GetMaster();
            Assert.AreEqual(await A, await B);
            Assert.AreEqual(await B, await C);

            var master = await A;

            DateTime start = DateTime.UtcNow;
            while (DateTime.UtcNow.Subtract(start) < TimeSpan.FromSeconds(30))
            {
                await Task.Delay(200);
                MastersLive(master, nodeA, nodeB, nodeC);
                Console.WriteLine("Correct At: " + DateTime.UtcNow.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK"));
            }
        }
Exemple #2
0
 public async Task TestMasterElection()
 {
     var nodeA = new Master("A", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20));
     var nodeB = new Master("B", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20));
     var nodeC = new Master("C", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20));
     Link(nodeA, nodeB, nodeC);
     var A = nodeA.GetMaster();
     var B = nodeB.GetMaster();
     var C = nodeC.GetMaster();
     Assert.AreEqual(await A, await B);
     Assert.AreEqual(await B, await C);
     MastersMatch(nodeA, nodeB, nodeC);
 }
Exemple #3
0
        public async Task MasterElectionIntegrationTest2()
        {
            DateTime Start = DateTime.UtcNow;
            Console.WriteLine("Start: " + Start.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK") + " (+0ms)");
            var nodeA = new Master("A", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20));
            var nodeB = new Master("B", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20));
            var nodeC = new Master("C", 3, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20));
            Link(nodeA, nodeB, nodeC);

            //the one that goes first is always elected if everyone else waits
            Assert.AreEqual("A", await nodeA.GetMaster());
            Assert.AreEqual("A", await nodeB.GetMaster());
            Assert.AreEqual("A", await nodeC.GetMaster());

            nodeA.Connected = false;
            DateTime KillTime = DateTime.UtcNow;
            Console.WriteLine("Master Killed: " + KillTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK") + " (+" + KillTime.Subtract(Start).TotalMilliseconds + "ms)");
            var leaseExpired = false;
            string master;
            while (!leaseExpired && nodeB.TryGetMaster(out master)) await Task.Yield();
            DateTime LeaseExpireTime = DateTime.UtcNow;
            Console.WriteLine("Lease Expired: " + LeaseExpireTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK") + " (+" + LeaseExpireTime.Subtract(Start).TotalMilliseconds + "ms)");
            master = await nodeB.GetMaster();
            Console.WriteLine("New Master: " + master);
            DateTime End = DateTime.UtcNow;
            Console.WriteLine("New Master Elected: " + End.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK") + " (+" + End.Subtract(Start).TotalMilliseconds + "ms)");

        }