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();
}
}
Aggregations