Search in sources :

Example 1 with ExecutionContextExtension

use of io.servicetalk.transport.netty.internal.ExecutionContextExtension in project servicetalk by apple.

the class DefaultMultiAddressUrlHttpClientBuilderTest method internalClientsUseDifferentExecutionContextWhenConfigured.

@Test
void internalClientsUseDifferentExecutionContextWhenConfigured() throws Exception {
    // Assert prerequisites first.
    // Use different strategies, as ExecutionContextExtension shares the same strategy.
    HttpExecutionStrategy internalExecutionStrategy = HttpExecutionStrategies.customStrategyBuilder().offloadNone().build();
    HttpExecutionStrategy externalExecutionStrategy = HttpExecutionStrategies.customStrategyBuilder().offloadAll().build();
    assertThat(internalExecutionStrategy, not(equalTo(externalExecutionStrategy)));
    BufferAllocator internalBufferAllocator = BufferAllocators.PREFER_DIRECT_ALLOCATOR;
    BufferAllocator externalBufferAllocator = BufferAllocators.PREFER_HEAP_ALLOCATOR;
    assertThat(internalBufferAllocator, not(equalTo(externalBufferAllocator)));
    assertThat(CTX.executor(), not(equalTo(INTERNAL_CLIENT_CTX.executor())));
    assertThat(CTX.ioExecutor(), not(equalTo(INTERNAL_CLIENT_CTX.ioExecutor())));
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNever()).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok()))) {
        AtomicReference<BufferAllocator> actualInternalBufferAllocator = new AtomicReference<>();
        AtomicReference<IoExecutor> actualInternalIoExecutor = new AtomicReference<>();
        AtomicReference<Executor> actualInternalExecutor = new AtomicReference<>();
        AtomicReference<ExecutionStrategy> actualInternalExecutionStrategy = new AtomicReference<>();
        final StreamingHttpClient streamingHttpClient = HttpClients.forMultiAddressUrl().initializer((scheme, address, builder) -> builder.executionStrategy(internalExecutionStrategy).executor(INTERNAL_CLIENT_CTX.executor()).ioExecutor(INTERNAL_CLIENT_CTX.ioExecutor()).bufferAllocator(internalBufferAllocator).executionStrategy(internalExecutionStrategy).appendClientFilter(client -> {
            HttpExecutionContext internalContext = client.executionContext();
            actualInternalBufferAllocator.set(internalContext.bufferAllocator());
            actualInternalExecutor.set(internalContext.executor());
            actualInternalIoExecutor.set(internalContext.ioExecutor());
            actualInternalExecutionStrategy.set(internalContext.executionStrategy());
            return new StreamingHttpClientFilter(client) {
            };
        })).executor(CTX.executor()).ioExecutor(CTX.ioExecutor()).executionStrategy(externalExecutionStrategy).bufferAllocator(externalBufferAllocator).buildStreaming();
        assertNotNull(streamingHttpClient);
        // Check external client
        final HttpExecutionContext executionContext = streamingHttpClient.executionContext();
        assertThat(executionContext.executor(), equalTo(CTX.executor()));
        assertThat(executionContext.executionStrategy(), equalTo(externalExecutionStrategy));
        assertThat(executionContext.ioExecutor(), equalTo(CTX.ioExecutor()));
        assertThat(executionContext.bufferAllocator(), equalTo(CTX.bufferAllocator()));
        // Make a request to trigger the filter execution that extracts the execution context.
        final HostAndPort address = HostAndPort.of((InetSocketAddress) serverContext.listenAddress());
        streamingHttpClient.reserveConnection(streamingHttpClient.get("http://" + address)).toFuture().get();
        // Check internal client
        assertThat(actualInternalBufferAllocator.get(), equalTo(internalBufferAllocator));
        assertThat(actualInternalExecutor.get(), equalTo(INTERNAL_CLIENT_CTX.executor()));
        assertThat(actualInternalIoExecutor.get(), equalTo(INTERNAL_CLIENT_CTX.ioExecutor()));
        assertThat(actualInternalExecutionStrategy.get(), equalTo(internalExecutionStrategy));
        streamingHttpClient.closeAsync().toFuture().get();
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Matchers.not(org.hamcrest.Matchers.not) ExecutionContextExtension.cached(io.servicetalk.transport.netty.internal.ExecutionContextExtension.cached) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpRequester(io.servicetalk.http.api.HttpRequester) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) ExecutionStrategy(io.servicetalk.transport.api.ExecutionStrategy) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategies(io.servicetalk.http.api.HttpExecutionStrategies) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) Executor(io.servicetalk.concurrent.api.Executor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ExecutionContextExtension.immediate(io.servicetalk.transport.netty.internal.ExecutionContextExtension.immediate) ServerContext(io.servicetalk.transport.api.ServerContext) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) BlockingHttpRequester(io.servicetalk.http.api.BlockingHttpRequester) BlockingStreamingHttpRequester(io.servicetalk.http.api.BlockingStreamingHttpRequester) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) InetSocketAddress(java.net.InetSocketAddress) Mockito.verify(org.mockito.Mockito.verify) BufferAllocators(io.servicetalk.buffer.netty.BufferAllocators) Test(org.junit.jupiter.api.Test) IoExecutor(io.servicetalk.transport.api.IoExecutor) Mockito.never(org.mockito.Mockito.never) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) Matchers.equalTo(org.hamcrest.Matchers.equalTo) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) Mockito.mock(org.mockito.Mockito.mock) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) IoExecutor(io.servicetalk.transport.api.IoExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferAllocator(io.servicetalk.buffer.api.BufferAllocator) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) HostAndPort(io.servicetalk.transport.api.HostAndPort) Executor(io.servicetalk.concurrent.api.Executor) IoExecutor(io.servicetalk.transport.api.IoExecutor) ServerContext(io.servicetalk.transport.api.ServerContext) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) ExecutionStrategy(io.servicetalk.transport.api.ExecutionStrategy) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)1 BufferAllocators (io.servicetalk.buffer.netty.BufferAllocators)1 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)1 ServiceDiscovererEvent (io.servicetalk.client.api.ServiceDiscovererEvent)1 Executor (io.servicetalk.concurrent.api.Executor)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 BlockingHttpRequester (io.servicetalk.http.api.BlockingHttpRequester)1 BlockingStreamingHttpRequester (io.servicetalk.http.api.BlockingStreamingHttpRequester)1 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)1 HttpExecutionStrategies (io.servicetalk.http.api.HttpExecutionStrategies)1 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)1 HttpExecutionStrategies.offloadNever (io.servicetalk.http.api.HttpExecutionStrategies.offloadNever)1 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)1 HttpRequester (io.servicetalk.http.api.HttpRequester)1 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)1 StreamingHttpClientFilter (io.servicetalk.http.api.StreamingHttpClientFilter)1 StreamingHttpRequester (io.servicetalk.http.api.StreamingHttpRequester)1 ExecutionStrategy (io.servicetalk.transport.api.ExecutionStrategy)1 HostAndPort (io.servicetalk.transport.api.HostAndPort)1 IoExecutor (io.servicetalk.transport.api.IoExecutor)1