Search in sources :

Example 41 with Timing

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

the class TestInterProcessMutexBase method testContainerCleanup.

@Test
public void testContainerCleanup() throws Exception {
    if (!ZKPaths.hasContainerSupport()) {
        System.out.println("ZooKeeper version does not support Containers. Skipping test");
        return;
    }
    server.close();
    System.setProperty("container.checkIntervalMs", "10");
    try {
        server = new TestingServer();
        final int THREAD_QTY = 10;
        ExecutorService service = null;
        final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(100, 3));
        try {
            client.start();
            List<Future<Object>> threads = Lists.newArrayList();
            service = Executors.newCachedThreadPool();
            for (int i = 0; i < THREAD_QTY; ++i) {
                Future<Object> t = service.submit(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        InterProcessLock lock = makeLock(client);
                        lock.acquire();
                        try {
                            Thread.sleep(10);
                        } finally {
                            lock.release();
                        }
                        return null;
                    }
                });
                threads.add(t);
            }
            for (Future<Object> t : threads) {
                t.get();
            }
            new Timing().sleepABit();
            Assert.assertNull(client.checkExists().forPath(LOCK_BASE_PATH));
        } finally {
            if (service != null) {
                service.shutdownNow();
            }
            CloseableUtils.closeQuietly(client);
        }
    } finally {
        System.clearProperty("container.checkIntervalMs");
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 42 with Timing

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

the class TestInterProcessMutexBase method testWaitingProcessKilledServer.

@Test
public void testWaitingProcessKilledServer() throws Exception {
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    try {
        client.start();
        final CountDownLatch latch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        final AtomicBoolean isFirst = new AtomicBoolean(true);
        ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(Executors.newFixedThreadPool(2));
        for (int i = 0; i < 2; ++i) {
            service.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    InterProcessLock lock = makeLock(client);
                    lock.acquire();
                    try {
                        if (isFirst.compareAndSet(true, false)) {
                            timing.sleepABit();
                            server.stop();
                            Assert.assertTrue(timing.awaitLatch(latch));
                            server.restart();
                        }
                    } finally {
                        try {
                            lock.release();
                        } catch (Exception e) {
                        // ignore
                        }
                    }
                    return null;
                }
            });
        }
        for (int i = 0; i < 2; ++i) {
            service.take().get(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
        }
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 43 with Timing

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

the class TestPathChildrenCache method testBasicsOnTwoCachesWithSameExecutor.

@Test
public void testBasicsOnTwoCachesWithSameExecutor() 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>();
        final ExecutorService exec = Executors.newSingleThreadExecutor();
        PathChildrenCache cache = new PathChildrenCache(client, "/test", true, false, exec);
        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();
        final BlockingQueue<PathChildrenCacheEvent.Type> events2 = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
        PathChildrenCache cache2 = new PathChildrenCache(client, "/test", true, false, exec);
        cache2.getListenable().addListener(new PathChildrenCacheListener() {

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

Example 44 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 45 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)

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