### Question:
NettyServerCnxn extends ServerCnxn { @Override public void sendCloseSession() { sendBuffer(ServerCnxnFactory.closeConn); } NettyServerCnxn(Channel channel, ZooKeeperServer zks, NettyServerCnxnFactory factory); @Override void close(DisconnectReason reason); void close(); @Override long getSessionId(); @Override int getSessionTimeout(); @Override void process(WatchedEvent event); @Override int sendResponse(ReplyHeader h, Record r, String tag,
                             String cacheKey, Stat stat, int opCode); @Override void setSessionId(long sessionId); @Override void sendBuffer(ByteBuffer... buffers); @Override void disableRecv(boolean waitDisableRecv); @Override void enableRecv(); @Override void setSessionTimeout(int sessionTimeout); @Override int getInterestOps(); @Override InetSocketAddress getRemoteSocketAddress(); @Override void sendCloseSession(); @Override boolean isSecure(); @Override Certificate[] getClientCertificateChain(); @Override void setClientCertificateChain(Certificate[] chain); int getQueuedReadableBytes(); void setHandshakeState(HandshakeState state); HandshakeState getHandshakeState(); public int readIssuedAfterReadComplete; }### Answer:
@Test @Timeout(value = 40) public void testSendCloseSession() throws Exception { assertTrue(serverFactory instanceof NettyServerCnxnFactory, "Didn't instantiate ServerCnxnFactory with NettyServerCnxnFactory!"); final ZooKeeper zk = createClient(); final ZooKeeperServer zkServer = serverFactory.getZooKeeperServer(); final String path = "/a"; try { zk.create(path, "test".getBytes(StandardCharsets.UTF_8), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); assertNotNull(zk.exists(path, true), "Didn't create znode:" + path); assertEquals(1, zkServer.getZKDatabase().getDataTree().getWatchCount()); Iterable<ServerCnxn> connections = serverFactory.getConnections(); assertEquals(1, serverFactory.getNumAliveConnections(), "Mismatch in number of live connections!"); 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) { fail("The number of live connections should be 0"); } } assertEquals(0, zkServer.getZKDatabase().getDataTree().getWatchCount()); } finally { zk.close(); } }