use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.
the class TestFramework method testExistsCreatingParentsInBackground.
@Test
public void testExistsCreatingParentsInBackground() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
try {
client.start();
Assert.assertNull(client.checkExists().forPath("/one/two"));
final CountDownLatch latch = new CountDownLatch(1);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
latch.countDown();
}
};
client.checkExists().creatingParentContainersIfNeeded().inBackground(callback).forPath("/one/two/three");
Assert.assertTrue(new Timing().awaitLatch(latch));
Assert.assertNotNull(client.checkExists().forPath("/one/two"));
Assert.assertNull(client.checkExists().forPath("/one/two/three"));
Assert.assertNull(client.checkExists().creatingParentContainersIfNeeded().forPath("/one/two/three"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.
the class TestFramework method testCustomCallback.
@Test
public void testCustomCallback() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client.start();
try {
final CountDownLatch latch = new CountDownLatch(1);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.CREATE) {
if (event.getPath().equals("/head")) {
latch.countDown();
}
}
}
};
client.create().inBackground(callback).forPath("/head");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.
the class TestFrameworkBackground method testBasic.
@Test
public void testBasic() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final CountDownLatch latch = new CountDownLatch(3);
final List<String> paths = Lists.newArrayList();
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
paths.add(event.getPath());
latch.countDown();
}
};
client.create().inBackground(callback).forPath("/one");
client.create().inBackground(callback).forPath("/one/two");
client.create().inBackground(callback).forPath("/one/two/three");
latch.await();
Assert.assertEquals(paths, Arrays.asList("/one", "/one/two", "/one/two/three"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.
the class TestFrameworkBackground method testCuratorCallbackOnError.
/**
* Attempt a background operation while Zookeeper server is down.
* Return code must be {@link Code#CONNECTIONLOSS}
*/
@Test
public void testCuratorCallbackOnError() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1000)).build();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.start();
BackgroundCallback curatorCallback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getResultCode() == Code.CONNECTIONLOSS.intValue()) {
latch.countDown();
}
}
};
// Stop the Zookeeper server
server.stop();
// Attempt to retrieve children list
client.getChildren().inBackground(curatorCallback).forPath("/");
// Check if the callback has been called with a correct return code
Assert.assertTrue(timing.awaitLatch(latch), "Callback has not been called by curator !");
} finally {
client.close();
}
}
use of org.apache.curator.framework.api.BackgroundCallback in project xian by happyyangyuan.
the class TestFrameworkEdges method internalTestPathsFromProtectingInBackground.
private void internalTestPathsFromProtectingInBackground(CreateMode mode) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
try {
client.start();
client.create().creatingParentsIfNeeded().forPath("/a/b/c");
final BlockingQueue<String> paths = new ArrayBlockingQueue<String>(2);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
paths.put(event.getName());
paths.put(event.getPath());
}
};
final String TEST_PATH = "/a/b/c/test-";
client.create().withMode(mode).inBackground(callback).forPath(TEST_PATH);
String name1 = paths.take();
String path1 = paths.take();
client.close();
client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
client.start();
CreateBuilderImpl createBuilder = (CreateBuilderImpl) client.create().withProtection();
client.create().forPath(createBuilder.adjustPath(TEST_PATH));
createBuilder.debugForceFindProtectedNode = true;
createBuilder.withMode(mode).inBackground(callback).forPath(TEST_PATH);
String name2 = paths.take();
String path2 = paths.take();
Assert.assertEquals(ZKPaths.getPathAndNode(name1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
Assert.assertEquals(ZKPaths.getPathAndNode(name2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
Assert.assertEquals(ZKPaths.getPathAndNode(path1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
Assert.assertEquals(ZKPaths.getPathAndNode(path2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
client.delete().deletingChildrenIfNeeded().forPath("/a/b/c");
client.delete().forPath("/a/b");
client.delete().forPath("/a");
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations