use of org.apache.curator.framework.api.CuratorEvent 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.CuratorEvent 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);
}
}
use of org.apache.curator.framework.api.CuratorEvent in project xian by happyyangyuan.
the class TestFrameworkEdges method testMissedResponseOnBackgroundESCreate.
@Test
public void testMissedResponseOnBackgroundESCreate() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
CreateBuilderImpl createBuilder = (CreateBuilderImpl) client.create();
createBuilder.failNextCreateForTesting = true;
final BlockingQueue<String> queue = Queues.newArrayBlockingQueue(1);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
queue.put(event.getPath());
}
};
createBuilder.withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(callback).forPath("/");
String ourPath = queue.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertTrue(ourPath.startsWith(ZKPaths.makePath("/", CreateBuilderImpl.PROTECTED_PREFIX)));
Assert.assertFalse(createBuilder.failNextCreateForTesting);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorEvent in project xian by happyyangyuan.
the class TestFrameworkEdges method testNestedCalls.
@Test
public void testNestedCalls() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
client.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.EXISTS) {
Stat stat = client.checkExists().forPath("/yo/yo/yo");
Assert.assertNull(stat);
client.create().inBackground(event.getContext()).forPath("/what");
} else if (event.getType() == CuratorEventType.CREATE) {
((CountDownLatch) event.getContext()).countDown();
}
}
});
CountDownLatch latch = new CountDownLatch(1);
client.checkExists().inBackground(latch).forPath("/hey");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorEvent in project xian by happyyangyuan.
the class TestMultiClient method testNotify.
@Test
public void testNotify() throws Exception {
CuratorFramework client1 = null;
CuratorFramework client2 = null;
try {
client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client1.start();
client2.start();
final CountDownLatch latch = new CountDownLatch(1);
client1.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.WATCHED) {
if (event.getWatchedEvent().getType() == Watcher.Event.EventType.NodeDataChanged) {
if (event.getPath().equals("/test")) {
latch.countDown();
}
}
}
}
});
client1.create().forPath("/test", new byte[] { 1, 2, 3 });
client1.checkExists().watched().forPath("/test");
client2.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.SYNC) {
client.setData().forPath("/test", new byte[] { 10, 20 });
}
}
});
client2.sync().forPath("/test");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(client1);
CloseableUtils.closeQuietly(client2);
}
}
Aggregations