public void testScheduleBroadcast() {
logger.info("{}: running test: testScheduleBroadcast", getClass().getSimpleName());
final CountDownLatch latch = new CountDownLatch(1);
long t1 = System.currentTimeMillis();
AsyncHttpClient c = new AsyncHttpClient();
try {
final AtomicReference<Response> response = new AtomicReference<Response>();
c.prepareGet(urlTarget + "/foreverWithoutComments").setHeader(X_ATMOSPHERE_TRANSPORT, STREAMING_TRANSPORT).execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response r) throws Exception {
try {
response.set(r);
return r;
} finally {
latch.countDown();
}
}
});
// Let Atmosphere suspend the connections.
Thread.sleep(2500);
c.preparePost(urlTarget + "/scheduleAndResume").addParameter("message", "foo").execute().get();
try {
latch.await(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail(e.getMessage());
}
Response r = response.get();
assertNotNull(r);
assertEquals(r.getStatusCode(), 200);
assertEquals(r.getResponseBody().trim(), "foo");
long current = System.currentTimeMillis() - t1;
assertTrue(current > 5000 && current < 10000);
} catch (Exception e) {
logger.error("test failed", e);
fail(e.getMessage());
}
c.close();
}