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();
}
}
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);
}
}
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();
}
}
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();
}
}
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);
}
}
Aggregations