use of io.servicetalk.concurrent.api.TestSubscription in project servicetalk by apple.
the class ReactiveStreamsAdaptersTest method completableToRSCancel.
@Test
void completableToRSCancel() {
TestCompletable stCompletable = new TestCompletable();
Subscriber<Integer> subscriber = toRSPublisherAndSubscribe(stCompletable);
TestSubscription subscription = new TestSubscription();
stCompletable.onSubscribe(subscription);
assertThat("Source not subscribed.", stCompletable.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.TestSubscription in project servicetalk by apple.
the class ReactiveStreamsAdaptersTest method toRSCancel.
@Test
void toRSCancel() {
TestPublisher<Integer> stPublisher = new TestPublisher<>();
Subscriber<Integer> subscriber = toRSPublisherAndSubscribe(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.TestSubscription in project servicetalk by apple.
the class BeforeFinallyHttpOperatorTest method cancelWhileDeliveringPayloadThenTerminate.
@ParameterizedTest(name = "{displayName} [{index}] fromOnNext={0} payloadTerminal={1}")
@MethodSource("booleanTerminalNotification")
void cancelWhileDeliveringPayloadThenTerminate(boolean fromOnNext, TerminalNotification payloadTerminal) {
TestPublisher<Buffer> payload = new TestPublisher.Builder<Buffer>().disableAutoOnSubscribe().build();
TestSubscription payloadSubscription = new TestSubscription();
LegacyTestSingle<StreamingHttpResponse> responseSingle = new LegacyTestSingle<>(true);
final ResponseSubscriber subscriber = new ResponseSubscriber();
toSource(responseSingle.liftSync(new BeforeFinallyHttpOperator(beforeFinally, true))).subscribe(subscriber);
assertThat("onSubscribe not called.", subscriber.cancellable, is(notNullValue()));
responseSingle.onSuccess(reqRespFactory.ok().payloadBody(payload));
verifyNoInteractions(beforeFinally);
responseSingle.verifyNotCancelled();
subscriber.verifyResponseReceived();
assert subscriber.response != null;
BlockingQueue<Buffer> receivedPayload = new LinkedBlockingDeque<>();
AtomicReference<TerminalNotification> subscriberTerminal = new AtomicReference<>();
toSource(subscriber.response.payloadBody()).subscribe(new PublisherSource.Subscriber<Buffer>() {
@Nullable
private Subscription subscription;
@Override
public void onSubscribe(final Subscription subscription) {
this.subscription = subscription;
subscription.request(1L);
}
@Override
public void onNext(@Nullable final Buffer buffer) {
assert buffer != null;
receivedPayload.add(buffer);
if (fromOnNext) {
assert subscription != null;
subscription.cancel();
}
if (payloadTerminal.cause() == null) {
payload.onComplete();
} else {
payload.onError(payloadTerminal.cause());
}
}
@Override
public void onError(final Throwable t) {
subscriberTerminal.set(TerminalNotification.error(t));
cancelFromTerminal();
}
@Override
public void onComplete() {
subscriberTerminal.set(TerminalNotification.complete());
cancelFromTerminal();
}
private void cancelFromTerminal() {
if (!fromOnNext) {
assert subscription != null;
subscription.cancel();
}
}
});
payload.onSubscribe(payloadSubscription);
assertThat("Payload was prematurely cancelled", payloadSubscription.isCancelled(), is(false));
payload.onNext(EMPTY_BUFFER);
assertThat("Payload was not cancelled", payloadSubscription.isCancelled(), is(true));
assertThat("Unexpected payload body items", receivedPayload, contains(EMPTY_BUFFER));
assertThat("Unexpected payload body termination", subscriberTerminal.get(), equalTo(payloadTerminal));
if (payloadTerminal.cause() == null) {
verify(beforeFinally).onComplete();
} else {
verify(beforeFinally).onError(payloadTerminal.cause());
}
verifyNoMoreInteractions(beforeFinally);
}
use of io.servicetalk.concurrent.api.TestSubscription in project servicetalk by apple.
the class BeforeFinallyHttpOperatorTest method cancelAfterOnNextThenTerminate.
@ParameterizedTest(name = "{displayName} [{index}] discardEventsAfterCancel={0} payloadTerminal={1}")
@MethodSource("booleanTerminalNotification")
void cancelAfterOnNextThenTerminate(boolean discardEventsAfterCancel, TerminalNotification payloadTerminal) {
TestPublisher<Buffer> payload = new TestPublisher.Builder<Buffer>().disableAutoOnSubscribe().build();
TestSubscription payloadSubscription = new TestSubscription();
TestPublisherSubscriber<Buffer> payloadSubscriber = new TestPublisherSubscriber<>();
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()));
responseSingle.onSuccess(reqRespFactory.ok().payloadBody(payload));
verifyNoInteractions(beforeFinally);
responseSingle.verifyNotCancelled();
subscriber.verifyResponseReceived();
assert subscriber.response != null;
toSource(subscriber.response.payloadBody()).subscribe(payloadSubscriber);
payload.onSubscribe(payloadSubscription);
payloadSubscriber.awaitSubscription().request(MAX_VALUE);
payload.onNext(EMPTY_BUFFER);
if (payloadTerminal.cause() == null) {
payload.onComplete();
} else {
payload.onError(payloadTerminal.cause());
}
assertThat("Unexpected payload body items", payloadSubscriber.pollAllOnNext(), contains(EMPTY_BUFFER));
if (payloadTerminal.cause() == null) {
payloadSubscriber.awaitOnComplete();
} else {
assertThat(payloadSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
}
assertThat("Payload was prematurely cancelled", payloadSubscription.isCancelled(), is(false));
payloadSubscriber.awaitSubscription().cancel();
assertThat("Payload was not cancelled", payloadSubscription.isCancelled(), is(true));
}
use of io.servicetalk.concurrent.api.TestSubscription in project servicetalk by apple.
the class RetryWhenTest method testCancelBeforeRetry.
@Test
void testCancelBeforeRetry() {
final TestSubscription subscription = new TestSubscription();
source.onSubscribe(subscription);
subscriber.awaitSubscription().request(2);
source.onNext(1, 2);
assertThat(subscriber.takeOnNext(2), contains(1, 2));
subscriber.awaitSubscription().cancel();
source.onComplete();
assertTrue(subscription.isCancelled());
}
Aggregations