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
{
client.close();
}
}