/** 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(); }
@Test public void testMinMaxSessionTimeout() throws Exception { // override the defaults final int MINSESS = 20000; final int MAXSESS = 240000; ZooKeeperServer zs = serverFactory.getZooKeeperServer(); zs.setMinSessionTimeout(MINSESS); zs.setMaxSessionTimeout(MAXSESS); // validate typical case - requested == negotiated int timeout = 120000; DisconnectableZooKeeper zk = createClient(timeout); Assert.assertEquals(timeout, zk.getSessionTimeout()); // make sure tostring works in both cases LOG.info(zk.toString()); zk.close(); LOG.info(zk.toString()); // validate lower limit zk = createClient(MINSESS / 2); Assert.assertEquals(MINSESS, zk.getSessionTimeout()); LOG.info(zk.toString()); zk.close(); LOG.info(zk.toString()); // validate upper limit zk = createClient(MAXSESS * 2); Assert.assertEquals(MAXSESS, zk.getSessionTimeout()); LOG.info(zk.toString()); zk.close(); LOG.info(zk.toString()); }
/** Verify access to the negotiated session timeout. */ @Test public void testSessionTimeoutAccess() throws Exception { // validate typical case - requested == negotiated DisconnectableZooKeeper zk = createClient(TICK_TIME * 4); Assert.assertEquals(TICK_TIME * 4, zk.getSessionTimeout()); // make sure tostring works in both cases LOG.info(zk.toString()); zk.close(); LOG.info(zk.toString()); // validate lower limit zk = createClient(TICK_TIME); Assert.assertEquals(TICK_TIME * 2, zk.getSessionTimeout()); LOG.info(zk.toString()); zk.close(); LOG.info(zk.toString()); // validate upper limit zk = createClient(TICK_TIME * 30); Assert.assertEquals(TICK_TIME * 20, zk.getSessionTimeout()); LOG.info(zk.toString()); zk.close(); LOG.info(zk.toString()); }
/** * 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(); }