Assert.assertNotNull(operatorRef.get());
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
final ManagedClientConnection conn = getConnection(mgr, route);
Assert.assertTrue(conn instanceof AbstractClientConnAdapter);
final AtomicReference<Throwable> throwRef = new AtomicReference<Throwable>();
Thread abortingThread = new Thread(new Runnable() {
public void run() {
try {
operatorRef.get().waitForState();
conn.abortConnection();
connectLatch.countDown();
} catch (Throwable e) {
throwRef.set(e);
}
}
});
abortingThread.start();
try {
conn.open(route, httpContext, defaultParams);
Assert.fail("expected exception");
} catch(IOException iox) {
Assert.assertEquals("Request aborted", iox.getMessage());
}
abortingThread.join(5000);
if(throwRef.get() != null)
throw new RuntimeException(throwRef.get());
Assert.assertFalse(conn.isOpen());
// Give the server a bit of time to accept the connection, but
// ensure that it can accept it.
for(int i = 0; i < 10; i++) {
if(localServer.getAcceptedConnectionCount() == 1)
break;
Thread.sleep(100);
}
Assert.assertEquals(1, localServer.getAcceptedConnectionCount());
// the connection is expected to be released back to the manager
ManagedClientConnection conn2 = getConnection(mgr, route, 5L, TimeUnit.SECONDS);
Assert.assertFalse("connection should have been closed", conn2.isOpen());
mgr.releaseConnection(conn2, -1, null);
mgr.shutdown();
}