Search in sources :

Example 81 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class NettyHttpServerConnectionDrainTest method server.

private static ServerContext server(boolean autoDrain, StreamingHttpService handler) throws Exception {
    HttpServerBuilder httpServerBuilder = HttpServers.forAddress(AddressUtils.localAddress(0));
    if (!autoDrain) {
        httpServerBuilder = httpServerBuilder.drainRequestPayloadBody(false);
    }
    ServerContext serverContext = httpServerBuilder.listenStreamingAndAwait(handler);
    return new ServerContext() {

        @Override
        public SocketAddress listenAddress() {
            return serverContext.listenAddress();
        }

        @Override
        public ExecutionContext<?> executionContext() {
            return serverContext.executionContext();
        }

        @Override
        public void acceptConnections(final boolean accept) {
            serverContext.acceptConnections(accept);
        }

        @Override
        public Completable onClose() {
            return serverContext.onClose();
        }

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

        @Override
        public void close() {
            // Without draining the request is expected to hang, don't wait too long unless on CI
            int timeoutSeconds = CI ? 15 : 1;
            awaitTermination(serverContext.closeAsyncGracefully().timeout(timeoutSeconds, SECONDS).onErrorResume(t -> serverContext.closeAsync().concat(Completable.failed(t))).toFuture());
        }
    };
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder)

Example 82 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class NettyHttpServerConnectionDrainTest method requestIsConsumedByUserWithDrainingEnabled.

@Test
void requestIsConsumedByUserWithDrainingEnabled() throws Exception {
    AtomicReference<String> resultRef = new AtomicReference<>();
    try (ServerContext serverContext = server(true, (ctx, request, responseFactory) -> request.payloadBody().collect(() -> new StringBuilder(LARGE_TEXT.length()), (sb, b) -> sb.append(b.toString(CharsetUtil.US_ASCII))).map(StringBuilder::toString).whenOnSuccess(resultRef::set).toCompletable().concat(succeeded(responseFactory.ok().payloadBody(from("OK"), appSerializerUtf8FixLen()))));
        BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking()) {
        postLargePayloadAndAssertResponseOk(client);
        assertThat(resultRef.get(), equalTo(LARGE_TEXT));
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) SocketAddress(java.net.SocketAddress) TimeoutException(java.util.concurrent.TimeoutException) CI(io.servicetalk.concurrent.internal.TestTimeoutConstants.CI) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpSerializers.appSerializerUtf8FixLen(io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) CharsetUtil(io.netty.util.CharsetUtil) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) Nullable(javax.annotation.Nullable) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ServerContext(io.servicetalk.transport.api.ServerContext) FutureUtils.awaitTermination(io.servicetalk.concurrent.internal.FutureUtils.awaitTermination) Completable(io.servicetalk.concurrent.api.Completable) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) ExecutionContext(io.servicetalk.transport.api.ExecutionContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) AddressUtils(io.servicetalk.transport.netty.internal.AddressUtils) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 83 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class HttpProvidersTest method testHttpServerBuilderProvider.

@Test
void testHttpServerBuilderProvider() throws Exception {
    final InetSocketAddress serverAddress = localAddress(0);
    TestHttpServerBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
    try (ServerContext serverContext = HttpServers.forAddress(serverAddress).listenStreamingAndAwait(new TestServiceStreaming())) {
        assertThat(TestHttpServerBuilderProvider.BUILD_COUNTER.get(), is(1));
        try (BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking()) {
            HttpResponse response = client.request(client.get(SVC_ECHO));
            assertThat(response.status(), is(OK));
            assertThat(TestHttpServerBuilderProvider.CONNECTION_COUNTER.get(), is(1));
        }
    }
}
Also used : HttpServerContext(io.servicetalk.http.api.HttpServerContext) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 84 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class HttpProvidersTest method testNoProvidersForAddress.

@Test
void testNoProvidersForAddress() throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming());
        BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking()) {
        HttpResponse response = client.request(client.get(SVC_ECHO));
        assertThat(response.status(), is(OK));
    }
    assertThat(TestHttpServerBuilderProvider.BUILD_COUNTER.get(), is(0));
    assertThat(TestHttpServerBuilderProvider.CONNECTION_COUNTER.get(), is(0));
    assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(0));
    assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(0));
}
Also used : HttpServerContext(io.servicetalk.http.api.HttpServerContext) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 85 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class HttpProvidersTest method testMultiAddressHttpClientBuilderProvider.

@Test
void testMultiAddressHttpClientBuilderProvider() throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
        HostAndPort serverAddress = serverHostAndPort(serverContext);
        TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
        try (BlockingHttpClient client = HttpClients.forMultiAddressUrl().buildBlocking()) {
            assertThat(TestMultiAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
            HttpResponse response = client.request(client.get("http://" + serverAddress + SVC_ECHO));
            assertThat(response.status(), is(OK));
            assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
            assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
        }
    }
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpServerContext(io.servicetalk.http.api.HttpServerContext) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ServerContext (io.servicetalk.transport.api.ServerContext)106 Test (org.junit.jupiter.api.Test)57 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)42 HttpResponse (io.servicetalk.http.api.HttpResponse)39 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)34 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)33 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)28 InetSocketAddress (java.net.InetSocketAddress)27 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)26 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)22 HostAndPort (io.servicetalk.transport.api.HostAndPort)20 Single (io.servicetalk.concurrent.api.Single)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)19 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)16 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)15 OK (io.servicetalk.http.api.HttpResponseStatus.OK)14 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14