Search in sources :

Example 6 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter 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)

Example 7 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestHttp11Basic method testFileDownloadWithChunking.

@Test
public void testFileDownloadWithChunking() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    HttpResponse resp = Requests.createResponse();
    resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    Http2Response headers = Http11ToHttp2.responseToHeaders(resp);
    XFuture<StreamWriter> future = in1.stream.process(headers);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    DataFrame dataFrame = new DataFrame();
    dataFrame.setEndOfStream(true);
    String bodyStr = "hi here and there";
    DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    dataFrame.setData(data);
    writer.processPiece(dataFrame);
    List<HttpPayload> frames = mockChannel.getFramesAndClear();
    Assert.assertEquals(2, frames.size());
    HttpData chunk = (HttpData) frames.get(0);
    DataWrapper body = chunk.getBodyNonNull();
    String result = body.createStringFromUtf8(0, body.getReadableSize());
    Assert.assertEquals(bodyStr, result);
    HttpLastData last = (HttpLastData) frames.get(1);
    Assert.assertEquals(0, last.getBodyNonNull().getReadableSize());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) HttpLastData(org.webpieces.httpparser.api.dto.HttpLastData) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 8 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestHttp11Basic method testSendTwoRequestsStreamFirstResponse.

@Test
public void testSendTwoRequestsStreamFirstResponse() throws InterruptedException, ExecutionException, TimeoutException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    mockChannel.sendToSvr(req);
    PassedIn in1 = mockListener.getSingleRequest();
    XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
    Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
    XFuture<StreamWriter> future = in1.stream.process(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Assert.assertFalse(fut1.isDone());
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, true);
    DataFrame data = (DataFrame) Http11ToHttp2.translateData(data1);
    writer.processPiece(data);
    fut1.get(2, TimeUnit.SECONDS);
    HttpData d = (HttpData) mockChannel.getFrameAndClear();
    Assert.assertEquals(10, d.getBody().getReadableSize());
    PassedIn in2 = mockListener.getSingleRequest();
    HttpResponse resp2 = Requests.createResponse(2);
    resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers2 = Http11ToHttp2.responseToHeaders(resp2);
    in2.stream.process(headers2);
    HttpPayload payload2 = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, payload2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) DataWrapper(org.webpieces.data.api.DataWrapper) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) Test(org.junit.Test)

Example 9 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestHttp11Backpressure method testInitializeThenPostWithChunkingBackpressure.

@Test
public void testInitializeThenPostWithChunkingBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
    // send first request to get past protocoal detection(ie. initialize) and send response
    initialize();
    List<ByteBuffer> buffers = create4SplitPayloads();
    XFuture<Void> ackBytePayload1 = mockChannel.sendToSvr(buffers.get(0));
    // not enough data.  parser consumes and acks future for more data(no client ack needed right now)
    Assert.assertTrue(ackBytePayload1.isDone());
    XFuture<StreamWriter> ackRequest = new XFuture<StreamWriter>();
    mockListener.addMockStreamToReturn(new MockStreamRef(ackRequest));
    XFuture<Void> ackBytePayload2 = mockChannel.sendToSvr(buffers.get(1));
    Assert.assertFalse(ackBytePayload2.isDone());
    // This releases the response msg acking 10 bytes
    ackRequest.complete(mockStreamWriter);
    Assert.assertTrue(ackBytePayload2.isDone());
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockStreamWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> ackBytePayload3 = mockChannel.sendToSvr(buffers.get(2));
    Assert.assertFalse(ackBytePayload3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    Assert.assertTrue(ackBytePayload3.isDone());
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockStreamWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> ackBytePayload4 = mockChannel.sendToSvr(buffers.get(3));
    Assert.assertFalse(ackBytePayload4.isDone());
    lastChunkAck.complete(null);
    Assert.assertTrue(ackBytePayload4.isDone());
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ByteBuffer(java.nio.ByteBuffer) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Example 10 with StreamWriter

use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.

the class TestHttp11Backpressure method testPostWithChunkingBackpressure.

@Test
public void testPostWithChunkingBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
    List<ByteBuffer> buffers = create4SplitPayloads();
    XFuture<Void> ackBytePayload1 = mockChannel.sendToSvr(buffers.get(0));
    ackBytePayload1.get(2, TimeUnit.SECONDS);
    XFuture<StreamWriter> ackRequest = new XFuture<StreamWriter>();
    mockListener.addMockStreamToReturn(new MockStreamRef(ackRequest));
    XFuture<Void> ackBytePayload2 = mockChannel.sendToSvr(buffers.get(1));
    // have to ack TWO...the stream writer AND the first HttpData fed in
    Assert.assertFalse(ackBytePayload2.isDone());
    XFuture<Void> firstChunkAck1 = new XFuture<Void>();
    mockStreamWriter.addProcessResponse(firstChunkAck1);
    ackRequest.complete(mockStreamWriter);
    Assert.assertFalse(ackBytePayload2.isDone());
    firstChunkAck1.complete(null);
    Assert.assertTrue(ackBytePayload2.isDone());
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockStreamWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> ackBytePayload3 = mockChannel.sendToSvr(buffers.get(2));
    Assert.assertFalse(ackBytePayload3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    Assert.assertTrue(ackBytePayload3.isDone());
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockStreamWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> ackBytePayload4 = mockChannel.sendToSvr(buffers.get(3));
    Assert.assertFalse(ackBytePayload4.isDone());
    lastChunkAck.complete(null);
    ackBytePayload4.get(2, TimeUnit.SECONDS);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ByteBuffer(java.nio.ByteBuffer) MockStreamRef(org.webpieces.httpfrontend2.api.mock2.MockStreamRef) Test(org.junit.Test)

Aggregations

StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)60 Test (org.junit.Test)43 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)37 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)32 StreamRef (com.webpieces.http2.api.streaming.StreamRef)25 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)24 DataWrapper (org.webpieces.data.api.DataWrapper)19 XFuture (org.webpieces.util.futures.XFuture)17 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)15 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)13 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 MockStreamRef (org.webpieces.httpfrontend2.api.mock2.MockStreamRef)8 Header (org.webpieces.httpparser.api.common.Header)8 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)8 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)7 ByteBuffer (java.nio.ByteBuffer)7 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)7 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)7