/** * This test verifies that when the session id is reused, and the original client is disconnected, * but not session closed, that the server will remove ephemeral nodes created by the original * session. */ @Test public void testSession() throws IOException, InterruptedException, KeeperException { DisconnectableZooKeeper zk = createClient(); zk.create("/e", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); LOG.info("zk with session id 0x" + Long.toHexString(zk.getSessionId()) + " was destroyed!"); // disconnect the client by killing the socket, not sending the // session disconnect to the server as usual. This allows the test // to verify disconnect handling zk.disconnect(); Stat stat = new Stat(); startSignal = new CountDownLatch(1); zk = new DisconnectableZooKeeper( HOSTPORT, CONNECTION_TIMEOUT, this, zk.getSessionId(), zk.getSessionPasswd()); startSignal.await(); LOG.info("zk with session id 0x" + Long.toHexString(zk.getSessionId()) + " was created!"); zk.getData("/e", false, stat); LOG.info("After get data /e"); zk.close(); zk = createClient(); Assert.assertEquals(null, zk.exists("/e", false)); LOG.info("before close zk with session id 0x" + Long.toHexString(zk.getSessionId()) + "!"); zk.close(); }
/** Make sure ephemerals get cleaned up when a session times out. */ @Test public void testSessionTimeout() throws Exception { final int TIMEOUT = 5000; DisconnectableZooKeeper zk = createClient(TIMEOUT); zk.create("/stest", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); zk.disconnect(); Thread.sleep(TIMEOUT * 2); zk = createClient(TIMEOUT); zk.create("/stest", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); tearDown(); zk.close(); zk.disconnect(); setUp(); zk = createClient(TIMEOUT); Assert.assertTrue(zk.exists("/stest", false) != null); Thread.sleep(TIMEOUT * 2); Assert.assertTrue(zk.exists("/stest", false) == null); zk.close(); }