Search in sources :

Example 16 with CuratorEvent

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

use of org.apache.curator.framework.api.CuratorEvent 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)

Example 18 with CuratorEvent

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

the class TestFrameworkEdges method testMissedResponseOnBackgroundESCreate.

@Test
public void testMissedResponseOnBackgroundESCreate() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        CreateBuilderImpl createBuilder = (CreateBuilderImpl) client.create();
        createBuilder.failNextCreateForTesting = true;
        final BlockingQueue<String> queue = Queues.newArrayBlockingQueue(1);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                queue.put(event.getPath());
            }
        };
        createBuilder.withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(callback).forPath("/");
        String ourPath = queue.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertTrue(ourPath.startsWith(ZKPaths.makePath("/", CreateBuilderImpl.PROTECTED_PREFIX)));
        Assert.assertFalse(createBuilder.failNextCreateForTesting);
    } 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) Test(org.testng.annotations.Test)

Example 19 with CuratorEvent

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

the class TestFrameworkEdges method testNestedCalls.

@Test
public void testNestedCalls() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        client.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.EXISTS) {
                    Stat stat = client.checkExists().forPath("/yo/yo/yo");
                    Assert.assertNull(stat);
                    client.create().inBackground(event.getContext()).forPath("/what");
                } else if (event.getType() == CuratorEventType.CREATE) {
                    ((CountDownLatch) event.getContext()).countDown();
                }
            }
        });
        CountDownLatch latch = new CountDownLatch(1);
        client.checkExists().inBackground(latch).forPath("/hey");
        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) Stat(org.apache.zookeeper.data.Stat) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 20 with CuratorEvent

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

the class TestMultiClient method testNotify.

@Test
public void testNotify() throws Exception {
    CuratorFramework client1 = null;
    CuratorFramework client2 = null;
    try {
        client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client1.start();
        client2.start();
        final CountDownLatch latch = new CountDownLatch(1);
        client1.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.WATCHED) {
                    if (event.getWatchedEvent().getType() == Watcher.Event.EventType.NodeDataChanged) {
                        if (event.getPath().equals("/test")) {
                            latch.countDown();
                        }
                    }
                }
            }
        });
        client1.create().forPath("/test", new byte[] { 1, 2, 3 });
        client1.checkExists().watched().forPath("/test");
        client2.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getType() == CuratorEventType.SYNC) {
                    client.setData().forPath("/test", new byte[] { 10, 20 });
                }
            }
        });
        client2.sync().forPath("/test");
        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    } finally {
        CloseableUtils.closeQuietly(client1);
        CloseableUtils.closeQuietly(client2);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Aggregations

CuratorEvent (org.apache.curator.framework.api.CuratorEvent)60 CuratorFramework (org.apache.curator.framework.CuratorFramework)57 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)36 CountDownLatch (java.util.concurrent.CountDownLatch)34 CuratorListener (org.apache.curator.framework.api.CuratorListener)23 Test (org.testng.annotations.Test)21 RetryOneTime (org.apache.curator.retry.RetryOneTime)18 KeeperException (org.apache.zookeeper.KeeperException)14 Test (org.junit.Test)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Watcher (org.apache.zookeeper.Watcher)8 ArrayList (java.util.ArrayList)4 Timing (org.apache.curator.test.Timing)4 WatchedEvent (org.apache.zookeeper.WatchedEvent)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Message (com.google.protobuf.Message)3 IOException (java.io.IOException)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3