Search in sources :

Example 46 with Timing

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

the class TestDistributedQueue method testFlush.

@Test
public void testFlush() throws Exception {
    final Timing timing = new Timing();
    final CountDownLatch latch = new CountDownLatch(1);
    DistributedQueue<TestQueueItem> queue = null;
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        final AtomicBoolean firstTime = new AtomicBoolean(true);
        queue = new DistributedQueue<TestQueueItem>(client, null, serializer, "/test", new ThreadFactoryBuilder().build(), MoreExecutors.directExecutor(), 10, true, null, QueueBuilder.NOT_SET, true, 0) {

            @Override
            void internalCreateNode(final String path, final byte[] bytes, final BackgroundCallback callback) throws Exception {
                if (firstTime.compareAndSet(true, false)) {
                    Executors.newSingleThreadExecutor().submit(new Callable<Object>() {

                        @Override
                        public Object call() throws Exception {
                            latch.await();
                            timing.sleepABit();
                            client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).inBackground(callback).forPath(path, bytes);
                            return null;
                        }
                    });
                } else {
                    super.internalCreateNode(path, bytes, callback);
                }
            }
        };
        queue.start();
        queue.put(new TestQueueItem("1"));
        Assert.assertFalse(queue.flushPuts(timing.forWaiting().seconds(), TimeUnit.SECONDS));
        latch.countDown();
        Assert.assertTrue(queue.flushPuts(timing.forWaiting().seconds(), TimeUnit.SECONDS));
    } finally {
        if (latch.getCount() > 0) {
            latch.countDown();
        }
        CloseableUtils.closeQuietly(queue);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 47 with Timing

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

the class TestSharedCount method testMultiClientVersioned.

@Test
public void testMultiClientVersioned() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    CuratorFramework client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    SharedCount count1 = new SharedCount(client1, "/count", 0);
    SharedCount count2 = new SharedCount(client2, "/count", 0);
    try {
        client1.start();
        client2.start();
        count1.start();
        count2.start();
        VersionedValue<Integer> versionedValue = count1.getVersionedValue();
        Assert.assertTrue(count1.trySetCount(versionedValue, 10));
        timing.sleepABit();
        versionedValue = count2.getVersionedValue();
        Assert.assertTrue(count2.trySetCount(versionedValue, 20));
        timing.sleepABit();
        VersionedValue<Integer> versionedValue1 = count1.getVersionedValue();
        VersionedValue<Integer> versionedValue2 = count2.getVersionedValue();
        Assert.assertTrue(count2.trySetCount(versionedValue2, 30));
        Assert.assertFalse(count1.trySetCount(versionedValue1, 40));
        versionedValue1 = count1.getVersionedValue();
        Assert.assertTrue(count1.trySetCount(versionedValue1, 40));
    } finally {
        CloseableUtils.closeQuietly(count2);
        CloseableUtils.closeQuietly(count1);
        CloseableUtils.closeQuietly(client2);
        CloseableUtils.closeQuietly(client1);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 48 with Timing

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

the class TestPathChildrenCache method testUpdateWhenNotCachingData.

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

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
                    updatedLatch.countDown();
                } else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
                    addedLatch.countDown();
                }
            }
        });
        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        client.create().forPath("/test/foo", "first".getBytes());
        Assert.assertTrue(timing.awaitLatch(addedLatch));
        client.setData().forPath("/test/foo", "something new".getBytes());
        Assert.assertTrue(timing.awaitLatch(updatedLatch));
    } finally {
        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 49 with Timing

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

the class TestPathChildrenCache method testAsyncInitialPopulation.

@Test
public void testAsyncInitialPopulation() 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");
        client.create().forPath("/test/one", "hey there".getBytes());
        final BlockingQueue<PathChildrenCacheEvent> events = new LinkedBlockingQueue<PathChildrenCacheEvent>();
        cache = new PathChildrenCache(client, "/test", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                events.offer(event);
            }
        });
        cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        PathChildrenCacheEvent event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.CHILD_ADDED);
        event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.INITIALIZED);
        Assert.assertEquals(event.getInitialData().size(), 1);
    } 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 50 with Timing

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

the class TestPathChildrenCache method testIssue27.

// see https://github.com/Netflix/curator/issues/27 - was caused by not comparing old->new data
@Test
public void testIssue27() 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("/base");
        client.create().forPath("/base/a");
        client.create().forPath("/base/b");
        client.create().forPath("/base/c");
        client.getChildren().forPath("/base");
        final List<PathChildrenCacheEvent.Type> events = Lists.newArrayList();
        final Semaphore semaphore = new Semaphore(0);
        PathChildrenCache cache = new PathChildrenCache(client, "/base", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                events.add(event.getType());
                semaphore.release();
            }
        });
        cache.start();
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 3));
        client.delete().forPath("/base/a");
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        client.create().forPath("/base/a");
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        List<PathChildrenCacheEvent.Type> expected = Lists.newArrayList(PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_REMOVED, PathChildrenCacheEvent.Type.CHILD_ADDED);
        Assert.assertEquals(expected, events);
    } 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)

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