Example #1
0
        public void testElection()
        {
            String dir         = "/test";
            String testString  = "Hello World";
            int    num_clients = 10;

            clients = new ZooKeeper[num_clients];
            LeaderElection[] elections = new LeaderElection[num_clients];
            for (byte i = 0; i < clients.Length; i++)
            {
                clients[i]   = CreateClient();
                elections[i] = new LeaderElection(clients[i], dir, new TestLeaderWatcher(i), new[] { i });
                elections[i].Start();
            }

            for (byte i = 0; i < clients.Length; i++)
            {
                while (!elections[i].IsOwner)
                {
                    Thread.Sleep(1);
                }
                elections[i].Close();
            }
            Assert.Pass();
        }
Example #2
0
        public void testNode4DoesNotBecomeLeaderWhenNonLeader3Closes()
        {
            var dir         = "/test";
            var num_clients = 4;

            clients = new ZooKeeper[num_clients];
            var elections      = new LeaderElection[num_clients];
            var leaderWatchers = new TestLeaderWatcher[num_clients];

            for (byte i = 0; i < clients.Length; i++)
            {
                clients[i]        = CreateClient();
                leaderWatchers[i] = new TestLeaderWatcher((byte)(i + 1));                 // Start at 1 so we can check it is set
                elections[i]      = new LeaderElection(clients[i], dir, leaderWatchers[i], new[] { i });
                elections[i].Start();
            }

            // Kill 2
            elections[2].Close();
            // First one should still be leader
            Thread.Sleep(3000);
            Assert.Equal(1, leaderWatchers[0].Leader);
            Assert.Equal(0, leaderWatchers[1].Leader);
            Assert.Equal(0, leaderWatchers[2].Leader);
            Assert.Equal(0, leaderWatchers[3].Leader);
            elections[0].Close();
            elections[1].Close();
            elections[3].Close();
        }
        public void testNode4DoesNotBecomeLeaderWhenNonLeader3Closes()
        {
            var dir         = "/test";
            var num_clients = 4;

            clients = new ZooKeeper[num_clients];
            var elections      = new LeaderElection[num_clients];
            var leaderWatchers = new TestLeaderWatcher[num_clients];

            for (byte i = 0; i < clients.Length; i++)
            {
                clients[i]        = createClient();
                leaderWatchers[i] = new TestLeaderWatcher((byte)(i + 1));
                elections[i]      = new LeaderElection(clients[i], dir, leaderWatchers[i], new[] { i });
                elections[i].Start();
            }


            elections[2].Close();

            Thread.Sleep(3000);
            Assert.AreEqual(1, leaderWatchers[0].Leader);
            Assert.AreEqual(0, leaderWatchers[1].Leader);
            Assert.AreEqual(0, leaderWatchers[2].Leader);
            Assert.AreEqual(0, leaderWatchers[3].Leader);
            elections[0].Close();
            elections[1].Close();
            elections[3].Close();
        }
Example #4
0
 public LeaderElection JoinElection()
 {
     log.Info(String.Format("join election,data={0}", node.Data));
     election = new LeaderElection(zookeeper, node.Path, new LeaderWatcher(this), Encoding.UTF8.GetBytes(node.Data));
     election.Start();
     return(election);
 }
Example #5
0
        public void Initialize()
        {
            var address = Configurations.ZooKeeperAddress;

            _zk       = ZooKeeperFactory.Instance.Connect(address);
            _election = new LeaderElection(_zk, "/ElectionTest", (_watcher = new LeaderWatcher()), new byte[0]);
        }
        public void TestMethod1()
        {
            var election = new LeaderElection("frogfish");

            election.OnLeaderShip += () =>
            {
                Console.WriteLine("Time to break some promises");
            };

            election.Run();
            Console.ReadLine();
            election.Leave();
        }
Example #7
0
 public HeartBeatService()
 {
     LElection = new LeaderElection();
 }