Ejemplo n.º 1
0
        private async Task runTest(int count)
        {
            var nodes = new WriteLock[count];
            var asyncManualResetEvent = new AsyncManualResetEvent();

            for (int i = 0; i < count; i++)
            {
                ZooKeeper keeper = await createClient();

                WriteLock leader = new WriteLock(keeper, "/test", null);
                leader.setLockListener(new LockCallback(asyncManualResetEvent));
                nodes[i] = leader;

                await leader.Lock();
            }

            // lets wait for any previous leaders to die and one of our new
            // nodes to become the new leader
            await asyncManualResetEvent.WaitAsync().WithTimeout(30 * 1000);

            WriteLock first = nodes[0];

            dumpNodes(nodes, count);

            // lets assert that the first election is the leader
            Assert.assertTrue("The first znode should be the leader " + first.Id, first.Owner);

            for (int i = 1; i < count; i++)
            {
                WriteLock node = nodes[i];
                Assert.assertFalse("Node should not be the leader " + node.Id, node.Owner);
            }

            if (count > 1)
            {
                LOG.debug("Now killing the leader");
                // now lets kill the leader
                asyncManualResetEvent.Reset();
                await first.unlock();

                await asyncManualResetEvent.WaitAsync().WithTimeout(30 * 1000);

                WriteLock second = nodes[1];
                dumpNodes(nodes, count);
                // lets assert that the first election is the leader
                Assert.assertTrue("The second znode should be the leader " + second.Id, second.Owner);

                for (int i = 2; i < count; i++)
                {
                    WriteLock node = nodes[i];
                    Assert.assertFalse("Node should not be the leader " + node.Id, node.Owner);
                }
            }
        }
Ejemplo n.º 2
0
        private void runTest(int count)
        {
            nodes = new WriteLock[count];
            for (int i = 0; i < count; i++)
            {
                ZooKeeper keeper = createClient();
                WriteLock leader = new WriteLock(keeper, "/test", null);
                leader.setLockListener(new LockCallback(this)).GetAwaiter().GetResult();
                nodes[i] = leader;

                leader.Lock().GetAwaiter().GetResult();
            }

            // lets wait for any previous leaders to die and one of our new
            // nodes to become the new leader
            latch.Wait(30 * 1000);

            WriteLock first = nodes[0];

            dumpNodes(count);

            // lets assert that the first election is the leader
            Assert.assertTrue("The first znode should be the leader " + first.Id, first.Owner);

            for (int i = 1; i < count; i++)
            {
                WriteLock node = nodes[i];
                Assert.assertFalse("Node should not be the leader " + node.Id, node.Owner);
            }

            if (count > 1)
            {
                Console.WriteLine("Now killing the leader");
                // now lets kill the leader
                latch = new ManualResetEventSlim(false);
                first.unlock().GetAwaiter().GetResult();
                latch.Wait(30 * 1000);
                WriteLock second = nodes[1];
                dumpNodes(count);
                // lets assert that the first election is the leader
                Assert.assertTrue("The second znode should be the leader " + second.Id, second.Owner);

                for (int i = 2; i < count; i++)
                {
                    WriteLock node = nodes[i];
                    Assert.assertFalse("Node should not be the leader " + node.Id, node.Owner);
                }
            }
        }