Search in sources :

Example 1 with PREFER_HEAP_ALLOCATOR

use of io.servicetalk.buffer.netty.BufferAllocators.PREFER_HEAP_ALLOCATOR in project servicetalk by apple.

the class DefaultMultiAddressUrlHttpClientBuilderTest method internalClientsUseDifferentExecutionContextWhenConfigured.

@Test
void internalClientsUseDifferentExecutionContextWhenConfigured() throws Exception {
    HttpExecutionContext externalCtx = new HttpExecutionContextBuilder().ioExecutor(CTX.ioExecutor()).executor(CTX.executor()).bufferAllocator(PREFER_HEAP_ALLOCATOR).executionStrategy(offloadAll()).build();
    HttpExecutionContext internalCtx = new HttpExecutionContextBuilder().ioExecutor(INTERNAL_CLIENT_CTX.ioExecutor()).executor(INTERNAL_CLIENT_CTX.executor()).bufferAllocator(PREFER_DIRECT_ALLOCATOR).executionStrategy(offloadNone()).build();
    AtomicReference<HttpExecutionContext> actualInternalCtx = new AtomicReference<>();
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNone()).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok()));
        BlockingHttpClient blockingHttpClient = HttpClients.forMultiAddressUrl().initializer((scheme, address, builder) -> builder.executionStrategy(internalCtx.executionStrategy()).executor(internalCtx.executor()).ioExecutor(internalCtx.ioExecutor()).bufferAllocator(internalCtx.bufferAllocator()).appendClientFilter(client -> {
            actualInternalCtx.set(client.executionContext());
            return new StreamingHttpClientFilter(client) {
            };
        })).executor(externalCtx.executor()).ioExecutor(externalCtx.ioExecutor()).executionStrategy(externalCtx.executionStrategy()).bufferAllocator(externalCtx.bufferAllocator()).buildBlocking()) {
        // Check external client
        assertExecutionContext(externalCtx, blockingHttpClient.executionContext());
        // Make a request to trigger the filter execution that extracts the execution context.
        HttpResponse response = blockingHttpClient.request(blockingHttpClient.get("http://" + serverHostAndPort(serverContext)));
        assertThat(response.status(), is(OK));
        // Check internal client
        assertExecutionContext(internalCtx, actualInternalCtx.get());
    }
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) HttpHeaders(io.servicetalk.http.api.HttpHeaders) 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) HttpRequest(io.servicetalk.http.api.HttpRequest) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) PREFER_HEAP_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.PREFER_HEAP_ALLOCATOR) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Matchers.contentEqualTo(io.servicetalk.buffer.api.Matchers.contentEqualTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ExecutionContextExtension.immediate(io.servicetalk.transport.netty.internal.ExecutionContextExtension.immediate) ServerContext(io.servicetalk.transport.api.ServerContext) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpResponse(io.servicetalk.http.api.HttpResponse) 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) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) Mockito.verify(org.mockito.Mockito.verify) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) PREFER_DIRECT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.PREFER_DIRECT_ALLOCATOR) Mockito.never(org.mockito.Mockito.never) HttpExecutionStrategies.offloadAll(io.servicetalk.http.api.HttpExecutionStrategies.offloadAll) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) HttpHeadersFactory(io.servicetalk.http.api.HttpHeadersFactory) HostAndPort(io.servicetalk.transport.api.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) HttpResponse(io.servicetalk.http.api.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

Matchers.contentEqualTo (io.servicetalk.buffer.api.Matchers.contentEqualTo)1 PREFER_DIRECT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.PREFER_DIRECT_ALLOCATOR)1 PREFER_HEAP_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.PREFER_HEAP_ALLOCATOR)1 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)1 ServiceDiscovererEvent (io.servicetalk.client.api.ServiceDiscovererEvent)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)1 BlockingHttpRequester (io.servicetalk.http.api.BlockingHttpRequester)1 BlockingStreamingHttpRequester (io.servicetalk.http.api.BlockingStreamingHttpRequester)1 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)1 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)1 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)1 HttpExecutionStrategies.offloadAll (io.servicetalk.http.api.HttpExecutionStrategies.offloadAll)1 HttpExecutionStrategies.offloadNone (io.servicetalk.http.api.HttpExecutionStrategies.offloadNone)1 HttpHeaders (io.servicetalk.http.api.HttpHeaders)1 HttpHeadersFactory (io.servicetalk.http.api.HttpHeadersFactory)1 HttpRequest (io.servicetalk.http.api.HttpRequest)1 HttpRequester (io.servicetalk.http.api.HttpRequester)1 HttpResponse (io.servicetalk.http.api.HttpResponse)1 OK (io.servicetalk.http.api.HttpResponseStatus.OK)1