use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class HttpLifecycleObserverTest method testClientCancelsRequestAfterResponse.
@ParameterizedTest(name = "{displayName} [{index}] protocol={0}")
@EnumSource(HttpProtocol.class)
void testClientCancelsRequestAfterResponse(HttpProtocol protocol) throws Exception {
TestPublisher<Buffer> serverResponsePayload = new TestPublisher<>();
serviceFilterFactory(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(HttpServiceContext ctx, StreamingHttpRequest request, StreamingHttpResponseFactory responseFactory) {
return request.payloadBody().ignoreElements().concat(succeeded(responseFactory.ok().payloadBody(serverResponsePayload)));
}
});
setUp(protocol);
StreamingHttpConnection connection = streamingHttpConnection();
StreamingHttpRequest request = connection.post("/").payloadBody(Publisher.from(CONTENT.duplicate())).transform(// adds empty trailers
new StatelessTrailersTransformer<>());
StreamingHttpResponse response = connection.request(request).toFuture().get();
assertResponse(response, protocol.version, OK);
Future<Collection<Buffer>> payload = response.payloadBody().toFuture();
payload.cancel(true);
if (protocol == HttpProtocol.HTTP_1) {
// wait for cancellation to close the connection:
connection.onClose().toFuture().get();
}
// try to write server content to trigger write failure and close the server-side connection:
serverResponsePayload.onNext(CONTENT.duplicate());
bothTerminate.await();
clientInOrder.verify(clientLifecycleObserver).onNewExchange();
clientInOrder.verify(clientExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
clientInOrder.verify(clientExchangeObserver).onRequest(any(StreamingHttpRequest.class));
clientInOrder.verify(clientExchangeObserver).onResponse(any(StreamingHttpResponse.class));
clientInOrder.verify(clientResponseObserver).onResponseCancel();
clientRequestInOrder.verify(clientRequestObserver).onRequestData(any(Buffer.class));
clientRequestInOrder.verify(clientRequestObserver).onRequestTrailers(any(HttpHeaders.class));
clientRequestInOrder.verify(clientRequestObserver).onRequestComplete();
clientInOrder.verify(clientExchangeObserver).onExchangeFinally();
verifyNoMoreInteractions(clientLifecycleObserver, clientExchangeObserver, clientRequestObserver, clientResponseObserver);
serverInOrder.verify(serverLifecycleObserver).onNewExchange();
serverInOrder.verify(serverExchangeObserver).onConnectionSelected(any(ConnectionInfo.class));
serverInOrder.verify(serverExchangeObserver).onRequest(any(StreamingHttpRequest.class));
serverInOrder.verify(serverExchangeObserver).onResponse(any(StreamingHttpResponse.class));
verify(serverResponseObserver, atMostOnce()).onResponseData(any(Buffer.class));
serverInOrder.verify(serverResponseObserver).onResponseCancel();
serverRequestInOrder.verify(serverRequestObserver).onRequestData(any(Buffer.class));
serverRequestInOrder.verify(serverRequestObserver).onRequestComplete();
serverInOrder.verify(serverExchangeObserver).onExchangeFinally();
verifyNoMoreInteractions(serverLifecycleObserver, serverExchangeObserver, serverRequestObserver, serverResponseObserver);
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class DefaultSerializerSerializationTest method applySerializationForPublisherWithTypeAndEstimator.
@Test
void applySerializationForPublisherWithTypeAndEstimator() {
TestPublisher<String> source = new TestPublisher<>();
final Publisher<Buffer> serialized = factory.serialize(source, allocator, String.class, sizeEstimator);
TestPublisherSubscriber<Buffer> subscriber = new TestPublisherSubscriber<>();
toSource(serialized).subscribe(subscriber);
subscriber.awaitSubscription().request(2);
verify(provider).getSerializer(String.class);
Buffer expected1 = verifySerializedBufferWithSizes(source, "Hello", 1);
assertThat(subscriber.takeOnNext(), is(expected1));
Buffer expected2 = verifySerializedBufferWithSizes(source, "Hello", 2);
assertThat(subscriber.takeOnNext(), is(expected2));
source.onComplete();
subscriber.awaitOnComplete();
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class NettyPipelinedConnectionTest method writeThrowsClosesConnection.
@Test
void writeThrowsClosesConnection() {
TestPublisher<Integer> mockReadPublisher2 = new TestPublisher<>();
@SuppressWarnings("unchecked") NettyConnection<Integer, Integer> mockConnection = mock(NettyConnection.class);
doAnswer((Answer<Publisher<Integer>>) invocation -> mockReadPublisher2).when(mockConnection).read();
doAnswer((Answer<Completable>) invocation -> {
throw DELIBERATE_EXCEPTION;
}).when(mockConnection).write(eq(writePublisher1), any(), any());
doAnswer((Answer<Completable>) invocation -> {
Publisher<Integer> writePub = invocation.getArgument(0);
return writePub.ignoreElements();
}).when(mockConnection).write(eq(writePublisher2), any(), any());
when(mockConnection.closeAsync()).thenReturn(completed());
requester = new NettyPipelinedConnection<>(mockConnection, 2);
toSource(requester.write(writePublisher1)).subscribe(readSubscriber);
toSource(requester.write(writePublisher2)).subscribe(readSubscriber2);
Subscription readSubscription = readSubscriber.awaitSubscription();
readSubscription.request(1);
assertThat(readSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
assertFalse(writePublisher1.isSubscribed());
verify(mockConnection).closeAsync();
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class JdkFlowAdaptersTest method toFlowCancel.
@Test
void toFlowCancel() {
TestPublisher<Integer> stPublisher = new TestPublisher<>();
Subscriber<Integer> subscriber = toFlowPublisherAndSubscribe(stPublisher);
TestSubscription subscription = new TestSubscription();
stPublisher.onSubscribe(subscription);
assertThat("Source not subscribed.", stPublisher.isSubscribed(), is(true));
ArgumentCaptor<Subscription> subscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
verify(subscriber).onSubscribe(subscriptionCaptor.capture());
subscriptionCaptor.getValue().cancel();
assertThat("Subscription not cancelled.", subscription.isCancelled(), is(true));
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class AbstractTimeoutHttpFilterTest method subscribeToPayloadBodyAfterTimeout.
@Test
void subscribeToPayloadBodyAfterTimeout() {
Duration timeout = ofMillis(100L);
TestPublisher<Buffer> payloadBody = new TestPublisher<>();
AtomicReference<StreamingHttpResponse> response = new AtomicReference<>();
StepVerifiers.create(applyFilter(timeout, true, defaultStrategy(), responseWith(payloadBody))).expectSuccessConsumed(response::set).verify();
// Subscribe to payload body after timeout
StepVerifiers.create(immediate().timer(timeout.plusMillis(5L)).concat(response.get().payloadBody())).expectError(TimeoutException.class).verify();
assertThat("No subscribe for payload body", payloadBody.isSubscribed(), is(true));
}
Aggregations