public void testWatchAutoResetWithPending() throws Exception {
MyWatcher watches[] = new MyWatcher[COUNT];
MyStatCallback cbs[] = new MyStatCallback[COUNT];
MyWatcher watcher = new MyWatcher();
int count[] = new int[1];
TestableZooKeeper zk = createClient(watcher, hostPort);
ZooKeeper zk2 = createClient(watcher, hostPort);
zk2.create("/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
for(int i = 0; i < COUNT/2; i++) {
watches[i] = new MyWatcher();
cbs[i] = new MyStatCallback();
zk.exists("/test", watches[i], cbs[i], count);
}
zk.exists("/test", false);
zk.pauseCnxn(4000);
Thread.sleep(50);
zk2.close();
stopServer();
watches[0].waitForDisconnected(3000);
for(int i = COUNT/2; i < COUNT; i++) {
watches[i] = new MyWatcher();
cbs[i] = new MyStatCallback();
zk.exists("/test", watches[i], cbs[i], count);
}
startServer();
watches[49].waitForConnected(4000);
assertEquals(null, zk.exists("/test", false));
Thread.sleep(10);
for(int i = 0; i < COUNT/2; i++) {
assertEquals("For " + i, 1, watches[i].events.size());
}
for(int i = COUNT/2; i < COUNT; i++) {
if (cbs[i].rc == 0) {
assertEquals("For " +i, 1, watches[i].events.size());
} else {
assertEquals("For " +i, 0, watches[i].events.size());
}
}
assertEquals(COUNT, count[0]);
zk.close();
}