Search in sources :

Example 16 with TestSubscription

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));
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 17 with TestSubscription

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));
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 18 with TestSubscription

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);
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) PublisherSource(io.servicetalk.concurrent.PublisherSource) TerminalNotification(io.servicetalk.concurrent.internal.TerminalNotification) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Nullable(javax.annotation.Nullable) LegacyTestSingle(io.servicetalk.concurrent.api.LegacyTestSingle) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 19 with TestSubscription

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));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) LegacyTestSingle(io.servicetalk.concurrent.api.LegacyTestSingle) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with TestSubscription

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());
}
Also used : TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Aggregations

TestSubscription (io.servicetalk.concurrent.api.TestSubscription)26 Test (org.junit.jupiter.api.Test)19 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)7 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)7 Buffer (io.servicetalk.buffer.api.Buffer)4 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)4 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)4 ScalarValueSubscription (io.servicetalk.concurrent.internal.ScalarValueSubscription)4 HttpExecutionStrategies.defaultStrategy (io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy)4 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)4 ServerContext (io.servicetalk.transport.api.ServerContext)4 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)4 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)4 ClientGroup (io.servicetalk.client.api.ClientGroup)3 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)3 AVAILABLE (io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE)3 DefaultPartitionAttributesBuilder (io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder)3 PartitionAttributes (io.servicetalk.client.api.partition.PartitionAttributes)3 PartitionAttributesBuilder (io.servicetalk.client.api.partition.PartitionAttributesBuilder)3 PartitionedServiceDiscovererEvent (io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent)3