Search in sources :

Example 26 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef 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 27 with StreamRef

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

the class Level8NotifySvrListeners method sendRstToApp.

@Override
public XFuture<Void> sendRstToApp(Stream stream, CancelReason payload) {
    if (stream instanceof ServerStream) {
        ServerStream str = (ServerStream) stream;
        StreamRef streamRef = str.getStreamRef();
        return streamRef.cancel(payload);
    }
    // since the stream is closed, any writes to the push streams will automatically close and be cancelled
    return XFuture.completedFuture(null);
}
Also used : StreamRef(com.webpieces.http2.api.streaming.StreamRef)

Example 28 with StreamRef

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

Example 29 with StreamRef

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

the class ErrorCommonTest method testArgsTypeMismatch.

@Test
public void testArgsTypeMismatch() {
    log.info("starting");
    String moduleFileContents = CommonRoutesModules.class.getName();
    RouterService server = createServer(isProdTest, moduleFileContents);
    server.start();
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response response = mockStream.getLastResponse();
    String contents = mockStream.getResponseBody();
    verifyNotFoundRendered(response, contents);
    // We did not send a keep alive so it should close
    Assert.assertTrue(mockStream.isWasClosed());
}
Also used : RouterService(org.webpieces.router.api.RouterService) 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) Test(org.junit.Test)

Example 30 with StreamRef

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

the class ErrorCommonTest method testRedirectRouteNotEnoughArguments.

@Test
public void testRedirectRouteNotEnoughArguments() {
    // say method is something(int arg, String this)
    // we verify redirects MUST match type and number of method arguments every time
    // then when we form url, we put the stuff in the path OR put it as query params so it works on the way back in again too
    String moduleFileContents = CommonRoutesModules.class.getName();
    RouterService server = createServer(isProdTest, moduleFileContents);
    server.start();
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/user/5553");
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response response = mockStream.getLastResponse();
    String contents = mockStream.getResponseBody();
    Assert.assertEquals(response.getSingleHeaderValue(Http2HeaderName.STATUS), "500");
    if (isProdTest) {
        // prod template to use
        Assert.assertTrue(contents.contains("There was a bug in the developers application or webpieces server"));
    } else {
        // The internalError template for DevServer should run...
        Assert.assertTrue(contents.contains("NullTemplateApi.java is running for templatePath=org.webpieces.devrouter.impl.internalError_html"));
    }
    // We did not send a keep alive so it should close
    Assert.assertTrue(mockStream.isWasClosed());
}
Also used : RouterService(org.webpieces.router.api.RouterService) 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) Test(org.junit.Test)

Aggregations

StreamRef (com.webpieces.http2.api.streaming.StreamRef)34 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)27 Test (org.junit.Test)22 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)17 XFuture (org.webpieces.util.futures.XFuture)12 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)8 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)8 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)6 DataWrapper (org.webpieces.data.api.DataWrapper)6 RouterService (org.webpieces.router.api.RouterService)6 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)6 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)5 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)5 Function (java.util.function.Function)5 RequestContext (org.webpieces.ctx.api.RequestContext)5 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)5 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)4 ConnectionClosedException (com.webpieces.http2engine.api.error.ConnectionClosedException)4