Esempio n. 1
0
        public void testBackgroundConnect()
        {
            int CONNECTION_TIMEOUT_MS = 4000;

            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                        10000,
                                                                        CONNECTION_TIMEOUT_MS,
                                                                        null,
                                                                        new RetryOneTime(1));
            try
            {
                Assert.False(client.isConnected());
                client.start();
                bool outerMustContinue = false;
                do
                {
                    for ( int i = 0; i < (CONNECTION_TIMEOUT_MS / 1000); ++i )
                    {
                        if ( client.isConnected() )
                        {
                            outerMustContinue = true;
                            break;
                        }
                        Thread.Sleep(CONNECTION_TIMEOUT_MS);
                    }
                    if (outerMustContinue)
                    {
                        continue;
                    }
                    Assert.Fail();
                } while ( false );
            }
            finally
            {
                client.Dispose();
            }
        }
Esempio n. 2
0
        public void testExpiredSession()
        {
            // WARN: test requires that this must be address of one ZK host,
            // not a connection string to many nodes
            Barrier expiresBarrier = new Barrier(2);
            Watcher watcher = new ExpiredWatcher(expiresBarrier);
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                        DefaultSessionTimeout,
                                                                        DefaultConnectionTimeout,
                                                                        watcher,
                                                                        new RetryOneTime(2));
            client.start();
            try
            {
                AtomicBoolean firstTime = new AtomicBoolean(true);
                RetryLoop.callWithRetry(
                    client,
                    CallableUtils.FromFunc<object>(() =>
                    {
                        if (firstTime.compareAndSet(true, false))
                        {
                            try
                            {
                                Task<string> createTask = client.getZooKeeper()
                                                                .createAsync("/foo",
                                                                                new byte[0],
                                                                                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                                                CreateMode.PERSISTENT);
                                createTask.Wait();
                            }
                            catch (AggregateException e)
                            {
                                if (e.InnerException is KeeperException.NodeExistsException)
                                {
                                    // ignore
                                }
                                else
                                {
                                    throw e;
                                }
                            }

                            KillSession.kill(client.getZooKeeper(), ZkDefaultHosts, DefaultSessionTimeout * 3);
                            Assert.True(expiresBarrier.SignalAndWait(DefaultSessionTimeout));
                        }
                        ZooKeeper zooKeeper = client.getZooKeeper();
                        client.blockUntilConnectedOrTimedOut();
                        Task<Stat> task = zooKeeper.existsAsync("/foo", false);
                        task.Wait();
                        Stat stat = task.Result;
                        Assert.NotNull(stat);
                        Assert.Greater(stat.getCzxid(), 0);
                        return null;
                    })
                );
            }
            finally
            {
                client.Dispose();
            }
        }
Esempio n. 3
0
        public void testRetryLoopWithFailure()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                        DefaultSessionTimeout,
                                                                        DefaultConnectionTimeout,
                                                                        null,
                                                                        new RetryOneTime(1));
            client.start();
            try
            {
                int loopCount = 0;
                RetryLoop retryLoop = client.newRetryLoop();
                while ( retryLoop.shouldContinue()  )
                {
                    ++loopCount;
                    switch ( loopCount )
                    {
                        case 1:
                        {
            //                            retryLoop.takeException();
                            break;
                        }

                        case 2:
                        {
                            retryLoop.markComplete();
                            break;
                        }

                        case 3:
                        case 4:
                        {
                            // ignore
                            break;
                        }

                        default:
                        {
                            Assert.Fail();
                            break;
                        }
                    }
                }

                Assert.True(loopCount >= 2);
            }
            finally
            {
                client.Dispose();
            }
        }
Esempio n. 4
0
 public void testSimple()
 {
     CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                 10000,
                                                                 10000,
                                                                 null,
                                                                 new RetryOneTime(1));
     client.start();
     try
     {
         client.blockUntilConnectedOrTimedOut();
         Task<string> pathTask = client.getZooKeeper().createAsync("/test",
                                                         new byte[] { 1, 2, 3 },
                                                         ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                         CreateMode.PERSISTENT);
         pathTask.Wait();
         Assert.AreEqual(pathTask.Result, "/test");
     }
     finally
     {
         client.Dispose();
     }
 }
Esempio n. 5
0
        public void testRetryLoop()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                        DefaultSessionTimeout,
                                                                        DefaultConnectionTimeout,
                                                                        null,
                                                                        new RetryOneTime(1));
            client.start();
            try
            {
                int loopCount = 0;
                RetryLoop retryLoop = client.newRetryLoop();
                while ( retryLoop.shouldContinue()  )
                {
                    if ( ++loopCount > 2 )
                    {
                        Assert.Fail();
                        break;
                    }

                    try
                    {
                        client.getZooKeeper().createAsync("/test",
                                                            new byte[]{1,2,3},
                                                            ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                            CreateMode.EPHEMERAL)
                                             .Wait();
                        retryLoop.markComplete();
                    }
                    catch ( Exception e )
                    {
                        retryLoop.takeException(e);
                    }
                }

                Assert.True(loopCount > 0);
            }
            finally
            {
                client.Dispose();
            }
        }