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;