use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class BeforeFinallyHttpOperatorTest method payloadComplete.
@ParameterizedTest(name = "{displayName} [{index}] discardEventsAfterCancel={0}")
@ValueSource(booleans = { false, true })
void payloadComplete(boolean discardEventsAfterCancel) {
TestPublisher<Buffer> payload = new TestPublisher<>();
LegacyTestSingle<StreamingHttpResponse> responseSingle = new LegacyTestSingle<>(true);
final ResponseSubscriber subscriber = new ResponseSubscriber();
toSource(responseSingle.liftSync(new BeforeFinallyHttpOperator(beforeFinally, discardEventsAfterCancel))).subscribe(subscriber);
assertThat("onSubscribe not called.", subscriber.cancellable, is(notNullValue()));
final StreamingHttpResponse response = reqRespFactory.ok().payloadBody(payload);
responseSingle.onSuccess(response);
verifyNoInteractions(beforeFinally);
responseSingle.verifyNotCancelled();
subscriber.verifyResponseReceived();
assert subscriber.response != null;
subscriber.response.payloadBody().forEach(chunk -> {
// ignore
});
payload.onComplete();
verify(beforeFinally).onComplete();
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class RetryRequestWithNonRepeatablePayloadTest method setUp.
private void setUp(HttpProtocol protocol, TestPublisher<Buffer> payloadBody, Queue<Throwable> errors, boolean offloading) {
protocol(protocol.config);
ChannelOutboundHandler firstWriteHandler = new FailingFirstWriteHandler();
connectionFactoryFilter(ConnectionFactoryFilter.withStrategy(factory -> new DelegatingConnectionFactory<InetSocketAddress, FilterableStreamingHttpConnection>(factory) {
@Override
public Single<FilterableStreamingHttpConnection> newConnection(InetSocketAddress address, @Nullable TransportObserver observer) {
return delegate().newConnection(address, observer).map(c -> {
final Channel channel = ((NettyConnectionContext) c.connectionContext()).nettyChannel();
if (protocol == HTTP_1) {
// Insert right before HttpResponseDecoder to avoid seeing failed frames on wire logs
channel.pipeline().addBefore(HttpRequestEncoder.class.getSimpleName() + "#0", null, firstWriteHandler);
} else if (protocol == HTTP_2) {
// Insert right before Http2MultiplexHandler to avoid failing connection-level frames and
// seeing failed stream frames on frame/wire logs
channel.pipeline().addBefore(Http2MultiplexHandler.class.getSimpleName() + "#0", null, firstWriteHandler);
}
return new StreamingHttpConnectionFilter(c) {
@Override
public Single<StreamingHttpResponse> request(StreamingHttpRequest request) {
return delegate().request(request).whenOnError(t -> {
try {
assertThat("Unexpected exception type", t, instanceOf(RetryableException.class));
assertThat("Unexpected exception type", t.getCause(), instanceOf(DeliberateException.class));
assertThat("Unexpected subscribe to payload body", payloadBody.isSubscribed(), is(false));
} catch (Throwable error) {
errors.add(error);
}
});
}
};
});
}
}, ExecutionStrategy.offloadNone()));
setUp(offloading ? CACHED : IMMEDIATE, offloading ? CACHED_SERVER : IMMEDIATE);
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class PublisherAsInputStreamTest method checkAvailableReturnsCorrectlyWithPrefetch.
@Test
void checkAvailableReturnsCorrectlyWithPrefetch() throws IOException {
TestPublisher<String> testPublisher = new TestPublisher<>();
InputStream stream = testPublisher.toInputStream(str -> str.getBytes(US_ASCII));
assertThat("Unexpected available return type.", stream.available(), is(0));
testPublisher.onNext("1234");
assertThat("Unexpected available return type.", stream.available(), is(0));
byte[] data = new byte[2];
int read = stream.read(data, 0, 2);
assertThat("Unexpected number of bytes read.", read, is(2));
assertThat("Unexpected bytes read.", bytesToCharArray(data, read), equalTo(new Character[] { '1', '2' }));
assertThat("Unexpected available return type.", stream.available(), is(2));
}
use of io.servicetalk.concurrent.api.TestPublisher in project servicetalk by apple.
the class NettyPipelinedConnectionTest method writeSubscribeThrowsLetsSubsequentRequestsThrough.
@Test
void writeSubscribeThrowsLetsSubsequentRequestsThrough() {
AtomicBoolean firstReadOperation = new AtomicBoolean();
TestPublisher<Integer> mockReadPublisher1 = new TestPublisher<>();
TestPublisher<Integer> mockReadPublisher2 = new TestPublisher<>();
@SuppressWarnings("unchecked") NettyConnection<Integer, Integer> mockConnection = mock(NettyConnection.class);
doAnswer((Answer<Publisher<Integer>>) invocation -> firstReadOperation.compareAndSet(false, true) ? mockReadPublisher1 : mockReadPublisher2).when(mockConnection).read();
doAnswer((Answer<Completable>) invocation -> new Completable() {
@Override
protected void handleSubscribe(final CompletableSource.Subscriber subscriber) {
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());
requester = new NettyPipelinedConnection<>(mockConnection, 2);
toSource(requester.write(writePublisher1)).subscribe(readSubscriber);
toSource(requester.write(writePublisher2)).subscribe(readSubscriber2);
Subscription readSubscription = readSubscriber.awaitSubscription();
readSubscription.request(1);
assertTrue(mockReadPublisher1.isSubscribed());
mockReadPublisher1.onError(newSecondException());
assertThat(readSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
assertFalse(writePublisher1.isSubscribed());
verifySecondRequestProcessed(mockReadPublisher2, mockConnection);
}
Aggregations