{
TreeCache cache2 = newTreeCacheWithListeners(client, "/test");
cache2.getListenable().removeListener(eventListener); // Don't listen on the second cache.
// Just ensures the same event count; enables test flow control on cache2.
final Semaphore semaphore = new Semaphore(0);
cache2.getListenable().addListener(new TreeCacheListener()
{
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception
{
semaphore.release();
}
});
try
{
client.create().forPath("/test");
cache = newTreeCacheWithListeners(client, "/test");
cache.start();
cache2.start();
assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
assertEvent(TreeCacheEvent.Type.INITIALIZED);
semaphore.acquire(2);
client.create().forPath("/test/one", "hey there".getBytes());
assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
semaphore.acquire();
Assert.assertEquals(new String(cache2.getCurrentData("/test/one").getData()), "hey there");
client.setData().forPath("/test/one", "sup!".getBytes());
assertEvent(TreeCacheEvent.Type.NODE_UPDATED, "/test/one");
Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "sup!");
semaphore.acquire();
Assert.assertEquals(new String(cache2.getCurrentData("/test/one").getData()), "sup!");
client.delete().forPath("/test/one");
assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/one");
Assert.assertNull(cache.getCurrentData("/test/one"));
semaphore.acquire();
Assert.assertNull(cache2.getCurrentData("/test/one"));
assertNoMoreEvents();
Assert.assertEquals(semaphore.availablePermits(), 0);
}
finally
{
CloseableUtils.closeQuietly(cache2);
}