use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestFramework method testBackgroundDeleteWithChildren.
@Test
public void testBackgroundDeleteWithChildren() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 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.DELETE) {
Assert.assertEquals(event.getPath(), "/one/two");
((CountDownLatch) event.getContext()).countDown();
}
}
});
client.create().creatingParentsIfNeeded().forPath("/one/two/three/four");
Assert.assertNotNull(client.checkExists().forPath("/one/two/three/four"));
CountDownLatch latch = new CountDownLatch(1);
client.delete().deletingChildrenIfNeeded().inBackground(latch).forPath("/one/two");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertNull(client.checkExists().forPath("/one/two"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestFramework method testNamespaceInBackground.
@Test
public void testNamespaceInBackground() throws Exception {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework client = builder.connectString(server.getConnectString()).namespace("aisa").retryPolicy(new RetryOneTime(1)).build();
client.start();
try {
final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
CuratorListener listener = new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.EXISTS) {
queue.put(event.getPath());
}
}
};
client.getCuratorListenable().addListener(listener);
client.create().forPath("/base");
client.checkExists().inBackground().forPath("/base");
String path = queue.poll(10, TimeUnit.SECONDS);
Assert.assertEquals(path, "/base");
client.getCuratorListenable().removeListener(listener);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
queue.put(event.getPath());
}
};
client.getChildren().inBackground(callback).forPath("/base");
path = queue.poll(10, TimeUnit.SECONDS);
Assert.assertEquals(path, "/base");
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestFramework method testBackgroundGetDataWithWatch.
@Test
public void testBackgroundGetDataWithWatch() throws Exception {
final byte[] data1 = { 1, 2, 3 };
final byte[] data2 = { 4, 5, 6, 7 };
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client.start();
try {
final CountDownLatch watchedLatch = new CountDownLatch(1);
client.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.GET_DATA) {
Assert.assertEquals(event.getPath(), "/test");
Assert.assertEquals(event.getData(), data1);
((CountDownLatch) event.getContext()).countDown();
} else if (event.getType() == CuratorEventType.WATCHED) {
if (event.getWatchedEvent().getType() == Watcher.Event.EventType.NodeDataChanged) {
Assert.assertEquals(event.getPath(), "/test");
watchedLatch.countDown();
}
}
}
});
client.create().forPath("/test", data1);
CountDownLatch backgroundLatch = new CountDownLatch(1);
client.getData().watched().inBackground(backgroundLatch).forPath("/test");
Assert.assertTrue(backgroundLatch.await(10, TimeUnit.SECONDS));
client.setData().forPath("/test", data2);
Assert.assertTrue(watchedLatch.await(10, TimeUnit.SECONDS));
byte[] checkData = client.getData().forPath("/test");
Assert.assertEquals(checkData, data2);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorListener in project xian by happyyangyuan.
the class TestFramework method testSync.
@Test
public void testSync() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 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.SYNC) {
Assert.assertEquals(event.getPath(), "/head");
((CountDownLatch) event.getContext()).countDown();
}
}
});
client.create().forPath("/head");
Assert.assertNotNull(client.checkExists().forPath("/head"));
CountDownLatch latch = new CountDownLatch(1);
client.sync("/head", latch);
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.api.CuratorListener 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);
}
}
Aggregations