this.localServer.register("/random/*", new RandomHandler());
this.localServer.start();
final InetSocketAddress saddress = this.localServer.getServiceAddress();
final PoolingHttpClientConnectionManager mgr = new PoolingHttpClientConnectionManager();
mgr.setMaxTotal(1);
mgr.setDefaultMaxPerRoute(1);
final HttpClient client = HttpClients.custom().setConnectionManager(mgr).build();
final HttpHost target = new HttpHost(saddress.getHostName(), saddress.getPort(), "http");
HttpResponse response = client.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, mgr.getTotalStats().getAvailable());
Assert.assertEquals(1, localServer.getAcceptedConnectionCount());
response = client.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, mgr.getTotalStats().getAvailable());
Assert.assertEquals(1, localServer.getAcceptedConnectionCount());
// Now sleep for 1.1 seconds and let the timeout do its work
Thread.sleep(1100);
response = client.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, mgr.getTotalStats().getAvailable());
Assert.assertEquals(2, localServer.getAcceptedConnectionCount());
// Do another request just under the 1 second limit & make
// sure we reuse that connection.
Thread.sleep(500);
response = client.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, mgr.getTotalStats().getAvailable());
Assert.assertEquals(2, localServer.getAcceptedConnectionCount());
mgr.shutdown();
}