Search in sources :

Example 6 with Subscriber

use of io.servicetalk.concurrent.SingleSource.Subscriber in project servicetalk by apple.

the class BeforeFinallyHttpOperatorTest method duplicateOnSuccess.

@ParameterizedTest(name = "{displayName} [{index}] discardEventsAfterCancel={0}")
@ValueSource(booleans = { false, true })
void duplicateOnSuccess(boolean discardEventsAfterCancel) {
    AtomicReference<SingleSource.Subscriber<? super StreamingHttpResponse>> subRef = new AtomicReference<>();
    Single<StreamingHttpResponse> original = new Single<StreamingHttpResponse>() {

        @Override
        protected void handleSubscribe(final Subscriber<? super StreamingHttpResponse> subscriber) {
            subRef.set(subscriber);
            subscriber.onSubscribe(IGNORE_CANCEL);
        }
    };
    final ResponseSubscriber subscriber = new ResponseSubscriber();
    toSource(original.liftSync(new BeforeFinallyHttpOperator(beforeFinally, discardEventsAfterCancel))).subscribe(subscriber);
    assertThat("Original Single not subscribed.", subRef.get(), is(notNullValue()));
    assertThat("onSubscribe not called.", subscriber.cancellable, is(notNullValue()));
    final StreamingHttpResponse response = reqRespFactory.newResponse(OK);
    subRef.get().onSuccess(response);
    final StreamingHttpResponse received = subscriber.verifyResponseReceived();
    verifyNoInteractions(beforeFinally);
    final StreamingHttpResponse response2 = reqRespFactory.newResponse(OK);
    try {
        subRef.get().onSuccess(response2);
    } catch (AssertionError e) {
    // silence assert on dupe success when not canceled
    }
    final StreamingHttpResponse received2 = subscriber.verifyResponseReceived();
    // Old response should be preserved.
    assertThat("Duplicate response received.", received2, is(received));
    verifyNoInteractions(beforeFinally);
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) Subscriber(io.servicetalk.concurrent.SingleSource.Subscriber) LegacyTestSingle(io.servicetalk.concurrent.api.LegacyTestSingle) Single(io.servicetalk.concurrent.api.Single) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Subscriber (io.servicetalk.concurrent.SingleSource.Subscriber)6 Single (io.servicetalk.concurrent.api.Single)4 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)3 OK (io.servicetalk.http.api.HttpResponseStatus.OK)2 Collection (java.util.Collection)2 Assertions.fail (org.junit.jupiter.api.Assertions.fail)2 Buffer (io.servicetalk.buffer.api.Buffer)1 ConnectionFactory (io.servicetalk.client.api.ConnectionFactory)1 DelegatingConnectionFactory (io.servicetalk.client.api.DelegatingConnectionFactory)1 Cancellable (io.servicetalk.concurrent.Cancellable)1 IGNORE_CANCEL (io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL)1 Processor (io.servicetalk.concurrent.CompletableSource.Processor)1 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)1 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)1 Completable (io.servicetalk.concurrent.api.Completable)1 LegacyTestSingle (io.servicetalk.concurrent.api.LegacyTestSingle)1 Processors.newCompletableProcessor (io.servicetalk.concurrent.api.Processors.newCompletableProcessor)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 Single.defer (io.servicetalk.concurrent.api.Single.defer)1 Single.failed (io.servicetalk.concurrent.api.Single.failed)1