use of io.servicetalk.http.api.StreamingHttpClient in project servicetalk by apple.
the class HttpLifecycleObserverTest method makeRequestAndAssertResponse.
private void makeRequestAndAssertResponse(String path, HttpProtocol protocol, HttpResponseStatus status, int contentLength) throws Exception {
StreamingHttpClient client = streamingHttpClient();
StreamingHttpRequest request = contentLength == 0 ? client.get(path) : client.post(path).payloadBody(Publisher.from(CONTENT.duplicate())).transform(// adds empty trailers
new StatelessTrailersTransformer<>());
StreamingHttpResponse response = client.request(request).toFuture().get();
assertResponse(response, protocol.version, status, contentLength);
}
use of io.servicetalk.http.api.StreamingHttpClient 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.StreamingHttpClient 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.StreamingHttpClient in project servicetalk by apple.
the class HttpTransportObserverAsyncContextTest method clientErrorWrite.
@ParameterizedTest(name = "protocol={0}")
@EnumSource(HttpProtocol.class)
void clientErrorWrite(HttpProtocol httpProtocol) {
setUp(httpProtocol);
StreamingHttpClient client = streamingHttpClient();
assertThrows(ExecutionException.class, () -> client.request(client.post(SVC_NO_CONTENT_AFTER_READ).payloadBody(Publisher.failed(DELIBERATE_EXCEPTION))).toFuture().get());
assertMap(clientObserver.storageMap(), CLIENT_VALUE);
}
use of io.servicetalk.http.api.StreamingHttpClient in project servicetalk by apple.
the class LoadBalancerReadyHttpClientTest method verifyFailsAction0.
private Throwable verifyFailsAction0(Function<StreamingHttpClient, Single<?>> action, Consumer<Throwable> errorConsumer, Throwable error) throws InterruptedException {
StreamingHttpClient client = TestStreamingHttpClient.from(reqRespFactory, mockExecutionCtx, appendClientFilterFactory(newAutomaticRetryFilterFactory(loadBalancerPublisher, sdStatusCompletable), testHandler));
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Throwable> causeRef = new AtomicReference<>();
action.apply(client).whenOnError(causeRef::set).afterFinally(latch::countDown).toCompletable().subscribe();
// We don't expect the request to complete until onInitialized completes.
assertThat(latch.await(100, MILLISECONDS), is(false));
// When a failure occurs that should also fail the action!
errorConsumer.accept(error);
latch.await();
return causeRef.get();
}
Aggregations