Search in sources :

Example 16 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFrameworkBackground method testRetries.

@Test
public void testRetries() throws Exception {
    final int SLEEP = 1000;
    final int TIMES = 5;
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(TIMES, SLEEP));
    try {
        client.start();
        client.getZookeeperClient().blockUntilConnectedOrTimedOut();
        final CountDownLatch latch = new CountDownLatch(TIMES);
        final List<Long> times = Lists.newArrayList();
        final AtomicLong start = new AtomicLong(System.currentTimeMillis());
        ((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {

            @Override
            public void listen(OperationAndData<?> data) {
                if (data.getOperation().getClass().getName().contains("CreateBuilderImpl")) {
                    long now = System.currentTimeMillis();
                    times.add(now - start.get());
                    start.set(now);
                    latch.countDown();
                }
            }
        };
        server.stop();
        client.create().inBackground().forPath("/one");
        latch.await();
        for (// first one isn't a retry
        long elapsed : // first one isn't a retry
        times.subList(1, times.size())) {
            Assert.assertTrue(elapsed >= SLEEP, elapsed + ": " + times);
        }
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 17 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFrameworkBackground method testBasic.

@Test
public void testBasic() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch latch = new CountDownLatch(3);
        final List<String> paths = Lists.newArrayList();
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                paths.add(event.getPath());
                latch.countDown();
            }
        };
        client.create().inBackground(callback).forPath("/one");
        client.create().inBackground(callback).forPath("/one/two");
        client.create().inBackground(callback).forPath("/one/two/three");
        latch.await();
        Assert.assertEquals(paths, Arrays.asList("/one", "/one/two", "/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 18 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFrameworkBackground method testErrorListener.

@Test
public void testErrorListener() throws Exception {
    ACLProvider badAclProvider = new ACLProvider() {

        @Override
        public List<ACL> getDefaultAcl() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<ACL> getAclForPath(String path) {
            throw new UnsupportedOperationException();
        }
    };
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).aclProvider(badAclProvider).build();
    try {
        client.start();
        final CountDownLatch errorLatch = new CountDownLatch(1);
        UnhandledErrorListener listener = new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                if (e instanceof UnsupportedOperationException) {
                    errorLatch.countDown();
                }
            }
        };
        client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
        Assert.assertTrue(new Timing().awaitLatch(errorLatch));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ACL(org.apache.zookeeper.data.ACL) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 19 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFrameworkBackground method testCuratorCallbackOnError.

/**
 * Attempt a background operation while Zookeeper server is down.
 * Return code must be {@link Code#CONNECTIONLOSS}
 */
@Test
public void testCuratorCallbackOnError() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1000)).build();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        client.start();
        BackgroundCallback curatorCallback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getResultCode() == Code.CONNECTIONLOSS.intValue()) {
                    latch.countDown();
                }
            }
        };
        // Stop the Zookeeper server
        server.stop();
        // Attempt to retrieve children list
        client.getChildren().inBackground(curatorCallback).forPath("/");
        // Check if the callback has been called with a correct return code
        Assert.assertTrue(timing.awaitLatch(latch), "Callback has not been called by curator !");
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 20 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestWatcherIdentity method testRefExpiration.

@Test
public void testRefExpiration() throws Exception {
    final int MAX_CHECKS = 10;
    final CuratorFrameworkImpl client = (CuratorFrameworkImpl) CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try {
        Assert.assertNull(client.getNamespaceWatcherMap().get(new CountCuratorWatcher()));
        final CountDownLatch latch = new CountDownLatch(1);
        ExecutorService service = Executors.newSingleThreadExecutor();
        service.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                CountZKWatcher watcher = new CountZKWatcher();
                client.getNamespaceWatcherMap().getNamespaceWatcher(watcher);
                Assert.assertNotNull(client.getNamespaceWatcherMap().get(watcher));
                latch.countDown();
                return null;
            }
        });
        latch.await();
        service.shutdownNow();
        Timing timing = new Timing();
        for (int i = 0; i < MAX_CHECKS; ++i) {
            Assert.assertTrue((i + 1) < MAX_CHECKS);
            timing.sleepABit();
            // just to cause drainReferenceQueues() to get called
            client.getNamespaceWatcherMap().drain();
            if (client.getNamespaceWatcherMap().isEmpty()) {
                break;
            }
        }
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

Timing (org.apache.curator.test.Timing)166 CuratorFramework (org.apache.curator.framework.CuratorFramework)147 Test (org.testng.annotations.Test)138 RetryOneTime (org.apache.curator.retry.RetryOneTime)129 CountDownLatch (java.util.concurrent.CountDownLatch)56 ConnectionState (org.apache.curator.framework.state.ConnectionState)39 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)29 KeeperException (org.apache.zookeeper.KeeperException)28 ExecutorService (java.util.concurrent.ExecutorService)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Semaphore (java.util.concurrent.Semaphore)18 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)16 TestingServer (org.apache.curator.test.TestingServer)15 Stat (org.apache.zookeeper.data.Stat)12 TestingCluster (org.apache.curator.test.TestingCluster)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 InstanceSpec (org.apache.curator.test.InstanceSpec)9 Test (org.junit.Test)9