HttpParams params = createDefaultParams();
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1));
ConnManagerParams.setMaxTotalConnections(params, 3);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
HttpHost target1 = new HttpHost("www.test1.invalid", 80, "http");
HttpRoute route1 = new HttpRoute(target1, null, false);
HttpHost target2 = new HttpHost("www.test2.invalid", 80, "http");
HttpRoute route2 = new HttpRoute(target2, null, false);
HttpHost target3 = new HttpHost("www.test3.invalid", 80, "http");
HttpRoute route3 = new HttpRoute(target3, null, false);
// the first three allocations should pass
ManagedClientConnection conn1 =
getConnection(mgr, route1, 10L, TimeUnit.MILLISECONDS);
ManagedClientConnection conn2 =
getConnection(mgr, route2, 10L, TimeUnit.MILLISECONDS);
ManagedClientConnection conn3 =
getConnection(mgr, route3, 10L, TimeUnit.MILLISECONDS);
assertNotNull(conn1);
assertNotNull(conn2);
assertNotNull(conn3);
// obtaining another connection for either of the three should fail
// this is somehow redundant with testMaxConnPerHost
try {
getConnection(mgr, route1, 10L, TimeUnit.MILLISECONDS);
fail("ConnectionPoolTimeoutException should have been thrown");
} catch (ConnectionPoolTimeoutException e) {
// expected
}
try {
getConnection(mgr, route2, 10L, TimeUnit.MILLISECONDS);
fail("ConnectionPoolTimeoutException should have been thrown");
} catch (ConnectionPoolTimeoutException e) {
// expected
}
try {
getConnection(mgr, route3, 10L, TimeUnit.MILLISECONDS);
fail("ConnectionPoolTimeoutException should have been thrown");
} catch (ConnectionPoolTimeoutException e) {
// expected
}
// now release one and check that exactly that one can be obtained then
mgr.releaseConnection(conn2, -1, null);
conn2 = null;
try {
getConnection(mgr, route1, 10L, TimeUnit.MILLISECONDS);
fail("ConnectionPoolTimeoutException should have been thrown");
} catch (ConnectionPoolTimeoutException e) {
// expected
}
// this one succeeds
conn2 = getConnection(mgr, route2, 10L, TimeUnit.MILLISECONDS);
assertNotNull(conn2);
try {
getConnection(mgr, route3, 10L, TimeUnit.MILLISECONDS);
fail("ConnectionPoolTimeoutException should have been thrown");
} catch (ConnectionPoolTimeoutException e) {
// expected
}
mgr.shutdown();
}