Search in sources :

Example 16 with ServerContext

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

the class PartitionedHttpClientTest method testClientGroupPartitioning.

@Test
void testClientGroupPartitioning() throws Exception {
    // user partition discovery service, userId=1 => srv1 | userId=2 => srv2
    try (ServerContext userDisco = HttpServers.forAddress(localAddress(0)).listenBlockingAndAwait((ctx, request, responseFactory) -> {
        if ("/partition".equals(request.path())) {
            String userIdParam = request.queryParameter("userId");
            if (userIdParam == null || userIdParam.isEmpty()) {
                return responseFactory.badRequest();
            }
            int userId = Integer.parseInt(userIdParam);
            if (userId != 1 && userId != 2) {
                return responseFactory.notFound();
            }
            ServerContext dSrv = userId == 1 ? srv1 : srv2;
            InetSocketAddress socketAddress = (InetSocketAddress) dSrv.listenAddress();
            return responseFactory.ok().payloadBody(socketAddress.getPort() + "", textSerializerUtf8());
        }
        return responseFactory.notFound();
    })) {
        try (PartitioningHttpClientWithOutOfBandDiscovery client = new PartitioningHttpClientWithOutOfBandDiscovery(userDisco)) {
            StreamingHttpResponse httpResponse1 = client.request(new User(1), client.get("/foo")).toFuture().get();
            StreamingHttpResponse httpResponse2 = client.request(new User(2), client.get("/foo")).toFuture().get();
            MatcherAssert.assertThat(httpResponse1.status(), is(OK));
            MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_1));
            MatcherAssert.assertThat(httpResponse2.status(), is(OK));
            MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_2));
        }
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) InetSocketAddress(java.net.InetSocketAddress) Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 17 with ServerContext

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

the class MalformedDataAfterHttpMessageTest method afterRequest.

@Test
void afterRequest() throws Exception {
    try (ServerContext server = stServer();
        BlockingHttpClient client = stClient(server.listenAddress())) {
        Buffer malformedBody = client.executionContext().bufferAllocator().fromAscii(CONTENT).writeShort(// malformed data at the end of the request msg
        0);
        HttpRequest request = client.post("/").setHeader(CONTENT_LENGTH, valueOf(CONTENT.length())).setHeader(CONTENT_TYPE, TEXT_PLAIN).payloadBody(malformedBody);
        ReservedBlockingHttpConnection connection = client.reserveConnection(request);
        CountDownLatch connectionClosedLatch = new CountDownLatch(1);
        connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
        assertThrows(IOException.class, () -> connection.request(request));
        // Server should close the connection:
        connectionClosedLatch.await();
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpRequest(io.servicetalk.http.api.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) ReservedBlockingHttpConnection(io.servicetalk.http.api.ReservedBlockingHttpConnection) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with ServerContext

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

the class NettyHttpServerConnectionDrainTest method requestIsAutoDrainedWhenUserFailsToConsume.

@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void requestIsAutoDrainedWhenUserFailsToConsume() throws Exception {
    BlockingHttpClient client = null;
    try (ServerContext serverContext = server(true, respondOkWithoutReadingRequest())) {
        client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildBlocking();
        postLargePayloadAndAssertResponseOk(client);
    } finally {
        closeClient(client);
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 19 with ServerContext

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

the class NettyHttpServerConnectionDrainTest method requestTimesOutWithoutAutoDrainingOrUserConsuming.

@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void requestTimesOutWithoutAutoDrainingOrUserConsuming() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    StreamingHttpClient client = null;
    try (ServerContext serverContext = server(false, respondOkWithoutReadingRequest(latch::countDown))) {
        client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).buildStreaming();
        client.request(client.post("/").payloadBody(from(LARGE_TEXT), appSerializerUtf8FixLen())).ignoreElement().subscribe();
        // Wait till the request is received
        assertThrows(TimeoutException.class, latch::await);
    // before initiating graceful close of the server
    } finally {
        closeClient(client);
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) ServerContext(io.servicetalk.transport.api.ServerContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 20 with ServerContext

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

the class AbstractHttpServiceAsyncContextTest method contextPreservedOverFilterBoundaries.

final void contextPreservedOverFilterBoundaries(boolean useImmediate, boolean asyncFilter, boolean asyncService) throws Exception {
    Queue<Throwable> errorQueue = new ConcurrentLinkedQueue<>();
    try (ServerContext ctx = serverWithService(HttpServers.forAddress(localAddress(0)).appendServiceFilter(filterFactory(useImmediate, asyncFilter, errorQueue)), useImmediate, asyncService);
        StreamingHttpClient client = HttpClients.forResolvedAddress(serverHostAndPort(ctx)).buildStreaming();
        StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
        makeClientRequestWithId(connection, "1");
        assertThat("Error queue is not empty!", errorQueue, empty());
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) ServerContext(io.servicetalk.transport.api.ServerContext) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection)

Aggregations

ServerContext (io.servicetalk.transport.api.ServerContext)87 Test (org.junit.jupiter.api.Test)40 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)32 HttpResponse (io.servicetalk.http.api.HttpResponse)29 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)28 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)27 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)24 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)23 MethodSource (org.junit.jupiter.params.provider.MethodSource)20 Single (io.servicetalk.concurrent.api.Single)18 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)17 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)17 InetSocketAddress (java.net.InetSocketAddress)17 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)14 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)13 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)13 StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)12 Matchers.is (org.hamcrest.Matchers.is)12