Search in sources :

Example 46 with ServerContext

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

the class BlockingStreamingHttpClientTest method iterableSequentialConsumptionDoesNotDeadLock.

@ParameterizedTest
@ValueSource(ints = { 1, 100 })
void iterableSequentialConsumptionDoesNotDeadLock(int numItems) throws Exception {
    try (ServerContext ctx = HttpServers.forAddress(localAddress(0)).listenBlockingStreamingAndAwait((ctx1, request, response) -> {
        HttpPayloadWriter<String> output = response.sendMetaData(appSerializerAsciiFixLen());
        for (String input : request.payloadBody(appSerializerAsciiFixLen())) {
            output.write(input);
        }
        output.close();
    });
        BlockingStreamingHttpClient client = HttpClients.forResolvedAddress(serverHostAndPort(ctx)).buildBlockingStreaming()) {
        NextSuppliers<String> nextSuppliers = NextSuppliers.stringSuppliers(numItems);
        // Allow first item in Iterator to return from next().
        nextSuppliers.countDownNextLatch();
        BlockingStreamingHttpResponse resp = client.request(client.get("/").payloadBody(new TestBlockingIterable<>(nextSuppliers, nextSuppliers), appSerializerAsciiFixLen()));
        StringBuilder responseBody = new StringBuilder(numItems);
        int i = 0;
        for (String respChunk : resp.payloadBody(appSerializerAsciiFixLen())) {
            // Goal is to ensure we can write each individual chunk independently without blocking threads or having
            // to batch multiple items. As each chunk is echoed back, unblock the next one.
            nextSuppliers.countDownNextLatch();
            responseBody.append(respChunk);
            ++i;
        }
        assertThat("num items: " + i + " responseBody: " + responseBody, i, equalTo(numItems));
    }
}
Also used : BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with ServerContext

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

the class AbstractHttpServiceAsyncContextTest method newRequestsGetFreshContext.

