Search in sources :

Example 1 with MockChannel

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());
}
Also used : StreamMsg(com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg) MockChannel(org.webpieces.webserver.test.sockets.MockChannel) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) ByteBuffer(java.nio.ByteBuffer) DataWrapper(org.webpieces.data.api.DataWrapper) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) DataListener(org.webpieces.nio.api.handlers.DataListener) AbstractWebpiecesTest(org.webpieces.webserver.test.sockets.AbstractWebpiecesTest) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Aggregations

DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 StreamMsg (com.webpieces.http2.api.dto.lowlevel.lib.StreamMsg)1 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)1 ByteBuffer (java.nio.ByteBuffer)1 Test (org.junit.Test)1 DataWrapper (org.webpieces.data.api.DataWrapper)1 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)1 DataListener (org.webpieces.nio.api.handlers.DataListener)1 XFuture (org.webpieces.util.futures.XFuture)1 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)1 AbstractWebpiecesTest (org.webpieces.webserver.test.sockets.AbstractWebpiecesTest)1 MockChannel (org.webpieces.webserver.test.sockets.MockChannel)1