Search in sources :

Example 6 with RequestStreamHandle

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

the class TestHttp1Backpressure method testBasicBackpressure.

@Test
public void testBasicBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createResponse(1, 250);
    HttpData response2 = Requests.createHttpData(250);
    List<ByteBuffer> buffers = create3BuffersWithTwoMessags(response1, response2);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since data is cached in parser (client doesn't receive this data)
    Assert.assertTrue(fut1.isDone());
    // This next one is confusing BUT in http1.1 parsing terms, data is data for content length so this results
    // in a full HttpData packet actually...
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter mockWriter = new MockStreamWriter();
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases BOTH packets above to be processed!!!(not just the one)
    requestFuture.complete(mockWriter);
    Assert.assertFalse(fut2.isDone());
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the last buffer in
    XFuture<Void> future3 = new XFuture<Void>();
    mockWriter.addProcessResponse(future3);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    future3.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) HttpData(org.webpieces.httpparser.api.dto.HttpData) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) Test(org.junit.Test)

Example 7 with RequestStreamHandle

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

the class TestWriteReads method testBasicReadWrite.

@Test
public void testBasicReadWrite() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    MockResponseListener listener2 = new MockResponseListener();
    request.getHeaderLookupStruct().getHeader("serverid").setValue("2");
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    StreamRef streamRef1 = handle.process(request, listener2);
    XFuture<StreamWriter> writer2 = streamRef1.getWriter();
    Assert.assertTrue(writer2.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createResponse(1, 0);
    listener.addProcessResponse(XFuture.completedFuture(null));
    XFuture<Void> fut1 = mockChannel.writeResponse(response1);
    // throws if exception occurred and ensures future resolved
    fut1.get(2, TimeUnit.SECONDS);
    Http2Response msg = listener.getIncomingMsg();
    Assert.assertEquals(response1, msg);
    Http2Response response2 = Requests.createResponse(2, 0);
    listener2.addProcessResponse(XFuture.completedFuture(null));
    XFuture<Void> fut2 = mockChannel.writeResponse(response2);
    // throws if exception occurred and ensures future resolved
    fut2.get(2, TimeUnit.SECONDS);
    Http2Response msg2 = listener2.getIncomingMsg();
    Assert.assertEquals(response2, msg2);
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Example 8 with RequestStreamHandle

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

the class TestHttp1Backpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createChunkedResponse(1);
    HttpChunk response2 = Requests.createHttpChunk(250);
    HttpLastChunk lastChunk = new HttpLastChunk();
    List<ByteBuffer> buffers = create4BuffersWith3Messags(response1, response2, lastChunk);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since it never made it to the client.
    Assert.assertTrue(fut1.isDone());
    // this keeps bytes in-memory that is less than bufPool.getSuggestedSize. ie. < 5k
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    MockStreamWriter mockWriter = new MockStreamWriter();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases the response BUT 'some' data was with response so can't ack yet
    requestFuture.complete(mockWriter);
    // not resolved yet since requestFuture AND streamWriterFutuer BOTH need to be resolved
    Assert.assertFalse(fut2.isDone());
    // This is BECAUSE 68 bytes resulted in TWO messages and both futures can ack the 68 bytes.
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
    Assert.assertFalse(fut4.isDone());
    lastChunkAck.complete(null);
    fut4.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) Test(org.junit.Test)

Example 9 with RequestStreamHandle

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

the class TestBackpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = httpSocket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getFrameAndClear());
    List<ByteBuffer> buffers = create4BuffersWith3Messags();
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // consume since not enough data for client
    Assert.assertTrue(fut1.isDone());
    XFuture<StreamWriter> future = new XFuture<StreamWriter>();
    listener.addReturnValueIncomingResponse(future);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter mockWriter = new MockStreamWriter();
    // This releases the response msg acking 10 bytes
    future.complete(mockWriter);
    fut2.get(2, TimeUnit.SECONDS);
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
    Assert.assertFalse(fut4.isDone());
    lastChunkAck.complete(null);
    fut4.get(2, TimeUnit.SECONDS);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Test(org.junit.Test) AbstractTest(org.webpieces.http2client.AbstractTest)

Example 10 with RequestStreamHandle

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

the class Level8NotifySvrListeners method fireRequestToApp.

public XFuture<Void> fireRequestToApp(ServerStream stream, Http2Request payload) {
    SvrSideResponseHandler handler = new SvrSideResponseHandler(level1ServerEngine, stream, pushIdGenerator);
    RequestStreamHandle streamHandle = listener.openStream();
    StreamRef streamRef = streamHandle.process(payload, handler);
    stream.setStreamHandle(streamHandle, streamRef);
    return streamRef.getWriter().thenApply(w -> null);
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef)

Aggregations

RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 StreamRef (com.webpieces.http2.api.streaming.StreamRef)11 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)10 Test (org.junit.Test)9 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)7 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)6 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)4 XFuture (org.webpieces.util.futures.XFuture)4 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 DataListener (org.webpieces.nio.api.handlers.DataListener)3 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)2 DataWrapper (org.webpieces.data.api.DataWrapper)2 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)2 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)2 MockStreamWriter (org.webpieces.httpclient.api.mocks.MockStreamWriter)2 Http2Trailers (com.webpieces.http2.api.dto.highlevel.Http2Trailers)1 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1