final void newRequestsGetFreshContext(boolean useImmediate) throws Exception {
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final int concurrency = 10;
    final int numRequests = 10;
    final String k1Value = "value";
    // The service should get an empty AsyncContext regardless of what is done outside the service.
    // There are utilities that may be accessed in a static context or before service initialization that
    // shouldn't pollute the service's AsyncContext.
    AsyncContext.put(K1, k1Value);
    try (ServerContext ctx = serverWithEmptyAsyncContextService(HttpServers.forAddress(localAddress(0)), useImmediate)) {
        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(() -> {
                SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = forResolvedAddress(serverHostAndPort(ctx)).protocols(h1().maxPipelinedRequests(numRequests).build());
                try (StreamingHttpClient client = (!useImmediate ? clientBuilder : clientBuilder.executionStrategy(offloadNone())).buildStreaming()) {
                    try (StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
                        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());
        assertEquals(k1Value, AsyncContext.get(K1));
    } finally {
        executorService.shutdown();
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServerContext(io.servicetalk.transport.api.ServerContext) ExecutorService(java.util.concurrent.ExecutorService) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection)

Example 48 with ServerContext

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

the class AbstractNettyHttpServerTest method startServer.

private void startServer() throws Exception {
    final InetSocketAddress bindAddress = localAddress(0);
    service(new TestServiceStreaming(publisherSupplier));
    // A small SNDBUF is needed to test that the server defers closing the connection until writes are complete.
    // However, if it is too small, tests that expect certain chunks of data will see those chunks broken up
    // differently.
    final HttpServerBuilder serverBuilder = HttpServers.forAddress(bindAddress).executor(serverExecutor).socketOption(StandardSocketOptions.SO_SNDBUF, 100).protocols(protocol).transportObserver(serverTransportObserver).enableWireLogging("servicetalk-tests-wire-logger", TRACE, () -> true);
    configureServerBuilder(serverBuilder);
    if (sslEnabled) {
        serverBuilder.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build());
    }
    if (nonOffloadingServiceFilterFactory != null) {
        serverBuilder.appendNonOffloadingServiceFilter(nonOffloadingServiceFilterFactory);
    }
    if (serviceFilterFactory != null) {
        serverBuilder.appendServiceFilter(serviceFilterFactory);
    }
    if (serverLifecycleObserver != NoopHttpLifecycleObserver.INSTANCE) {
        serverBuilder.lifecycleObserver(serverLifecycleObserver);
    }
    serverContext = awaitIndefinitelyNonNull(listen(serverBuilder.ioExecutor(serverIoExecutor).appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(connectionAcceptor))).beforeOnSuccess(ctx -> LOGGER.debug("Server started on {}.", ctx.listenAddress())).beforeOnError(throwable -> LOGGER.debug("Failed starting server on {}.", bindAddress)));
    final SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = newClientBuilder();
    if (sslEnabled) {
        clientBuilder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).build());
    }
    if (connectionFactoryFilter != null) {
        clientBuilder.appendConnectionFactoryFilter(connectionFactoryFilter);
    }
    if (connectionFilterFactory != null) {
        clientBuilder.appendConnectionFilter(connectionFilterFactory);
    }
    if (clientTransportObserver != NoopTransportObserver.INSTANCE) {
        clientBuilder.appendConnectionFactoryFilter(new TransportObserverConnectionFactoryFilter<>(clientTransportObserver));
    }
    if (clientLifecycleObserver != NoopHttpLifecycleObserver.INSTANCE) {
        clientBuilder.appendClientFilter(new HttpLifecycleObserverRequesterFilter(clientLifecycleObserver));
    }
    if (clientFilterFactory != null) {
        clientBuilder.appendClientFilter(clientFilterFactory);
    }
    httpClient = clientBuilder.ioExecutor(clientIoExecutor).executor(clientExecutor).executionStrategy(defaultStrategy()).protocols(protocol).enableWireLogging("servicetalk-tests-wire-logger", TRACE, Boolean.TRUE::booleanValue).buildStreaming();
    httpConnection = httpClient.reserveConnection(httpClient.get("/")).toFuture().get();
}
Also used : HttpLifecycleObserver(io.servicetalk.http.api.HttpLifecycleObserver) PlatformDependent.throwException(io.servicetalk.utils.internal.PlatformDependent.throwException) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) LoggerFactory(org.slf4j.LoggerFactory) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) ConnectionAcceptor(io.servicetalk.transport.api.ConnectionAcceptor) AfterAll(org.junit.jupiter.api.AfterAll) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) Assumptions.assumeFalse(org.junit.jupiter.api.Assumptions.assumeFalse) Executor(io.servicetalk.concurrent.api.Executor) HttpProtocolConfig(io.servicetalk.http.api.HttpProtocolConfig) BlockingTestUtils.awaitIndefinitelyNonNull(io.servicetalk.concurrent.api.BlockingTestUtils.awaitIndefinitelyNonNull) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) StandardSocketOptions(java.net.StandardSocketOptions) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ConnectionFactoryFilter(io.servicetalk.client.api.ConnectionFactoryFilter) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) InetSocketAddress(java.net.InetSocketAddress) DefaultTestCerts.serverPemHostname(io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname) Buffer(io.servicetalk.buffer.api.Buffer) DelegatingConnectionAcceptor(io.servicetalk.transport.api.DelegatingConnectionAcceptor) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) TransportObserver(io.servicetalk.transport.api.TransportObserver) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) Matchers.is(org.hamcrest.Matchers.is) Strictness(org.mockito.quality.Strictness) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) Publisher(io.servicetalk.concurrent.api.Publisher) Mock(org.mockito.Mock) TRACE(io.servicetalk.logging.api.LogLevel.TRACE) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) Function(java.util.function.Function) Supplier(java.util.function.Supplier) HttpProtocolConfigs.h1Default(io.servicetalk.http.netty.HttpProtocolConfigs.h1Default) HttpSerializers.appSerializerUtf8FixLen(io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen) HttpServerContext(io.servicetalk.http.api.HttpServerContext) Objects.requireNonNull(java.util.Objects.requireNonNull) HttpProtocolVersion(io.servicetalk.http.api.HttpProtocolVersion) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DefaultThreadFactory(io.servicetalk.concurrent.api.DefaultThreadFactory) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) Nullable(javax.annotation.Nullable) ACCEPT_ALL(io.servicetalk.transport.api.ConnectionAcceptor.ACCEPT_ALL) NettyIoExecutors(io.servicetalk.transport.netty.NettyIoExecutors) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) Logger(org.slf4j.Logger) ServerContext(io.servicetalk.transport.api.ServerContext) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) Single(io.servicetalk.concurrent.api.Single) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) ExecutionException(java.util.concurrent.ExecutionException) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) IoExecutor(io.servicetalk.transport.api.IoExecutor) AfterEach(org.junit.jupiter.api.AfterEach) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) TransportObserverConnectionFactoryFilter(io.servicetalk.client.api.TransportObserverConnectionFactoryFilter) Executors.newCachedThreadExecutor(io.servicetalk.concurrent.api.Executors.newCachedThreadExecutor) NORM_PRIORITY(java.lang.Thread.NORM_PRIORITY) Executors(io.servicetalk.concurrent.api.Executors) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) NoopTransportObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver) HostAndPort(io.servicetalk.transport.api.HostAndPort) InetSocketAddress(java.net.InetSocketAddress) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) DelegatingConnectionAcceptor(io.servicetalk.transport.api.DelegatingConnectionAcceptor) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder)

Example 49 with ServerContext

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

the class TracingHttpRequesterFilterTest method testInjectWithParent.

