use of org.webpieces.webserver.test.sockets.MockChannel in project webpieces by deanhiller.
the class TestStreamingRaw method testAsyncJsonGet.
@Test
public void testAsyncJsonGet() throws InterruptedException, ExecutionException {
MockChannel channel = new MockChannel();
XFuture<DataListener> future = mgr.simulateHttpConnect(channel);
DataListener dataListener = future.get();
HttpFullRequest fullRequest = Requests.createJsonRequest(KnownHttpMethod.GET, "/json/streaming");
ByteBuffer buffer = parser.marshalToByteBuffer(fullRequest.getRequest());
DataWrapper data = fullRequest.getData();
byte[] headers = new byte[buffer.remaining()];
buffer.get(headers);
data.getReadableSize();
byte[] part1 = data.readBytesAt(0, 10);
String str = new String(part1);
byte[] part2 = data.readBytesAt(10, data.getReadableSize() - 10);
String str2 = new String(part2);
ByteBuffer firstPacket = ByteBuffer.allocate(headers.length + part1.length);
firstPacket.put(headers);
firstPacket.put(part1);
firstPacket.flip();
XFuture<Boolean> authFuture = new XFuture<Boolean>();
mockAuth.addValueToReturn(authFuture);
Assert.assertEquals(0, Context.getContext().size());
// Feed in request with content-length AND part of the body as well...
XFuture<Void> fut1 = dataListener.incomingData(channel, firstPacket);
Assert.assertEquals(0, Context.getContext().size());
// Feed in more BEFORE authFuture is complete(this was the bug, ie. race condition)
ByteBuffer buf2 = ByteBuffer.allocate(part2.length);
buf2.put(part2);
buf2.flip();
XFuture<Void> fut2 = dataListener.incomingData(channel, buf2);
Assert.assertEquals(0, Context.getContext().size());
XFuture<StreamWriter> streamWriterFuture = new XFuture<StreamWriter>();
mockStreamClient.addStreamWriter(streamWriterFuture);
// complete it
authFuture.complete(true);
Assert.assertFalse(fut1.isDone());
Assert.assertFalse(fut2.isDone());
MockStreamWriter2 mockStreamWriter = new MockStreamWriter2();
streamWriterFuture.complete(mockStreamWriter);
Assert.assertEquals(0, Context.getContext().size());
Assert.assertTrue(fut1.isDone());
Assert.assertTrue(fut2.isDone());
List<StreamMsg> frames = mockStreamWriter.getFrames();
Assert.assertEquals(2, frames.size());
DataFrame f1 = (DataFrame) frames.get(0);
DataFrame f2 = (DataFrame) frames.get(1);
String s1 = f1.getData().createStringFromUtf8(0, f1.getData().getReadableSize());
Assert.assertEquals(str, s1);
String s2 = f2.getData().createStringFromUtf8(0, f2.getData().getReadableSize());
Assert.assertEquals(str2, s2);
Assert.assertEquals(0, Context.getContext().size());
}
Aggregations