use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.
the class AbstractSingleOffloadingTest method testOffloading.
protected int testOffloading(BiFunction<Single<String>, Executor, Single<String>> offloadingFunction, TerminalOperation terminal) throws InterruptedException {
Runnable appCode = () -> {
try {
// Insert a custom value into AsyncContext map
AsyncContext.put(ASYNC_CONTEXT_CUSTOM_KEY, ASYNC_CONTEXT_VALUE);
capture(CaptureSlot.APP);
// Add thread recording test points
final Single<String> original = testSingle.liftSync((SingleOperator<String, String>) subscriber -> {
capture(CaptureSlot.OFFLOADED_SUBSCRIBE);
return subscriber;
}).beforeOnSubscribe(cancellable -> capture(CaptureSlot.ORIGINAL_ON_SUBSCRIBE)).beforeFinally(new TerminalSignalConsumer() {
@Override
public void onComplete() {
capture(CaptureSlot.ORIGINAL_ON_COMPLETE);
}
@Override
public void onError(final Throwable throwable) {
capture(CaptureSlot.ORIGINAL_ON_ERROR);
}
@Override
public void cancel() {
capture(CaptureSlot.OFFLOADED_CANCEL);
}
});
// Perform offloading and add more thread recording test points
Single<String> offloaded = offloadingFunction.apply(original, testExecutor.executor()).liftSync((SingleOperator<String, String>) subscriber -> {
capture(CaptureSlot.ORIGINAL_SUBSCRIBE);
return subscriber;
}).beforeOnSubscribe(cancellable -> capture(CaptureSlot.OFFLOADED_ON_SUBSCRIBE)).beforeFinally(new TerminalSignalConsumer() {
@Override
public void onComplete() {
capture(CaptureSlot.OFFLOADED_ON_COMPLETE);
}
@Override
public void onError(final Throwable throwable) {
capture(CaptureSlot.OFFLOADED_ON_ERROR);
}
@Override
public void cancel() {
capture(CaptureSlot.ORIGINAL_CANCEL);
}
});
// subscribe and generate terminal
toSource(offloaded).subscribe(testSubscriber);
assertThat("Unexpected tasks " + testExecutor.executor().queuedTasksPending(), testExecutor.executor().queuedTasksPending(), lessThan(2));
if (1 == testExecutor.executor().queuedTasksPending()) {
// execute offloaded subscribe
testExecutor.executor().executeNextTask();
}
Cancellable cancellable = testSubscriber.awaitSubscription();
assertThat("No Cancellable", cancellable, notNullValue());
testSingle.awaitSubscribed();
assertThat("Source is not subscribed", testSingle.isSubscribed());
assertThat("Thread was interrupted", !Thread.currentThread().isInterrupted());
switch(terminal) {
case CANCEL:
cancellable.cancel();
break;
case COMPLETE:
testSingle.onSuccess(ITEM_VALUE);
break;
case ERROR:
testSingle.onError(DELIBERATE_EXCEPTION);
break;
default:
throw new AssertionError("unexpected terminal mode");
}
assertThat("Unexpected tasks " + testExecutor.executor().queuedTasksPending(), testExecutor.executor().queuedTasksPending(), lessThan(2));
if (1 == testExecutor.executor().queuedTasksPending()) {
// execute offloaded terminal
testExecutor.executor().executeNextTask();
}
} catch (Throwable all) {
AbstractOffloadingTest.LOGGER.warn("Unexpected throwable", all);
testSubscriber.onError(all);
}
};
APP_EXECUTOR_EXT.executor().execute(appCode);
// Ensure we reached the correct terminal condition
switch(terminal) {
case CANCEL:
testCancellable.awaitCancelled();
break;
case ERROR:
Throwable thrown = testSubscriber.awaitOnError();
assertThat("unexpected exception " + thrown, thrown, sameInstance(DELIBERATE_EXCEPTION));
break;
case COMPLETE:
String result = testSubscriber.awaitOnSuccess();
assertThat("Unexpected result", result, sameInstance(ITEM_VALUE));
break;
default:
throw new AssertionError("unexpected terminal mode");
}
// Ensure that Async Context Map was correctly set during signals
ContextMap appMap = capturedContexts.captured(CaptureSlot.APP);
assertThat(appMap, notNullValue());
ContextMap subscribeMap = capturedContexts.captured(CaptureSlot.ORIGINAL_SUBSCRIBE);
assertThat(subscribeMap, notNullValue());
assertThat("Map was shared not copied", subscribeMap, not(sameInstance(appMap)));
assertThat("Missing custom async context entry ", subscribeMap.get(ASYNC_CONTEXT_CUSTOM_KEY), sameInstance(ASYNC_CONTEXT_VALUE));
EnumSet<CaptureSlot> checkSlots = EnumSet.complementOf(EnumSet.of(CaptureSlot.APP, CaptureSlot.ORIGINAL_SUBSCRIBE));
checkSlots.stream().filter(slot -> null != capturedContexts.captured(slot)).forEach(slot -> {
ContextMap map = capturedContexts.captured(slot);
assertThat("Context map was not captured", map, is(notNullValue()));
assertThat("Custom key missing from context map", map.containsKey(ASYNC_CONTEXT_CUSTOM_KEY));
assertThat("Unexpected context map @ slot " + slot + " : " + map, map, sameInstance(subscribeMap));
});
// Ensure that all offloading completed.
assertThat("Offloading pending", testExecutor.executor().queuedTasksPending(), is(0));
return testExecutor.executor().queuedTasksExecuted();
}
use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverThrowsFromHandler.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void serverThrowsFromHandler(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
throw DELIBERATE_EXCEPTION;
}
}, null);
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpResponse response = client.request(client.get("/"));
assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
assertThat(response.payloadBody(), equalTo(EMPTY_BUFFER));
}
}
use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientSendsInvalidContentLength.
private void clientSendsInvalidContentLength(boolean addTrailers, BiConsumer<HttpHeaders, Integer> headersModifier) throws Exception {
assumeFalse(!h2PriorKnowledge && addTrailers, "HTTP/1.1 does not support Content-Length with trailers");
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).appendClientFilter(client1 -> new StreamingHttpClientFilter(client1) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return request.toRequest().map(req -> {
req.headers().remove(TRANSFER_ENCODING);
headersModifier.accept(req.headers(), req.payloadBody().readableBytes());
return req.toStreamingRequest();
}).flatMap(delegate::request);
}
}).buildBlocking()) {
HttpRequest request = client.get("/").payloadBody("a", textSerializerUtf8());
if (addTrailers) {
request.trailers().set("mytrailer", "myvalue");
}
if (h2PriorKnowledge) {
assertThrows(H2StreamResetException.class, () -> client.request(request));
} else {
try (ReservedBlockingHttpConnection reservedConn = client.reserveConnection(request)) {
assertThrows(IOException.class, () -> {
// Either the current request or the next one should fail
reservedConn.request(request);
reservedConn.request(client.get("/"));
});
}
}
}
}
use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientFilterAsyncContext.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientFilterAsyncContext(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer();
final Queue<Throwable> errorQueue = new ConcurrentLinkedQueue<>();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).appendClientFilter(client2 -> new StreamingHttpClientFilter(client2) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return asyncContextTestRequest(errorQueue, delegate, request);
}
}).buildBlocking()) {
final String responseBody = "foo";
HttpResponse response = client.request(client.post("/0").payloadBody(responseBody, textSerializerUtf8()));
assertEquals(responseBody, response.payloadBody(textSerializerUtf8()));
assertNoAsyncErrors(errorQueue);
}
}
use of io.servicetalk.concurrent.api.Single in project servicetalk by apple.
the class HttpConnectionEmptyPayloadTest method headRequestContentEmpty.
@Test
void headRequestContentEmpty() throws Exception {
try (CompositeCloseable closeable = AsyncCloseables.newCompositeCloseable()) {
final int expectedContentLength = 128;
byte[] expectedPayload = new byte[expectedContentLength];
ThreadLocalRandom.current().nextBytes(expectedPayload);
ServerContext serverContext = closeable.merge(HttpServers.forAddress(localAddress(0)).ioExecutor(executionContextRule.ioExecutor()).executionStrategy(offloadNever()).listenStreamingAndAwait((ctx, req, factory) -> {
StreamingHttpResponse resp = factory.ok().payloadBody(from(HEAD.equals(req.method()) ? EMPTY_BUFFER : ctx.executionContext().bufferAllocator().newBuffer(expectedContentLength).writeBytes(expectedPayload)));
resp.addHeader(CONTENT_LENGTH, String.valueOf(expectedContentLength));
return succeeded(resp);
}));
StreamingHttpClient client = closeable.merge(forResolvedAddress(serverHostAndPort(serverContext)).ioExecutor(executionContextRule.ioExecutor()).protocols(h1().maxPipelinedRequests(3).build()).executor(executionContextRule.executor()).executionStrategy(defaultStrategy()).buildStreaming());
StreamingHttpConnection connection = closeable.merge(client.reserveConnection(client.get("/")).toFuture().get());
// Request HEAD, GET, HEAD to verify that we can keep reading data despite a HEAD request providing a hint
// about content-length (and not actually providing the content).
Single<StreamingHttpResponse> response1Single = connection.request(connection.newRequest(HEAD, "/"));
Single<StreamingHttpResponse> response2Single = connection.request(connection.get("/"));
Single<StreamingHttpResponse> response3Single = connection.request(connection.newRequest(HEAD, "/"));
StreamingHttpResponse response = awaitIndefinitelyNonNull(response1Single);
assertEquals(OK, response.status());
CharSequence contentLength = response.headers().get(CONTENT_LENGTH);
assertNotNull(contentLength);
assertEquals(expectedContentLength, parseInt(contentLength.toString()));
// Drain the current response content so we will be able to read the next response.
response.messageBody().ignoreElements().toFuture().get();
response = awaitIndefinitelyNonNull(response2Single);
assertEquals(OK, response.status());
contentLength = response.headers().get(CONTENT_LENGTH);
assertNotNull(contentLength);
assertEquals(expectedContentLength, parseInt(contentLength.toString()));
Buffer buffer = awaitIndefinitelyNonNull(response.payloadBody().collect(() -> connection.connectionContext().executionContext().bufferAllocator().newBuffer(), Buffer::writeBytes));
byte[] actualBytes = new byte[buffer.readableBytes()];
buffer.readBytes(actualBytes);
assertArrayEquals(expectedPayload, actualBytes);
response = awaitIndefinitelyNonNull(response3Single);
assertEquals(OK, response.status());
contentLength = response.headers().get(CONTENT_LENGTH);
assertNotNull(contentLength);
assertEquals(expectedContentLength, parseInt(contentLength.toString()));
response.messageBody().ignoreElements().toFuture().get();
}
}
Aggregations