Search in sources :

Example 1 with ExecuteCalledWatchingExecutorService

use of org.apache.curator.test.ExecuteCalledWatchingExecutorService 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 2 with ExecuteCalledWatchingExecutorService

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

the class TestServiceCache method testExecutorServiceIsInvoked.

@Test
public void testExecutorServiceIsInvoked() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/discovery").client(client).build();
        closeables.add(discovery);
        discovery.start();
        ExecuteCalledWatchingExecutorService exec = new ExecuteCalledWatchingExecutorService(Executors.newSingleThreadExecutor());
        Assert.assertFalse(exec.isExecuteCalled());
        ServiceCache<String> cache = discovery.serviceCacheBuilder().name("test").executorService(exec).build();
        closeables.add(cache);
        cache.start();
        final Semaphore semaphore = new Semaphore(0);
        ServiceCacheListener listener = new ServiceCacheListener() {

            @Override
            public void cacheChanged() {
                semaphore.release();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        cache.addListener(listener);
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        discovery.registerService(instance1);
        Assert.assertTrue(semaphore.tryAcquire(10, TimeUnit.SECONDS));
        Assert.assertTrue(exec.isExecuteCalled());
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ExecuteCalledWatchingExecutorService(org.apache.curator.test.ExecuteCalledWatchingExecutorService) Closeable(java.io.Closeable) ServiceCacheListener(org.apache.curator.x.discovery.details.ServiceCacheListener) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) Test(org.testng.annotations.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)2 RetryOneTime (org.apache.curator.retry.RetryOneTime)2 ExecuteCalledWatchingExecutorService (org.apache.curator.test.ExecuteCalledWatchingExecutorService)2 Test (org.testng.annotations.Test)2 Closeable (java.io.Closeable)1 Semaphore (java.util.concurrent.Semaphore)1 ConnectionState (org.apache.curator.framework.state.ConnectionState)1 Timing (org.apache.curator.test.Timing)1 ServiceCacheListener (org.apache.curator.x.discovery.details.ServiceCacheListener)1