WatcherRemovalManager { @VisibleForTesting Set<? extends Watcher> getEntries() { return Sets.newHashSet(entries); } WatcherRemovalManager(CuratorFrameworkImpl client);   }
@Test public void testWithRetry() throws Exception { server.stop(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework(); Watcher w = new Watcher() { @Override public void process(WatchedEvent event) { } }; try { removerClient.checkExists().usingWatcher(w).forPath("/one/two/three"); Assert.fail("Should have thrown ConnectionLossException"); } catch ( KeeperException.ConnectionLossException expected ) { } Assert.assertEquals(removerClient.getWatcherRemovalManager().getEntries().size(), 0); } finally { TestCleanState.closeAndTestClean(client); } }
@Test public void testWithRetryInBackground() throws Exception { server.stop(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework(); Watcher w = new Watcher() { @Override public void process(WatchedEvent event) { } }; final CountDownLatch latch = new CountDownLatch(1); BackgroundCallback callback = new BackgroundCallback() { @Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { latch.countDown(); } }; removerClient.checkExists().usingWatcher(w).inBackground(callback).forPath("/one/two/three"); Assert.assertTrue(new Timing().awaitLatch(latch)); Assert.assertEquals(removerClient.getWatcherRemovalManager().getEntries().size(), 0); } finally { TestCleanState.closeAndTestClean(client); } }
@Test public void testTriggered() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework(); final CountDownLatch latch = new CountDownLatch(1); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if ( event.getType() == Event.EventType.NodeCreated ) { latch.countDown(); } } }; removerClient.checkExists().usingWatcher(watcher).forPath("/yo"); Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1); removerClient.create().forPath("/yo"); Assert.assertTrue(new Timing().awaitLatch(latch)); Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 0); } finally { TestCleanState.closeAndTestClean(client); } }
@Test public void testResetFromWatcher() throws Exception { Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); final WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework(); final CountDownLatch createdLatch = new CountDownLatch(1); final CountDownLatch deletedLatch = new CountDownLatch(1); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if ( event.getType() == Event.EventType.NodeCreated ) { try { removerClient.checkExists().usingWatcher(this).forPath("/yo"); } catch ( Exception e ) { e.printStackTrace(); } createdLatch.countDown(); } else if ( event.getType() == Event.EventType.NodeDeleted ) { deletedLatch.countDown(); } } }; removerClient.checkExists().usingWatcher(watcher).forPath("/yo"); Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1); removerClient.create().forPath("/yo"); Assert.assertTrue(timing.awaitLatch(createdLatch)); Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1); removerClient.delete().forPath("/yo"); Assert.assertTrue(timing.awaitLatch(deletedLatch)); Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 0); } finally { TestCleanState.closeAndTestClean(client); } }