Source:
<code>public void testBindByAddress() throws Exception { String bindAddress = null; Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); while(intfs.hasMoreElements()) { NetworkInterface i = intfs.nextElement(); try { if (i.isLoopback()) { Enumeration<InetAddress> addrs = i.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress a = addrs.nextElement(); if(a.isLoopbackAddress()) { bindAddress = a.getHostAddress(); break; } } } } catch (SocketException se) { LOG.warn("Couldn't find loopback interface: " + se.getMessage()); } } if (bindAddress == null) { LOG.warn("Unable to determine loop back address, skipping test"); return; } final int PORT = PortAssignment.unique(); LOG.info("Using " + bindAddress + " as the bind address"); final String HOSTPORT = bindAddress + ":" + PORT; LOG.info("Using " + HOSTPORT + " as the host/port"); File tmpDir = ClientBase.createTmpDir(); ClientBase.setupTestEnv(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); ServerCnxnFactory f = ServerCnxnFactory.createFactory( new InetSocketAddress(bindAddress, PORT), -1); f.startup(zks); LOG.info("starting up the the server, waiting"); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); startSignal = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); try { startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); <START> Assert.assertTrue("count == " + startSignal.getCount(), startSignal.getCount() == 0); <END> zk.close(); } finally { f.shutdown(); zks.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); } }</code><technical_language>a huge fan of calling getCount more once, I change invocations. return await in line above</technical_language>

Target:
public void testBindByAddress() throws Exception { String bindAddress = null; Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); while(intfs.hasMoreElements()) { NetworkInterface i = intfs.nextElement(); try { if (i.isLoopback()) { Enumeration<InetAddress> addrs = i.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress a = addrs.nextElement(); if(a.isLoopbackAddress()) { bindAddress = a.getHostAddress(); break; } } } } catch (SocketException se) { LOG.warn("Couldn't find loopback interface: " + se.getMessage()); } } if (bindAddress == null) { LOG.warn("Unable to determine loop back address, skipping test"); return; } final int PORT = PortAssignment.unique(); LOG.info("Using " + bindAddress + " as the bind address"); final String HOSTPORT = bindAddress + ":" + PORT; LOG.info("Using " + HOSTPORT + " as the host/port"); File tmpDir = ClientBase.createTmpDir(); ClientBase.setupTestEnv(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); ServerCnxnFactory f = ServerCnxnFactory.createFactory( new InetSocketAddress(bindAddress, PORT), -1); f.startup(zks); LOG.info("starting up the the server, waiting"); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); startSignal = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); try { boolean isZero = startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertTrue("count == " + startSignal.getCount(), isZero); zk.close(); } finally { f.shutdown(); zks.shutdown(); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); } }