Search in sources :

Example 1 with DelayedSubscription

use of io.servicetalk.concurrent.internal.DelayedSubscription in project servicetalk by apple.

the class PublisherBufferTest method originalSourceIsRetriedIfSubscriberThrows.

@Test
void originalSourceIsRetriedIfSubscriberThrows() {
    TestPublisher<Accumulator<Integer, Integer>> bPublisher = new TestPublisher<>();
    DelayedSubscription bSubscription = new DelayedSubscription();
    AtomicReference<TerminalNotification> terminal = new AtomicReference<>();
    BlockingQueue<Integer> items = new LinkedBlockingDeque<>();
    BlockingQueue<Integer> buffers = new LinkedBlockingDeque<>();
    AtomicInteger counter = new AtomicInteger();
    toSource(defer(() -> from(counter.incrementAndGet())).whenOnNext(items::add).retry((i, t) -> i < 3 && t == DELIBERATE_EXCEPTION).buffer(new TestBufferStrategy(bPublisher, 1))).subscribe(new Subscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            bSubscription.delayedSubscription(s);
            bSubscription.request(1);
        }

        @Override
        public void onNext(@Nullable Integer integer) {
            assert integer != null;
            buffers.add(integer);
            throw DELIBERATE_EXCEPTION;
        }

        @Override
        public void onError(Throwable t) {
            terminal.set(error(t));
        }

        @Override
        public void onComplete() {
            terminal.set(complete());
        }
    });
    // it will generate a new boundary on each accumulation
    bPublisher.onNext(new SumAccumulator(bPublisher));
    assertThat(items, hasSize(1));
    assertThat(items, contains(1));
    assertThat(buffers, hasSize(1));
    assertThat(buffers, contains(1));
    bSubscription.request(MAX_VALUE);
    assertThat(items, hasSize(3));
    assertThat(items, contains(1, 2, 3));
    assertThat(buffers, hasSize(3));
    assertThat(buffers, contains(1, 2, 3));
    assertThat(terminal.get().cause(), is(DELIBERATE_EXCEPTION));
}
Also used : Accumulator(io.servicetalk.concurrent.api.BufferStrategy.Accumulator) Publisher.never(io.servicetalk.concurrent.api.Publisher.never) TerminalNotification.complete(io.servicetalk.concurrent.internal.TerminalNotification.complete) MAX_VALUE(java.lang.Long.MAX_VALUE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscriber(io.servicetalk.concurrent.PublisherSource.Subscriber) TerminalNotification(io.servicetalk.concurrent.internal.TerminalNotification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.nullValue(org.hamcrest.Matchers.nullValue) DelayedSubscription(io.servicetalk.concurrent.internal.DelayedSubscription) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Publisher.defer(io.servicetalk.concurrent.api.Publisher.defer) Nullable(javax.annotation.Nullable) Accumulator(io.servicetalk.concurrent.api.BufferStrategy.Accumulator) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) Matchers.empty(org.hamcrest.Matchers.empty) BlockingQueue(java.util.concurrent.BlockingQueue) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) TerminalNotification.error(io.servicetalk.concurrent.internal.TerminalNotification.error) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matcher(org.hamcrest.Matcher) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Matchers.is(org.hamcrest.Matchers.is) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) TerminalNotification(io.servicetalk.concurrent.internal.TerminalNotification) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DelayedSubscription(io.servicetalk.concurrent.internal.DelayedSubscription) DelayedSubscription(io.servicetalk.concurrent.internal.DelayedSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with DelayedSubscription

use of io.servicetalk.concurrent.internal.DelayedSubscription in project servicetalk by apple.

the class BufferStrategiesTest method forCountOneDemandIsRespected.

@Test
void forCountOneDemandIsRespected() {
    DelayedSubscription subscription = new DelayedSubscription();
    AtomicReference<TerminalNotification> terminated = new AtomicReference<>();
    BlockingQueue<Iterable<Integer>> accumulations = new LinkedBlockingDeque<>();
    toSource(range(1, 6).buffer(forCountOrTime(2, ofDays(1)))).subscribe(new Subscriber<Iterable<Integer>>() {

        @Override
        public void onSubscribe(final Subscription s) {
            subscription.delayedSubscription(s);
            subscription.request(1);
        }

        @Override
        public void onNext(@Nullable final Iterable<Integer> integers) {
            assert integers != null;
            accumulations.add(integers);
        }

        @Override
        public void onError(final Throwable t) {
            terminated.set(error(t));
        }

        @Override
        public void onComplete() {
            terminated.set(complete());
        }
    });
    assertThat("Unexpected number of emitted accumulators", accumulations, hasSize(1));
    assertThat("Unexpected accumulators", accumulations, contains(asList(1, 2)));
    assertThat("Unexpected termination", terminated.get(), is(nullValue()));
    subscription.request(1);
    assertThat("Unexpected number of emitted accumulators", accumulations, hasSize(2));
    assertThat("Unexpected accumulators", accumulations, contains(asList(1, 2), asList(3, 4)));
    assertThat("Unexpected termination", terminated.get(), is(nullValue()));
    subscription.request(1);
    assertThat("Unexpected number of emitted accumulators", accumulations, hasSize(3));
    assertThat("Unexpected accumulators", accumulations, contains(asList(1, 2), asList(3, 4), singletonList(5)));
    assertThat("Unexpected termination", terminated.get(), is(complete()));
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Matchers.emptyIterable(org.hamcrest.Matchers.emptyIterable) TerminalNotification(io.servicetalk.concurrent.internal.TerminalNotification) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DelayedSubscription(io.servicetalk.concurrent.internal.DelayedSubscription) DelayedSubscription(io.servicetalk.concurrent.internal.DelayedSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test)

Example 3 with DelayedSubscription

use of io.servicetalk.concurrent.internal.DelayedSubscription in project servicetalk by apple.

the class PublisherProcessor method handleSubscribe.

@Override
protected void handleSubscribe(final Subscriber<? super T> subscriber) {
    final DelayedSubscription delayedSubscription = new DelayedSubscription();
    try {
        subscriber.onSubscribe(delayedSubscription);
    } catch (Throwable t) {
        handleExceptionFromOnSubscribe(subscriber, t);
        return;
    }
    if (consumerUpdater.compareAndSet(this, null, new SubscriberProcessorSignalsConsumer<>(subscriber))) {
        try {
            delayedSubscription.delayedSubscription(this);
            tryEmitSignals();
        } catch (Throwable t) {
            LOGGER.error("Unexpected error while delivering signals to the subscriber {}", subscriber, t);
        }
    } else {
        ProcessorSignalsConsumer<? super T> existingConsumer = this.consumer;
        assert existingConsumer != null;
        @SuppressWarnings("unchecked") final Subscriber<? super T> existingSubscriber = existingConsumer instanceof PublisherProcessor.SubscriberProcessorSignalsConsumer ? ((SubscriberProcessorSignalsConsumer<T>) existingConsumer).subscriber : null;
        safeOnError(subscriber, new DuplicateSubscribeException(existingSubscriber, subscriber));
    }
}
Also used : DuplicateSubscribeException(io.servicetalk.concurrent.internal.DuplicateSubscribeException) DelayedSubscription(io.servicetalk.concurrent.internal.DelayedSubscription)

Aggregations

DelayedSubscription (io.servicetalk.concurrent.internal.DelayedSubscription)3 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)2 TerminalNotification (io.servicetalk.concurrent.internal.TerminalNotification)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.jupiter.api.Test)2 Subscriber (io.servicetalk.concurrent.PublisherSource.Subscriber)1 Accumulator (io.servicetalk.concurrent.api.BufferStrategy.Accumulator)1 Publisher.defer (io.servicetalk.concurrent.api.Publisher.defer)1 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)1 Publisher.never (io.servicetalk.concurrent.api.Publisher.never)1 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)1 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)1 DuplicateSubscribeException (io.servicetalk.concurrent.internal.DuplicateSubscribeException)1 TerminalNotification.complete (io.servicetalk.concurrent.internal.TerminalNotification.complete)1 TerminalNotification.error (io.servicetalk.concurrent.internal.TerminalNotification.error)1 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)1 MAX_VALUE (java.lang.Long.MAX_VALUE)1 BlockingQueue (java.util.concurrent.BlockingQueue)1