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