Search in sources :

Example 51 with Timing

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

the class TestPathChildrenCache method testRebuildAgainstOtherProcesses.

@Test
public void testRebuildAgainstOtherProcesses() throws Exception {
    Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        client.create().forPath("/test");
        client.create().forPath("/test/foo");
        client.create().forPath("/test/bar");
        client.create().forPath("/test/snafu", "original".getBytes());
        final CountDownLatch addedLatch = new CountDownLatch(2);
        final PathChildrenCache cache = new PathChildrenCache(client, "/test", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
                    if (event.getData().getPath().equals("/test/test")) {
                        addedLatch.countDown();
                    }
                } else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
                    if (event.getData().getPath().equals("/test/snafu")) {
                        addedLatch.countDown();
                    }
                }
            }
        });
        cache.rebuildTestExchanger = new Exchanger<Object>();
        ExecutorService service = Executors.newSingleThreadExecutor();
        final AtomicReference<String> deletedPath = new AtomicReference<String>();
        Future<Object> future = service.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                cache.rebuildTestExchanger.exchange(new Object());
                // simulate another process adding a node while we're rebuilding
                client.create().forPath("/test/test");
                List<ChildData> currentData = cache.getCurrentData();
                Assert.assertTrue(currentData.size() > 0);
                // simulate another process removing a node while we're rebuilding
                client.delete().forPath(currentData.get(0).getPath());
                deletedPath.set(currentData.get(0).getPath());
                cache.rebuildTestExchanger.exchange(new Object());
                ChildData childData = null;
                while (childData == null) {
                    childData = cache.getCurrentData("/test/snafu");
                    Thread.sleep(1000);
                }
                Assert.assertEquals(childData.getData(), "original".getBytes());
                client.setData().forPath("/test/snafu", "grilled".getBytes());
                cache.rebuildTestExchanger.exchange(new Object());
                return null;
            }
        });
        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        future.get();
        Assert.assertTrue(timing.awaitLatch(addedLatch));
        Assert.assertNotNull(cache.getCurrentData("/test/test"));
        Assert.assertNull(cache.getCurrentData(deletedPath.get()));
        Assert.assertEquals(cache.getCurrentData("/test/snafu").getData(), "grilled".getBytes());
        cache.close();
    } finally {
        client.close();
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) AtomicReference(java.util.concurrent.atomic.AtomicReference) KeeperException(org.apache.zookeeper.KeeperException) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecuteCalledWatchingExecutorService(org.apache.curator.test.ExecuteCalledWatchingExecutorService) List(java.util.List) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 52 with Timing

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

the class TestPathChildrenCache method testPostInitializedForEmpty.

@Test
public void testPostInitializedForEmpty() throws Exception {
    Timing timing = new Timing();
    PathChildrenCache cache = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch latch = new CountDownLatch(1);
        cache = new PathChildrenCache(client, "/test", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getType() == PathChildrenCacheEvent.Type.INITIALIZED) {
                    latch.countDown();
                }
            }
        });
        cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        Assert.assertTrue(timing.awaitLatch(latch));
    } finally {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 53 with Timing

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

the class TestPathChildrenCache method testDeleteNodeAfterCloseDoesntCallExecutor.

@Test
public void testDeleteNodeAfterCloseDoesntCallExecutor() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        client.create().forPath("/test");
        final ExecuteCalledWatchingExecutorService exec = new ExecuteCalledWatchingExecutorService(Executors.newSingleThreadExecutor());
        PathChildrenCache cache = new PathChildrenCache(client, "/test", true, false, exec);
        cache.start();
        client.create().forPath("/test/one", "hey there".getBytes());
        cache.rebuild();
        Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
        Assert.assertTrue(exec.isExecuteCalled());
        exec.setExecuteCalled(false);
        cache.close();
        Assert.assertFalse(exec.isExecuteCalled());
        client.delete().forPath("/test/one");
        timing.sleepABit();
        Assert.assertFalse(exec.isExecuteCalled());
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ExecuteCalledWatchingExecutorService(org.apache.curator.test.ExecuteCalledWatchingExecutorService) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 54 with Timing

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

the class TestPathChildrenCache method testBasics.

@Test
public void testBasics() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        client.create().forPath("/test");
        final BlockingQueue<PathChildrenCacheEvent.Type> events = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
        PathChildrenCache cache = new PathChildrenCache(client, "/test", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getData().getPath().equals("/test/one")) {
                    events.offer(event.getType());
                }
            }
        });
        cache.start();
        client.create().forPath("/test/one", "hey there".getBytes());
        Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_ADDED);
        client.setData().forPath("/test/one", "sup!".getBytes());
        Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_UPDATED);
        Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "sup!");
        client.delete().forPath("/test/one");
        Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);
        cache.close();
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 55 with Timing

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

the class TestPathChildrenCache method testChildrenInitialized.

@Test
public void testChildrenInitialized() throws Exception {
    Timing timing = new Timing();
    PathChildrenCache cache = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        client.create().forPath("/test");
        cache = new PathChildrenCache(client, "/test", true);
        final CountDownLatch addedLatch = new CountDownLatch(3);
        final CountDownLatch initLatch = new CountDownLatch(1);
        cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
                    addedLatch.countDown();
                } else if (event.getType() == PathChildrenCacheEvent.Type.INITIALIZED) {
                    initLatch.countDown();
                }
            }
        });
        client.create().forPath("/test/1", "1".getBytes());
        client.create().forPath("/test/2", "2".getBytes());
        client.create().forPath("/test/3", "3".getBytes());
        cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        Assert.assertTrue(timing.awaitLatch(addedLatch));
        Assert.assertTrue(timing.awaitLatch(initLatch));
        Assert.assertEquals(cache.getCurrentData().size(), 3);
        Assert.assertEquals(cache.getCurrentData().get(0).getData(), "1".getBytes());
        Assert.assertEquals(cache.getCurrentData().get(1).getData(), "2".getBytes());
        Assert.assertEquals(cache.getCurrentData().get(2).getData(), "3".getBytes());
    } finally {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) KeeperException(org.apache.zookeeper.KeeperException) 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