@Test
public void rapidChanges() throws Exception {

    final int maxPropagationTime = 100;
    final String pathPrefix = "/foo/rapido";
    curator.create().creatingParentsIfNeeded().forPath(pathPrefix);
    final int numNodes = 50;
    final ListenerCounter listener = new ListenerCounter(numNodes, 0, numNodes);
    final NodeCollectionWatcher nodeCollectionWatcher = new NodeCollectionWatcher(zooKeeper, pathPrefix, listener);
    // Create all of the nodes at once
    for (int i = 0; i < numNodes; i++) {
        curator.create().forPath(pathPrefix + "/node" + i, getData());
    }
    assertTrue(listener.createLatch.await(maxPropagationTime, TimeUnit.MILLISECONDS));
    assertThat(listener.createCount.get(), is(numNodes));
    assertThat(listener.dataCount.get(), is(0));
    assertThat(listener.removeCount.get(), is(0));
    // Repeat data test multiple times to ensure data changes are detected
    // repeatedly on the same nodes
    int total = 0;
    for (int j = 0; j < 5; j++) {
        listener.dataLatch = new CountDownLatch(numNodes);
        // Since there's a watch for every node all of the data changes should be detected
        for (int i = 0; i < numNodes; i++) {
            curator.setData().forPath(pathPrefix + "/node" + i, getData());
        }
        total += numNodes;
        assertTrue(listener.dataLatch.await(maxPropagationTime, TimeUnit.MILLISECONDS));
        assertThat(listener.createCount.get(), is(numNodes));
        assertThat(listener.dataCount.get(), is(total));
        assertThat(listener.removeCount.get(), is(0));
    }
    // Finally, remove everything in rapid succession
    // Create all of the nodes at once
    for (int i = 0; i < numNodes; i++) {
        curator.delete().forPath(pathPrefix + "/node" + i);
    }
    assertTrue(listener.removeLatch.await(maxPropagationTime, TimeUnit.MILLISECONDS));
    assertThat(listener.createCount.get(), is(numNodes));
    assertThat(listener.dataCount.get(), is(total));
    assertThat(listener.removeCount.get(), is(numNodes));
    nodeCollectionWatcher.shutdown();
}