@Test
public void blockKeepAliveDeadNewConnectionHighEarlyCloseLevel()
    throws ConnectionException, InterruptedException, IOException {

  initPool(1_000, // 1s keep alive - check approx every second for keep alives.
      1, // Only 1 connection simultaneously - we expect to block!
      Integer.MAX_VALUE, //
      2); // high earlyCloseLevel to force to block
  TestConnection<ClusterManagementService.Iface> conn = (TestConnection<ClusterManagementService.Iface>) pool
      .reserveConnection(ClusterManagementService.Iface.class, ADDR1, null);
  int connId = conn.getId();
  Assert.assertEquals(conn.getServiceInfo().getServiceInterface(), ClusterManagementService.Iface.class,
      "Correct service expected");
  long beforeNanos = System.nanoTime();
  // adjust the morph of the connection to a KeepAliveService and make sure there is an exception thrown when the
  // "ping" method is called.
  conFac.adjustMorphOfConn(connId, new Consumer<TestConnection<?>>() {
    @Override
    public void accept(TestConnection<?> t) {
      try {
        if (t.getServiceInfo().getServiceInterface().equals(KeepAliveService.Iface.class)) {
          // temp unpool to install our mock
          t.pooledCAS(true, false);
          Mockito.doThrow(TException.class).when(((KeepAliveService.Iface) t.getService())).ping();
          t.pooledCAS(false, true);
        }
      } catch (TException e) {
        throw new RuntimeException(e);
      }
    }
  });
  conn.close(); // release connection
  // request a connection to another host (so it won't get re-used), which should be blocked - the first connection
  // was released, but it is effectively still open until it is closed automatically by the timeout!
  TestConnection<ClusterManagementService.Iface> conn2 = (TestConnection<ClusterManagementService.Iface>) pool
      .reserveConnection(ClusterManagementService.Iface.class, ADDR2, null);
  long afterNanos = System.nanoTime();
  conn2.close();
  // ensure that the "close" method on the first connection was called
  Mockito.verify(conn.getTransport()).close();
  Assert.assertTrue(afterNanos - beforeNanos >= 1_000_000_000L,
      "Expected to have blocked at least the keep-alive time until it was found that the first conn is dead and a "
          + "new conn was opened, but waited only " + (afterNanos - beforeNanos) + " nanos");
  Assert.assertNotEquals(conn2.getId(), connId, "Expected that connection was not re-used, as "
      + "first connection should have been closed before the second was opened");
  Assert.assertEquals(conn2.getServiceInfo().getServiceInterface(), ClusterManagementService.Iface.class,
      "Correct service expected");
}