Source:
public void testOutstandingHandshakeLimit() throws Exception { SimpleCounter tlsHandshakeExceeded = (SimpleCounter) ServerMetrics.getMetrics().TLS_HANDSHAKE_EXCEEDED; tlsHandshakeExceeded.reset(); Assert.assertEquals(tlsHandshakeExceeded.get(), 0); ClientX509Util x509Util = SSLAuthTest.setUpSecure(); NettyServerCnxnFactory factory = (NettyServerCnxnFactory) serverFactory; factory.setSecure(true); factory.setOutstandingHandshakeLimit(10); int threadNum = 3; int cnxnPerThread = 10; Thread[] cnxnWorker = new Thread[threadNum]; final LinkedBlockingQueue<ZooKeeper> zks = new LinkedBlockingQueue<ZooKeeper>(); AtomicInteger cnxnCreated = new AtomicInteger(0); CountDownLatch latch = new CountDownLatch(1); for (int i = 0; i < cnxnWorker.length; i++) { cnxnWorker[i] = new Thread() { @Override public void run() { for (int i = 0; i < cnxnPerThread; i++) { try { zks.add(new ZooKeeper(hostPort, 3000, new Watcher() { @Override public void process(WatchedEvent event) { int created = cnxnCreated.addAndGet(1); if (created == threadNum * cnxnPerThread) { latch.countDown(); } } })); } catch (Exception e) { LOG.info("Error while creating zk client", e); } } } }; cnxnWorker[i].start(); } Assert.assertTrue(latch.await(3, TimeUnit.SECONDS)); LOG.info("created connections", threadNum * cnxnPerThread); long handshakeThrottledNum = tlsHandshakeExceeded.get(); LOG.info("TLS_HANDSHAKE_EXCEEDED: {}", handshakeThrottledNum); Assert.assertTrue(handshakeThrottledNum > 0); int outstandingHandshakeNum = factory.getOutstandingHandshakeNum(); LOG.info("outstanding handshake is {}", outstandingHandshakeNum); Assert.assertTrue(outstandingHandshakeNum == 0); for (ZooKeeper zk : zks) { zk.close(); } }

Target:
public void testOutstandingHandshakeLimit() throws Exception { SimpleCounter tlsHandshakeExceeded = (SimpleCounter) ServerMetrics.getMetrics().TLS_HANDSHAKE_EXCEEDED; tlsHandshakeExceeded.reset(); Assert.assertEquals(tlsHandshakeExceeded.get(), 0); ClientX509Util x509Util = SSLAuthTest.setUpSecure(); NettyServerCnxnFactory factory = (NettyServerCnxnFactory) serverFactory; factory.setSecure(true); factory.setOutstandingHandshakeLimit(10); int threadNum = 3; int cnxnPerThread = 10; Thread[] cnxnWorker = new Thread[threadNum]; AtomicInteger cnxnCreated = new AtomicInteger(0); CountDownLatch latch = new CountDownLatch(1); for (int i = 0; i < cnxnWorker.length; i++) { cnxnWorker[i] = new Thread() { @Override public void run() { for (int i = 0; i < cnxnPerThread; i++) { try { zks.add(new ZooKeeper(hostPort, 3000, new Watcher() { @Override public void process(WatchedEvent event) { int created = cnxnCreated.addAndGet(1); if (created == threadNum * cnxnPerThread) { latch.countDown(); } } })); } catch (Exception e) { LOG.info("Error while creating zk client", e); } } } }; cnxnWorker[i].start(); } Assert.assertThat(latch.await(3, TimeUnit.SECONDS), Matchers.is(true)); LOG.info("created connections", threadNum * cnxnPerThread); long handshakeThrottledNum = tlsHandshakeExceeded.get(); LOG.info("TLS_HANDSHAKE_EXCEEDED: {}", handshakeThrottledNum); Assert.assertThat("The number of handshake throttled should be " + "greater than 0", handshakeThrottledNum, Matchers.greaterThan(0L)); int outstandingHandshakeNum = factory.getOutstandingHandshakeNum(); LOG.info("outstanding handshake is {}", outstandingHandshakeNum); Assert.assertThat("The outstanding handshake number should be 0 " + "after all cnxns established", outstandingHandshakeNum, Matchers.is(0)); }