Search in sources :

Example 31 with BlockingHttpClient

use of io.servicetalk.http.api.BlockingHttpClient 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 32 with BlockingHttpClient

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

the class NettyHttpServerTest method testServiceThrowsReturnsErrorResponse.

@ParameterizedTest(name = "{displayName} [{index}] disableOffloading={0}")
@ValueSource(booleans = { true, false })
void testServiceThrowsReturnsErrorResponse(boolean disableOffloading) throws Exception {
    // the test suite state isn't used by this individual test, but cleanup code requires it is initialized.
    setUp(IMMEDIATE, IMMEDIATE);
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0));
    if (disableOffloading) {
        serverBuilder.executionStrategy(offloadNone());
    }
    try (ServerContext serverCtx = serverBuilder.listenStreamingAndAwait((ctx, request, responseFactory) -> {
        throw DELIBERATE_EXCEPTION;
    });
        BlockingHttpClient client = disableOffloading(HttpClients.forResolvedAddress(serverHostAndPort(serverCtx)), disableOffloading).buildBlocking()) {
        HttpResponse resp = client.request(client.get("/"));
        assertThat(resp.status(), is(INTERNAL_SERVER_ERROR));
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with BlockingHttpClient

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

the class NoOffloadsStrategyTest method turnOffAllExecutors.

@Test
void turnOffAllExecutors() throws Exception {
    serverBuilder.executor(immediate()).executionStrategy(customStrategyBuilder().offloadNone().build());
    StreamingHttpServiceImpl svc = new StreamingHttpServiceImpl();
    BlockingHttpClient client = initServerAndClient(svc);
    client.request(client.get("/"));
    assertThat("Unexpected thread for the server executor.", svc.executorThread.getName(), startsWith(IO_EXECUTOR_NAME));
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Test(org.junit.jupiter.api.Test)

Example 34 with BlockingHttpClient

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

the class NoOffloadsStrategyTest method noOffloadsStillUsesAServerExecutor.

@Test
void noOffloadsStillUsesAServerExecutor() throws Exception {
    serverBuilder.executionStrategy(customStrategyBuilder().offloadNone().build());
    StreamingHttpServiceImpl svc = new StreamingHttpServiceImpl();
    BlockingHttpClient client = initServerAndClient(svc);
    client.request(client.get("/"));
    assertThat("Unexpected thread for the server executor.", svc.executorThread.getName(), not(startsWith(IO_EXECUTOR_NAME)));
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) Test(org.junit.jupiter.api.Test)

Example 35 with BlockingHttpClient

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

the class PartitionedHttpClientTest method testPartitionByTarget.

@Test
void testPartitionByTarget() throws Exception {
    final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, req.requestTarget().substring(1));
    try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).initializer((pa, builder) -> builder.unresolvedAddressToHost(addr -> pa.get(SRV_NAME))).buildBlocking()) {
        sdPublisher.onSubscribe(new TestSubscription());
        sdPublisher.onNext(new TestPSDE(SRV_1, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, (InetSocketAddress) srv2.listenAddress()));
        final HttpResponse httpResponse1 = clt.request(clt.get("/" + SRV_2));
        final HttpResponse httpResponse2 = clt.request(clt.get("/" + SRV_1));
        MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
        MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_1));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) HttpRequestMethod(io.servicetalk.http.api.HttpRequestMethod) Publisher(io.servicetalk.concurrent.api.Publisher) ClientGroup(io.servicetalk.client.api.ClientGroup) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) AVAILABLE(io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) Objects.requireNonNull(java.util.Objects.requireNonNull) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) InetAddress.getLoopbackAddress(java.net.InetAddress.getLoopbackAddress) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpRequestFactory(io.servicetalk.http.api.StreamingHttpRequestFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Single(io.servicetalk.concurrent.api.Single) HttpResponse(io.servicetalk.http.api.HttpResponse) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) StandardCharsets(java.nio.charset.StandardCharsets) PartitionedServiceDiscovererEvent(io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) List(java.util.List) PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) MatcherAssert(org.hamcrest.MatcherAssert) ExecutionContext(io.servicetalk.transport.api.ExecutionContext) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) DefaultStreamingHttpRequestResponseFactory(io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory) HostAndPort(io.servicetalk.transport.api.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)100 HttpResponse (io.servicetalk.http.api.HttpResponse)69 ServerContext (io.servicetalk.transport.api.ServerContext)54 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)49 Test (org.junit.jupiter.api.Test)41 InetSocketAddress (java.net.InetSocketAddress)32 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)30 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)28 MethodSource (org.junit.jupiter.params.provider.MethodSource)28 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)26 HttpRequest (io.servicetalk.http.api.HttpRequest)25 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)25 Single (io.servicetalk.concurrent.api.Single)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)21 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)21 HostAndPort (io.servicetalk.transport.api.HostAndPort)21 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)21 Matchers.is (org.hamcrest.Matchers.is)20 Nullable (javax.annotation.Nullable)16 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)15