Example #1
0
  /**
   * 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();
  }