Search in sources :

Example 11 with BackgroundCallback

use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.

the class TestFramework method testExistsCreatingParentsInBackground.

@Test
public void testExistsCreatingParentsInBackground() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try {
        client.start();
        Assert.assertNull(client.checkExists().forPath("/one/two"));
        final CountDownLatch latch = new CountDownLatch(1);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                latch.countDown();
            }
        };
        client.checkExists().creatingParentContainersIfNeeded().inBackground(callback).forPath("/one/two/three");
        Assert.assertTrue(new Timing().awaitLatch(latch));
        Assert.assertNotNull(client.checkExists().forPath("/one/two"));
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
        Assert.assertNull(client.checkExists().creatingParentContainersIfNeeded().forPath("/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 12 with BackgroundCallback

use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.

the class TestFramework method testCustomCallback.

@Test
public void testCustomCallback() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.CREATE) {
                    if (event.getPath().equals("/head")) {
                        latch.countDown();
                    }
                }
            }
        };
        client.create().inBackground(callback).forPath("/head");
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    } 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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 13 with BackgroundCallback

use of org.apache.curator.framework.api.BackgroundCallback 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 14 with BackgroundCallback

use of org.apache.curator.framework.api.BackgroundCallback 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 15 with BackgroundCallback

use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.

the class TestFrameworkEdges method internalTestPathsFromProtectingInBackground.

private void internalTestPathsFromProtectingInBackground(CreateMode mode) throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/a/b/c");
        final BlockingQueue<String> paths = new ArrayBlockingQueue<String>(2);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                paths.put(event.getName());
                paths.put(event.getPath());
            }
        };
        final String TEST_PATH = "/a/b/c/test-";
        client.create().withMode(mode).inBackground(callback).forPath(TEST_PATH);
        String name1 = paths.take();
        String path1 = paths.take();
        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
        client.start();
        CreateBuilderImpl createBuilder = (CreateBuilderImpl) client.create().withProtection();
        client.create().forPath(createBuilder.adjustPath(TEST_PATH));
        createBuilder.debugForceFindProtectedNode = true;
        createBuilder.withMode(mode).inBackground(callback).forPath(TEST_PATH);
        String name2 = paths.take();
        String path2 = paths.take();
        Assert.assertEquals(ZKPaths.getPathAndNode(name1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        Assert.assertEquals(ZKPaths.getPathAndNode(name2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        Assert.assertEquals(ZKPaths.getPathAndNode(path1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        Assert.assertEquals(ZKPaths.getPathAndNode(path2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        client.delete().deletingChildrenIfNeeded().forPath("/a/b/c");
        client.delete().forPath("/a/b");
        client.delete().forPath("/a");
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Aggregations

BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)37 CuratorFramework (org.apache.curator.framework.CuratorFramework)33 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)33 CountDownLatch (java.util.concurrent.CountDownLatch)18 Test (org.testng.annotations.Test)14 RetryOneTime (org.apache.curator.retry.RetryOneTime)11 KeeperException (org.apache.zookeeper.KeeperException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Watcher (org.apache.zookeeper.Watcher)6 Timing (org.apache.curator.test.Timing)4 Test (org.junit.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 CreateBuilder (org.apache.curator.framework.api.CreateBuilder)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 Message (com.google.protobuf.Message)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2