Search in sources :

Example 1 with ConnectAndHttpExecutionStrategy

use of io.servicetalk.http.api.ConnectAndHttpExecutionStrategy in project servicetalk by apple.

the class ConnectionFactoryOffloadingTest method testFactoryOffloading.

@ParameterizedTest(name = "offload={0} httpStrategy={1}")
@MethodSource("testCases")
void testFactoryOffloading(boolean offload, HttpExecutionStrategy httpStrategy) throws Exception {
    AtomicReference<Thread> factoryThread = new AtomicReference<>();
    Thread appThread = Thread.currentThread();
    try (ServerContext server = HttpServers.forPort(0).listenAndAwait(this::helloWorld)) {
        SocketAddress serverAddress = server.listenAddress();
        ConnectionFactoryFilter<SocketAddress, FilterableStreamingHttpConnection> factory = ConnectionFactoryFilter.withStrategy(original -> new ConnectionFactory<SocketAddress, FilterableStreamingHttpConnection>() {

            private final ListenableAsyncCloseable close = emptyAsyncCloseable();

            @Override
            public Single<FilterableStreamingHttpConnection> newConnection(final SocketAddress socketAddress, @Nullable final TransportObserver observer) {
                factoryThread.set(Thread.currentThread());
                return original.newConnection(socketAddress, observer);
            }

            @Override
            public Completable onClose() {
                return close.onClose();
            }

            @Override
            public Completable closeAsync() {
                return close.closeAsync();
            }

            @Override
            public Completable closeAsyncGracefully() {
                return close.closeAsyncGracefully();
            }
        }, new ConnectAndHttpExecutionStrategy(offload ? ConnectExecutionStrategy.offloadAll() : ConnectExecutionStrategy.offloadNone(), httpStrategy));
        try (HttpClient client = HttpClients.forResolvedAddress(serverAddress).appendConnectionFactoryFilter(factory).build()) {
            assertThat(client.executionContext().executionStrategy().missing(httpStrategy), is(HttpExecutionStrategies.offloadNone()));
            Single<HttpResponse> single = client.request(client.get("/sayHello"));
            HttpResponse response = single.toFuture().get();
            assertThat("unexpected status", response.status(), is(HttpResponseStatus.OK));
        }
    }
    assertTrue((offload && !IoThreadFactory.IoThread.isIoThread(factoryThread.get())) || (!offload && factoryThread.get() == appThread), "incorrect offloading, offload=" + offload + " thread=" + factoryThread.get());
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) TransportObserver(io.servicetalk.transport.api.TransportObserver) HttpResponse(io.servicetalk.http.api.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) ConnectAndHttpExecutionStrategy(io.servicetalk.http.api.ConnectAndHttpExecutionStrategy) HttpClient(io.servicetalk.http.api.HttpClient) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) SocketAddress(java.net.SocketAddress) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Completable (io.servicetalk.concurrent.api.Completable)1 ListenableAsyncCloseable (io.servicetalk.concurrent.api.ListenableAsyncCloseable)1 Single (io.servicetalk.concurrent.api.Single)1 ConnectAndHttpExecutionStrategy (io.servicetalk.http.api.ConnectAndHttpExecutionStrategy)1 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)1 HttpClient (io.servicetalk.http.api.HttpClient)1 HttpResponse (io.servicetalk.http.api.HttpResponse)1 ServerContext (io.servicetalk.transport.api.ServerContext)1 TransportObserver (io.servicetalk.transport.api.TransportObserver)1 SocketAddress (java.net.SocketAddress)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1