final AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setConnectTimeout(1000).setRequestTimeout(5000).setAllowPoolingConnections(true)//
.setMaxConnections(1).setMaxConnectionsPerHost(1).build());
final CountDownLatch inThreadsLatch = new CountDownLatch(2);
final AtomicReference<Integer> failedRank = new AtomicReference(-1);
try {
for (int i = 0; i < urls.length; i++) {
final String url = urls[i];
final int rank = i;
Thread t = new Thread() {
public void run() {
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
inThreadsLatch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
failedRank.set(rank);
inThreadsLatch.countDown();
}
});
}
};
t.start();
}
inThreadsLatch.await();
assertEquals(failedRank.get().intValue(), 1, "Max Connections should have been reached");
final CountDownLatch notInThreadsLatch = new CountDownLatch(2);
failedRank.set(-1);
for (int i = 0; i < urls.length; i++) {
final String url = urls[i];
final int rank = i;
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
notInThreadsLatch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
failedRank.set(rank);
notInThreadsLatch.countDown();
}
});
}
notInThreadsLatch.await();
assertEquals(failedRank.get().intValue(), 1, "Max Connections should have been reached");
} finally {
client.close();
}
}