System.out.println("testStreamingCompletionHandler");
WebContainer container = new WebContainer(new MyServlet());
container.start();
HttpClientConnection con = new HttpClientConnection("localhost", container.getLocalPort());
for (int i = 0; i < 100; i++) {
final FutureResponseHandler respHdl = new FutureResponseHandler();
final BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
dataSink.setFlushmode(FlushMode.ASYNC);
IWriteCompletionHandler completionHandler1 = new IWriteCompletionHandler() {
public void onWritten(int written) throws IOException {
try {
IHttpResponse response = respHdl.getResponse();
Assert.assertEquals(200, response.getStatus());
final BodyDataSource dataSource = response.getBody();
Assert.assertEquals("test", dataSource.readStringByLength(4));
IWriteCompletionHandler completionHandler2 = new IWriteCompletionHandler() {
public void onWritten(int written) throws IOException {
Assert.assertEquals("12345", dataSource.readStringByLength(5));
IWriteCompletionHandler completionHandler3 = new IWriteCompletionHandler() {
public void onWritten(int written) throws IOException {
Assert.assertEquals("789", dataSource.readStringByLength(3));
dataSink.close();
}
public void onException(IOException ioe) {
ioe.printStackTrace();
}
};
// writing 3. junk and close
dataSink.write(new ByteBuffer[] { DataConverter.toByteBuffer("789", "US-ASCII") }, completionHandler3);
}
public void onException(IOException ioe) {
ioe.printStackTrace();
}
};
// writing 2. junk
dataSink.write(new ByteBuffer[] { DataConverter.toByteBuffer("12345", "US-ASCII") }, completionHandler2);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onException(IOException ioe) {
ioe.printStackTrace();
}
};
// writing 1. junk
dataSink.write(new ByteBuffer[] { DataConverter.toByteBuffer("test", "US-ASCII") }, completionHandler1);
IBodyCloseListener closeListener = new IBodyCloseListener() {
public void onClose() throws IOException {
synchronized (dataSink) {
dataSink.notify();
}
}
};
dataSink.addCloseListener(closeListener);
try {
synchronized (dataSink) {
if (dataSink.isOpen()) {
dataSink.wait();
}
}
} catch (InterruptedException ignore) { }
System.out.print(".");
}
System.out.print("finished");
con.close();
container.stop();
}