@Test(timeout = 40000)
public void testSendCloseSession() throws Exception {

    Assert.assertTrue(
            "Didn't instantiate ServerCnxnFactory with NettyServerCnxnFactory!",
            serverFactory instanceof NettyServerCnxnFactory);
    final ZooKeeper zk = createClient();
    final ZooKeeperServer zkServer = getServer(serverFactory);
    final String path = "/a";
    try {
        // make sure zkclient works
        zk.create(path, "test".getBytes(StandardCharsets.UTF_8), Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        // set on watch
        Assert.assertNotNull("Didn't create znode:" + path,
                zk.exists(path, true));
        Assert.assertEquals(1, zkServer.getZKDatabase().getDataTree().getWatchCount());
        Iterable<ServerCnxn> connections = serverFactory.getConnections();
        Assert.assertEquals("Mismatch in number of live connections!", 1,
                serverFactory.getNumAliveConnections());
        for (ServerCnxn serverCnxn : connections) {
            serverCnxn.sendCloseSession();
        }
        LOG.info("Waiting for the channel disconnected event");
        int timeout = 0;
        while (serverFactory.getNumAliveConnections() != 0) {
            Thread.sleep(1000);
            timeout += 1000;
            if (timeout > CONNECTION_TIMEOUT) {
                Assert.fail("The number of live connections should be 0");
            }
        }
        // make sure the watch is removed when the connection closed
        Assert.assertEquals(0, zkServer.getZKDatabase().getDataTree().getWatchCount());
    } finally {
        zk.close();
    }
}