@Test
public void testHeaderBroadcasterCache() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
logger.info("{}: running test: testHeaderBroadcasterCache", getClass().getSimpleName());
atmoServlet.setBroadcasterCacheClassName(HeaderBroadcasterCache.class.getName());
final CountDownLatch latch = new CountDownLatch(1);
long t1 = System.currentTimeMillis();
atmoServlet.addAtmosphereHandler(ROOT, new AbstractHttpAtmosphereHandler() {
public void onRequest(AtmosphereResource<HttpServletRequest, HttpServletResponse> event) throws IOException {
try {
if (event.getRequest().getHeader(HeaderBroadcasterCache.HEADER_CACHE) != null) {
event.suspend(-1, false);
return;
}
event.getBroadcaster().broadcast("12345678910").get();
} catch (InterruptedException e) {
logger.error("", e);
} catch (ExecutionException e) {
logger.error("", e);
}
event.getResponse().flushBuffer();
}
public void onStateChange(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) throws IOException {
if (event.isResuming()) {
return;
}
try {
assertFalse(event.isCancelled());
assertNotNull(event.getMessage());
if (List.class.isAssignableFrom(event.getMessage().getClass())) {
for (String m : (List<String>) event.getMessage()) {
event.getResource().getResponse().getOutputStream().write(m.getBytes());
}
}
event.getResource().getResponse().flushBuffer();
event.getResource().resume();
} finally {
latch.countDown();
}
}
}, new RecyclableBroadcaster("suspend"));
AsyncHttpClient c = new AsyncHttpClient();
try {
c.prepareGet(urlTarget).execute().get();
c.prepareGet(urlTarget).execute().get();
//Suspend
Response r = c.prepareGet(urlTarget).addHeader(HeaderBroadcasterCache.HEADER_CACHE, String.valueOf(t1)).execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response r) throws Exception {
try {
return r;
} finally {
latch.countDown();
}
}
}).get();
try {
latch.await(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail(e.getMessage());
}
assertNotNull(r);