Search in sources :

Example 1 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class GrpcRouter method bind.

Single<GrpcServerContext> bind(final ServerBinder binder, final GrpcExecutionContext executionContext) {
    final CompositeCloseable closeable = AsyncCloseables.newCompositeCloseable();
    final Map<String, StreamingHttpService> allRoutes = new HashMap<>();
    populateRoutes(executionContext, allRoutes, routes, closeable, executionStrategies);
    populateRoutes(executionContext, allRoutes, streamingRoutes, closeable, executionStrategies);
    populateRoutes(executionContext, allRoutes, blockingRoutes, closeable, executionStrategies);
    populateRoutes(executionContext, allRoutes, blockingStreamingRoutes, closeable, executionStrategies);
    // TODO: Optimize to bind a specific programming model service based on routes
    return binder.bindStreaming(new StreamingHttpService() {

        @Override
        public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
            final StreamingHttpService service;
            if (!POST.equals(request.method()) || (service = allRoutes.get(request.path())) == null) {
                return NOT_FOUND_SERVICE.handle(ctx, request, responseFactory);
            } else {
                return service.handle(ctx, request, responseFactory);
            }
        }

        @Override
        public Completable closeAsync() {
            return closeable.closeAsync();
        }

        @Override
        public Completable closeAsyncGracefully() {
            return closeable.closeAsyncGracefully();
        }
    }).map(httpServerContext -> new DefaultGrpcServerContext(httpServerContext, executionContext));
}
Also used : HashMap(java.util.HashMap) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) BlockingStreamingHttpService(io.servicetalk.http.api.BlockingStreamingHttpService) StreamingHttpServiceToOffloadedStreamingHttpService(io.servicetalk.http.api.StreamingHttpServiceToOffloadedStreamingHttpService) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpApiConversions.toStreamingHttpService(io.servicetalk.http.api.HttpApiConversions.toStreamingHttpService) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 2 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class GracefulConnectionClosureHandlingTest method closeAfterRequestMetaDataSentResponseMetaDataReceived.

@ParameterizedTest(name = "{index}: protocol={0} initiateClosureFromClient={1} useUds={2} viaProxy={3}")
@MethodSource("data")
void closeAfterRequestMetaDataSentResponseMetaDataReceived(HttpProtocol protocol, boolean initiateClosureFromClient, boolean useUds, boolean viaProxy) throws Exception {
    setUp(protocol, initiateClosureFromClient, useUds, viaProxy);
    CountDownLatch clientSendRequestPayload = new CountDownLatch(1);
    StreamingHttpRequest request = newRequest("/first", clientSendRequestPayload);
    Future<StreamingHttpResponse> responseFuture = connection.request(request).toFuture();
    serverSendResponse.countDown();
    StreamingHttpResponse response = responseFuture.get();
    assertResponse(response);
    triggerGracefulClosure();
    clientSendRequestPayload.countDown();
    serverSendResponsePayload.countDown();
    assertRequestPayloadBody(request);
    assertResponsePayloadBody(response);
    awaitConnectionClosed();
    assertNextRequestFails();
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class GracefulConnectionClosureHandlingTest method closeAfterRequestMetaDataSentNoResponseReceived.

@ParameterizedTest(name = "{index}: protocol={0} initiateClosureFromClient={1} useUds={2} viaProxy={3}")
@MethodSource("data")
void closeAfterRequestMetaDataSentNoResponseReceived(HttpProtocol protocol, boolean initiateClosureFromClient, boolean useUds, boolean viaProxy) throws Exception {
    setUp(protocol, initiateClosureFromClient, useUds, viaProxy);
    CountDownLatch clientSendRequestPayload = new CountDownLatch(1);
    StreamingHttpRequest request = newRequest("/first", clientSendRequestPayload);
    Future<StreamingHttpResponse> responseFuture = connection.request(request).toFuture();
    serverReceivedRequest.await();
    triggerGracefulClosure();
    serverSendResponse.countDown();
    StreamingHttpResponse response = responseFuture.get();
    assertResponse(response);
    clientSendRequestPayload.countDown();
    serverSendResponsePayload.countDown();
    assertRequestPayloadBody(request);
    assertResponsePayloadBody(response);
    awaitConnectionClosed();
    assertNextRequestFails();
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class H2PriorKnowledgeFeatureParityTest method clientGracefulClose.

@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientGracefulClose(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
    setUp(strategy, h2PriorKnowledge);
    InetSocketAddress serverAddress = bindHttpEchoServer();
    StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildStreaming();
    CountDownLatch onCloseLatch = new CountDownLatch(1);
    Processor<Buffer, Buffer> requestBody = newProcessor();
    client.onClose().subscribe(onCloseLatch::countDown);
    // We want to make a request, and intentionally not complete it. While the request is in process we invoke
    // closeAsyncGracefully and verify that we wait until the request has completed before the underlying
    // transport is closed.
    StreamingHttpRequest request = client.post("/").payloadBody(fromSource(requestBody));
    StreamingHttpResponse response = client.request(request).toFuture().get();
    client.closeAsyncGracefully().subscribe();
    // We expect this to timeout, because we have not completed the outstanding request.
    assertFalse(onCloseLatch.await(300, MILLISECONDS));
    requestBody.onComplete();
    response.payloadBody().ignoreElements().toFuture();
    onCloseLatch.await();
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class H2PriorKnowledgeFeatureParityTest method continue100.

private void continue100(boolean failExpectation) throws Exception {
    InetSocketAddress serverAddress = bindHttpEchoServer();
    try (StreamingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildStreaming()) {
        Processor<Buffer, Buffer> requestBody1 = newProcessor();
        StreamingHttpRequest request = client.post("/").payloadBody(fromSource(requestBody1));
        request.addHeader(EXPECT, CONTINUE);
        if (failExpectation) {
            request.addHeader(EXPECT_FAIL_HEADER, "notused");
        }
        StreamingHttpResponse response = client.request(request).toFuture().get();
        if (failExpectation) {
            assertEquals(EXPECTATION_FAILED, response.status());
            assertFalse(response.payloadBody().toIterable().iterator().hasNext());
        } else {
            assertEquals(HttpResponseStatus.CONTINUE, response.status());
            String payloadBody = "foo";
            requestBody1.onNext(client.executionContext().bufferAllocator().fromAscii(payloadBody));
            requestBody1.onComplete();
            Iterator<Buffer> responseBody = response.payloadBody().toIterable().iterator();
            assertTrue(responseBody.hasNext());
            Buffer next = responseBody.next();
            assertNotNull(next);
            assertEquals(payloadBody, next.toString(UTF_8));
            assertFalse(responseBody.hasNext());
        }
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Matchers.emptyString(org.hamcrest.Matchers.emptyString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Aggregations

StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)167 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)83 Test (org.junit.jupiter.api.Test)78 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)65 MethodSource (org.junit.jupiter.params.provider.MethodSource)47 Single (io.servicetalk.concurrent.api.Single)34 Buffer (io.servicetalk.buffer.api.Buffer)33 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)31 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)24 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)22 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)22 Nullable (javax.annotation.Nullable)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)20 InetSocketAddress (java.net.InetSocketAddress)20 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)17 HttpHeaders (io.servicetalk.http.api.HttpHeaders)17 ServerContext (io.servicetalk.transport.api.ServerContext)16 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)16