@Test
  public void testCbHandlerLeakOnParticipantSessionExpiry() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    final int n = 2;

    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));

    TestHelper.setupCluster(
        clusterName,
        ZK_ADDR,
        12918, // participant port
        "localhost", // participant name prefix
        "TestDB", // resource name prefix
        1, // resources
        32, // partitions per resource
        n, // number of nodes
        2, // replicas
        "MasterSlave",
        true); // do rebalance

    final ClusterControllerManager controller =
        new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    controller.syncStart();

    // start participants
    MockParticipantManager[] participants = new MockParticipantManager[n];
    for (int i = 0; i < n; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
      participants[i].syncStart();
    }

    boolean result =
        ClusterStateVerifier.verifyByZkCallback(
            new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    final MockParticipantManager participantManagerToExpire = participants[1];

    // check controller zk-watchers
    result =
        TestHelper.verify(
            new TestHelper.Verifier() {

              @Override
              public boolean verify() throws Exception {
                Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR);
                // Set<String> watchPaths = watchers.get("0x" + controllerManager.getSessionId());
                Set<String> watchPaths = watchers.get("0x" + controller.getSessionId());
                // System.out.println("controller watch paths: " + watchPaths);

                // controller should have 5 + 2n + m + (m+2)n zk-watchers
                // where n is number of nodes and m is number of resources
                return watchPaths.size() == (6 + 5 * n);
              }
            },
            500);
    Assert.assertTrue(result, "Controller should have 6 + 5*n zk-watchers.");

    // check participant zk-watchers
    result =
        TestHelper.verify(
            new TestHelper.Verifier() {

              @Override
              public boolean verify() throws Exception {
                Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR);
                Set<String> watchPaths =
                    watchers.get("0x" + participantManagerToExpire.getSessionId());
                // System.out.println("participant watch paths: " + watchPaths);

                // participant should have 2 zk-watchers: 1 for MESSAGE and 1 for CONTROLLER
                return watchPaths.size() == 2;
              }
            },
            500);
    Assert.assertTrue(result, "Participant should have 2 zk-watchers.");

    // check HelixManager#_handlers
    // printHandlers(controllerManager);
    // printHandlers(participantManagerToExpire);
    int controllerHandlerNb = controller.getHandlers().size();
    int particHandlerNb = participantManagerToExpire.getHandlers().size();
    Assert.assertEquals(
        controllerHandlerNb,
        9,
        "HelixController should have 9 (5+2n) callback handlers for 2 (n) participant");
    Assert.assertEquals(
        particHandlerNb, 2, "HelixParticipant should have 2 (msg+cur-state) callback handlers");

    // expire the session of participant
    System.out.println("Expiring participant session...");
    String oldSessionId = participantManagerToExpire.getSessionId();

    ZkTestHelper.expireSession(participantManagerToExpire.getZkClient());
    String newSessionId = participantManagerToExpire.getSessionId();
    System.out.println(
        "Expried participant session. oldSessionId: "
            + oldSessionId
            + ", newSessionId: "
            + newSessionId);

    result =
        ClusterStateVerifier.verifyByPolling(
            new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);

    // check controller zk-watchers
    result =
        TestHelper.verify(
            new TestHelper.Verifier() {

              @Override
              public boolean verify() throws Exception {
                Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR);
                Set<String> watchPaths = watchers.get("0x" + controller.getSessionId());
                // System.out.println("controller watch paths after session expiry: " + watchPaths);

                // controller should have 5 + 2n + m + (m+2)n zk-watchers
                // where n is number of nodes and m is number of resources
                return watchPaths.size() == (6 + 5 * n);
              }
            },
            500);
    Assert.assertTrue(result, "Controller should have 6 + 5*n zk-watchers after session expiry.");

    // check participant zk-watchers
    result =
        TestHelper.verify(
            new TestHelper.Verifier() {

              @Override
              public boolean verify() throws Exception {
                Map<String, Set<String>> watchers = ZkTestHelper.getListenersBySession(ZK_ADDR);
                Set<String> watchPaths =
                    watchers.get("0x" + participantManagerToExpire.getSessionId());
                // System.out.println("participant watch paths after session expiry: " +
                // watchPaths);

                // participant should have 2 zk-watchers: 1 for MESSAGE and 1 for CONTROLLER
                return watchPaths.size() == 2;
              }
            },
            500);
    Assert.assertTrue(result, "Participant should have 2 zk-watchers after session expiry.");

    // check handlers
    // printHandlers(controllerManager);
    // printHandlers(participantManagerToExpire);
    int handlerNb = controller.getHandlers().size();
    Assert.assertEquals(
        handlerNb,
        controllerHandlerNb,
        "controller callback handlers should not increase after participant session expiry");
    handlerNb = participantManagerToExpire.getHandlers().size();
    Assert.assertEquals(
        handlerNb,
        particHandlerNb,
        "participant callback handlers should not increase after participant session expiry");

    // clean up
    controller.syncStop();
    for (int i = 0; i < n; i++) {
      participants[i].syncStop();
    }

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
  }