@Test
void testInjectWithParent() throws Exception {
    final String requestUrl = "/foo";
    CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
    DefaultInMemoryTracer tracer = new DefaultInMemoryTracer.Builder(SCOPE_MANAGER).addListener(spanListener).build();
    try (ServerContext context = buildServer()) {
        try (HttpClient client = forSingleAddress(serverHostAndPort(context)).appendClientFilter(new TracingHttpRequesterFilter(tracer, "testClient")).appendClientFilter(new TestTracingLoggerFilter(TRACING_TEST_LOG_LINE_PREFIX)).build()) {
            InMemorySpan clientSpan = tracer.buildSpan("test").start();
            try (Scope ignored = tracer.activateSpan(clientSpan)) {
                HttpResponse response = client.request(client.get(requestUrl)).toFuture().get();
                TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
                assertThat(serverSpanState.traceId, isHexId());
                assertThat(serverSpanState.spanId, isHexId());
                assertThat(serverSpanState.parentSpanId, isHexId());
                assertThat(serverSpanState.traceId, equalToIgnoringCase(clientSpan.context().toTraceId()));
                assertThat(serverSpanState.parentSpanId, equalToIgnoringCase(clientSpan.context().toSpanId()));
                // don't mess with caller span state
                assertEquals(clientSpan, tracer.activeSpan());
                assertEquals(1, spanListener.spanFinishedCount());
                InMemorySpan lastFinishedSpan = spanListener.lastFinishedSpan();
                assertNotNull(lastFinishedSpan);
                assertEquals(SPAN_KIND_CLIENT, lastFinishedSpan.tags().get(SPAN_KIND.getKey()));
                assertEquals(GET.name(), lastFinishedSpan.tags().get(HTTP_METHOD.getKey()));
                assertEquals(requestUrl, lastFinishedSpan.tags().get(HTTP_URL.getKey()));
                assertEquals(OK.code(), lastFinishedSpan.tags().get(HTTP_STATUS.getKey()));
                assertFalse(lastFinishedSpan.tags().containsKey(ERROR.getKey()));
                verifyTraceIdPresentInLogs(stableAccumulated(1000), requestUrl, serverSpanState.traceId, serverSpanState.spanId, serverSpanState.parentSpanId, TRACING_TEST_LOG_LINE_PREFIX);
            } finally {
                clientSpan.finish();
            }
        }
    }
}
Also used : InMemorySpan(io.servicetalk.opentracing.inmemory.api.InMemorySpan) DefaultInMemoryTracer(io.servicetalk.opentracing.inmemory.DefaultInMemoryTracer) ServerContext(io.servicetalk.transport.api.ServerContext) Scope(io.opentracing.Scope) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) CountingInMemorySpanEventListener(io.servicetalk.opentracing.http.TestUtils.CountingInMemorySpanEventListener) Test(org.junit.jupiter.api.Test)

Example 50 with ServerContext

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

the class TracingHttpRequesterFilterTest method testInjectWithNoParent.

@Test
void testInjectWithNoParent() throws Exception {
    final String requestUrl = "/";
    CountingInMemorySpanEventListener spanListener = new CountingInMemorySpanEventListener();
    DefaultInMemoryTracer tracer = new DefaultInMemoryTracer.Builder(SCOPE_MANAGER).addListener(spanListener).build();
    try (ServerContext context = buildServer()) {
        try (HttpClient client = forSingleAddress(serverHostAndPort(context)).appendClientFilter(new TracingHttpRequesterFilter(tracer, "testClient")).appendClientFilter(new TestTracingLoggerFilter(TRACING_TEST_LOG_LINE_PREFIX)).build()) {
            HttpResponse response = client.request(client.get(requestUrl)).toFuture().get();
            TestSpanState serverSpanState = response.payloadBody(SPAN_STATE_SERIALIZER);
            assertThat(serverSpanState.traceId, isHexId());
            assertThat(serverSpanState.spanId, isHexId());
            assertNull(serverSpanState.parentSpanId);
            // don't mess with caller span state
            assertNull(tracer.activeSpan());
            assertEquals(1, spanListener.spanFinishedCount());
            InMemorySpan lastFinishedSpan = spanListener.lastFinishedSpan();
            assertNotNull(lastFinishedSpan);
            assertEquals(SPAN_KIND_CLIENT, lastFinishedSpan.tags().get(SPAN_KIND.getKey()));
            assertEquals(GET.name(), lastFinishedSpan.tags().get(HTTP_METHOD.getKey()));
            assertEquals(requestUrl, lastFinishedSpan.tags().get(HTTP_URL.getKey()));
            assertEquals(OK.code(), lastFinishedSpan.tags().get(HTTP_STATUS.getKey()));
            assertFalse(lastFinishedSpan.tags().containsKey(ERROR.getKey()));
            verifyTraceIdPresentInLogs(stableAccumulated(1000), requestUrl, serverSpanState.traceId, serverSpanState.spanId, null, TRACING_TEST_LOG_LINE_PREFIX);
        }
    }
}
Also used : InMemorySpan(io.servicetalk.opentracing.inmemory.api.InMemorySpan) DefaultInMemoryTracer(io.servicetalk.opentracing.inmemory.DefaultInMemoryTracer) ServerContext(io.servicetalk.transport.api.ServerContext) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) CountingInMemorySpanEventListener(io.servicetalk.opentracing.http.TestUtils.CountingInMemorySpanEventListener) 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