use of io.servicetalk.http.api.ReservedStreamingHttpConnection in project servicetalk by apple.
the class ExecutionStrategyInContextTest method testStreaming.
@ParameterizedTest(name = "customStrategy={0}")
@ValueSource(booleans = { false, true })
void testStreaming(boolean customStrategy) throws Exception {
StreamingHttpClient client = initClientAndServer(builder -> builder.listenStreaming((ctx, request, responseFactory) -> {
serviceStrategyRef.set(ctx.executionContext().executionStrategy());
return succeeded(responseFactory.ok());
}), customStrategy).buildStreaming();
clientAsCloseable = client;
if (!customStrategy) {
assert expectedClientStrategy == null;
expectedClientStrategy = defaultStrategy();
assert expectedServerStrategy == null;
expectedServerStrategy = defaultStrategy();
}
HttpExecutionStrategy clientStrat = client.executionContext().executionStrategy();
assertThat("Unexpected client strategy.", clientStrat, equalStrategies(expectedClientStrategy));
client.request(client.get("/")).toFuture().get();
assertThat("Unexpected service strategy", serviceStrategyRef.get(), equalStrategies(expectedServerStrategy));
ReservedStreamingHttpConnection conn = client.reserveConnection(client.get("/")).toFuture().get();
assertThat("Unexpected connection strategy (from execution context).", conn.executionContext().executionStrategy(), equalStrategies(expectedClientStrategy));
assertThat("Unexpected connection strategy (from execution context).", conn.connectionContext().executionContext().executionStrategy(), equalStrategies(expectedClientStrategy));
}
use of io.servicetalk.http.api.ReservedStreamingHttpConnection in project servicetalk by apple.
the class GracefulCloseTest method useConnection.
@ParameterizedTest
@EnumSource(TrailerAddType.class)
void useConnection(TrailerAddType trailerAddType) throws Exception {
setUp(trailerAddType);
ReservedStreamingHttpConnection conn = client.reserveConnection(client.get("/")).toFuture().get();
StreamingHttpResponse resp = conn.request(client.get("/").payloadBody(from("Hello"), appSerializerUtf8FixLen())).toFuture().get();
assertThat("Unexpected response.", resp.status().code(), equalTo(HttpResponseStatus.OK.code()));
// Drain response.
resp.payloadBody().toFuture().get();
conn.close();
}
use of io.servicetalk.http.api.ReservedStreamingHttpConnection in project servicetalk by apple.
the class HttpServerMultipleRequestsTest method consumeOfRequestBodyDoesNotCloseConnection.
@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void consumeOfRequestBodyDoesNotCloseConnection() throws Exception {
StreamingHttpService service = (ctx, request, responseFactory) -> {
request.messageBody().ignoreElements().subscribe();
CharSequence requestId = request.headers().get(REQUEST_ID_HEADER);
if (requestId != null) {
StreamingHttpResponse response = responseFactory.ok();
response.headers().set(REQUEST_ID_HEADER, requestId);
return succeeded(response);
} else {
return succeeded(responseFactory.newResponse(BAD_REQUEST));
}
};
final int concurrency = 10;
final int numRequests = 10;
CompositeCloseable compositeCloseable = AsyncCloseables.newCompositeCloseable();
ServerContext ctx = compositeCloseable.append(HttpServers.forAddress(localAddress(0)).ioExecutor(serverContext.ioExecutor()).executor(serverContext.executor()).executionStrategy(defaultStrategy()).listenStreamingAndAwait(service));
ExecutorService executorService = Executors.newCachedThreadPool();
try {
AtomicReference<Throwable> causeRef = new AtomicReference<>();
CyclicBarrier barrier = new CyclicBarrier(concurrency);
CountDownLatch latch = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; ++i) {
final int finalI = i;
executorService.execute(() -> {
try {
StreamingHttpClient client = compositeCloseable.append(HttpClients.forResolvedAddress(serverHostAndPort(ctx)).protocols(h1().maxPipelinedRequests(numRequests).build()).ioExecutor(clientContext.ioExecutor()).executor(clientContext.executor()).executionStrategy(defaultStrategy()).buildStreaming());
ReservedStreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get();
compositeCloseable.append(connection);
barrier.await();
for (int x = 0; x < numRequests; ++x) {
makeClientRequestWithId(connection, "thread=" + finalI + " request=" + x);
}
} catch (Throwable cause) {
causeRef.compareAndSet(null, cause);
} finally {
latch.countDown();
}
});
}
latch.await();
assertNull(causeRef.get());
} finally {
executorService.shutdown();
compositeCloseable.close();
}
}
use of io.servicetalk.http.api.ReservedStreamingHttpConnection in project servicetalk by apple.
the class ConnectionFactoryFilterTest method onClosingIsDelegated.
@Test
void onClosingIsDelegated() throws Exception {
CompletableSource.Processor onClosing = newCompletableProcessor();
client = clientBuilder.appendConnectionFactoryFilter(newConnectionFactoryFilter(delegate -> new NettyConnectionContextReturningConnection(delegate, onClosing))).buildBlocking();
ReservedStreamingHttpConnection con = client.asStreamingClient().reserveConnection(client.get("/")).toFuture().get();
NettyConnectionContext ctx = (NettyConnectionContext) con.connectionContext();
onClosing.onComplete();
ctx.onClosing().toFuture().get();
}
use of io.servicetalk.http.api.ReservedStreamingHttpConnection in project servicetalk by apple.
the class LoadBalancedStreamingHttpClient method reserveConnection.
@Override
public Single<ReservedStreamingHttpConnection> reserveConnection(final HttpRequestMetaData metaData) {
return Single.defer(() -> {
Single<ReservedStreamingHttpConnection> connection = loadBalancer.selectConnection(SELECTOR_FOR_RESERVE).map(identity());
final HttpExecutionStrategy strategy = requestExecutionStrategy(metaData, executionContext().executionStrategy());
return (strategy.isMetadataReceiveOffloaded() || strategy.isDataReceiveOffloaded() ? connection.publishOn(executionContext.executor(), IoThread::currentThreadIsIoThread) : connection).shareContextOnSubscribe();
});
}
Aggregations