@Override
public void handleNewSession() throws Exception {
// make sure zkclient is connected again
zkClient.waitUntilConnected();
ZkConnection connection = ((ZkConnection) zkClient.getConnection());
ZooKeeper curZookeeper = connection.getZookeeper();
LOG.info("handleNewSession. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
waitNewSession.countDown();
}
};
zkClient.subscribeStateChanges(listener);
ZkConnection connection = ((ZkConnection) zkClient.getConnection());
ZooKeeper curZookeeper = connection.getZookeeper();
String oldSessionId = Long.toHexString(curZookeeper.getSessionId());
LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper);
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
LOG.info("Watcher#process, event: " + event);
}
};
final ZooKeeper dupZookeeper =
new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
// wait until connected, then close
while (dupZookeeper.getState() != States.CONNECTED) {
Thread.sleep(10);
}
Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED,
"Fail to connect to zk using current session info");
dupZookeeper.close();
// make sure session expiry really happens
waitNewSession.await();
zkClient.unsubscribeStateChanges(listener);
connection = (ZkConnection) zkClient.getConnection();
curZookeeper = connection.getZookeeper();
String newSessionId = Long.toHexString(curZookeeper.getSessionId());
LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper);
Assert.assertNotSame(newSessionId, oldSessionId, "Fail to expire current session, zk: "
+ curZookeeper);