Search in sources :

Example 1 with BAD_REQUEST

use of io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST in project servicetalk by apple.

the class HttpServerMultipleRequestsTest method consumeOfRequestBodyDoesNotCloseConnection.

@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void consumeOfRequestBodyDoesNotCloseConnection() throws Exception {
    StreamingHttpService service = (ctx, request, responseFactory) -> {
        request.messageBody().ignoreElements().subscribe();
        CharSequence requestId = request.headers().get(REQUEST_ID_HEADER);
        if (requestId != null) {
            StreamingHttpResponse response = responseFactory.ok();
            response.headers().set(REQUEST_ID_HEADER, requestId);
            return succeeded(response);
        } else {
            return succeeded(responseFactory.newResponse(BAD_REQUEST));
        }
    };
    final int concurrency = 10;
    final int numRequests = 10;
    CompositeCloseable compositeCloseable = AsyncCloseables.newCompositeCloseable();
    ServerContext ctx = compositeCloseable.append(HttpServers.forAddress(localAddress(0)).ioExecutor(serverContext.ioExecutor()).executor(serverContext.executor()).executionStrategy(defaultStrategy()).listenStreamingAndAwait(service));
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        AtomicReference<Throwable> causeRef = new AtomicReference<>();
        CyclicBarrier barrier = new CyclicBarrier(concurrency);
        CountDownLatch latch = new CountDownLatch(concurrency);
        for (int i = 0; i < concurrency; ++i) {
            final int finalI = i;
            executorService.execute(() -> {
                try {
                    StreamingHttpClient client = compositeCloseable.append(HttpClients.forResolvedAddress(serverHostAndPort(ctx)).protocols(h1().maxPipelinedRequests(numRequests).build()).ioExecutor(clientContext.ioExecutor()).executor(clientContext.executor()).executionStrategy(defaultStrategy()).buildStreaming());
                    ReservedStreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get();
                    compositeCloseable.append(connection);
                    barrier.await();
                    for (int x = 0; x < numRequests; ++x) {
                        makeClientRequestWithId(connection, "thread=" + finalI + " request=" + x);
                    }
                } catch (Throwable cause) {
                    causeRef.compareAndSet(null, cause);
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await();
        assertNull(causeRef.get());
    } finally {
        executorService.shutdown();
        compositeCloseable.close();
    }
}
Also used : StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ExecutionContextExtension.cached(io.servicetalk.transport.netty.internal.ExecutionContextExtension.cached) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) BAD_REQUEST(io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) CyclicBarrier(java.util.concurrent.CyclicBarrier) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) NettyIoThreadFactory(io.servicetalk.transport.netty.internal.NettyIoThreadFactory) HttpProtocolConfigs.h1(io.servicetalk.http.netty.HttpProtocolConfigs.h1) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) OK(io.servicetalk.http.api.HttpResponseStatus.OK) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AsyncCloseables(io.servicetalk.concurrent.api.AsyncCloseables) ReservedStreamingHttpConnection(io.servicetalk.http.api.ReservedStreamingHttpConnection) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) ReservedStreamingHttpConnection(io.servicetalk.http.api.ReservedStreamingHttpConnection) ServerContext(io.servicetalk.transport.api.ServerContext) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) ExecutorService(java.util.concurrent.ExecutorService) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

CharSequences.newAsciiString (io.servicetalk.buffer.api.CharSequences.newAsciiString)1 AsyncCloseables (io.servicetalk.concurrent.api.AsyncCloseables)1 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)1 BAD_REQUEST (io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST)1 OK (io.servicetalk.http.api.HttpResponseStatus.OK)1 ReservedStreamingHttpConnection (io.servicetalk.http.api.ReservedStreamingHttpConnection)1 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)1 StreamingHttpConnection (io.servicetalk.http.api.StreamingHttpConnection)1 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)1 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)1 StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)1 HttpProtocolConfigs.h1 (io.servicetalk.http.netty.HttpProtocolConfigs.h1)1 ServerContext (io.servicetalk.transport.api.ServerContext)1 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)1 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)1 ExecutionContextExtension (io.servicetalk.transport.netty.internal.ExecutionContextExtension)1 ExecutionContextExtension.cached (io.servicetalk.transport.netty.internal.ExecutionContextExtension.cached)1 NettyIoThreadFactory (io.servicetalk.transport.netty.internal.NettyIoThreadFactory)1