// TODO Netty only.
@Test(groups = { "standalone", "default_provider" })
public void testStream() throws IOException {
AsyncHttpClient ahc = getAsyncHttpClient(null);
try {
final AtomicBoolean err = new AtomicBoolean(false);
final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
final AtomicBoolean status = new AtomicBoolean(false);
final AtomicInteger headers = new AtomicInteger(0);
final CountDownLatch latch = new CountDownLatch(1);
ahc.executeRequest(ahc.prepareGet(getTargetUrl()).build(), new AsyncHandler<Object>() {
public void onThrowable(Throwable t) {
fail("Got throwable.", t);
err.set(true);
}
public STATE onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
if (e.length() != 0) {
String s = new String(e.getBodyPartBytes());
logger.info("got part: {}", s);
queue.put(s);
}
return STATE.CONTINUE;
}
public STATE onStatusReceived(HttpResponseStatus e) throws Exception {
status.set(true);
return STATE.CONTINUE;
}
public STATE onHeadersReceived(HttpResponseHeaders e) throws Exception {
if (headers.incrementAndGet() == 2) {
throw new Exception("Analyze this.");
}
return STATE.CONTINUE;
}
public Object onCompleted() throws Exception {
latch.countDown();
return null;
}
});
try {
assertTrue(latch.await(1, TimeUnit.SECONDS), "Latch failed.");
} catch (InterruptedException e) {
fail("Interrupted.", e);
}
assertFalse(err.get());
assertEquals(queue.size(), 2);
assertTrue(queue.contains("part1"));
assertTrue(queue.contains("part2"));
assertTrue(status.get());
assertEquals(headers.get(), 1);
} finally {
ahc.close();
}
}