@Test
public void multipleAsynchronousHttpClientTest() throws InterruptedException {
for (int i = 0; i < 100; i++) {
final CountDownLatch latch = new CountDownLatch(1);
final String url = "http://localhost:" + PORT + "/";
final AsynchronousHttpClient http = new AsynchronousHttpClient();
final String[] result = {"BODY_PLACEHOLDER", "STATUSCODE_PLACEHOLDER"};
final AsyncResult<org.deftserver.web.http.client.HttpResponse> cb =
new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {
public void onSuccess(org.deftserver.web.http.client.HttpResponse response) {
result[0] = response.getBody();
result[1] = response.getStatusLine();
latch.countDown();
}
public void onFailure(Throwable ignore) { }
};
// make sure that the http.fetch(..) is invoked from the ioloop thread
IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
latch.await(5, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
assertEquals("hello test", result[0]);
assertEquals("HTTP/1.1 200 OK", result[1]);
}