Search in sources :

Example 21 with TestPublisherSubscriber

use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.

the class ScanWithPublisherTest method terminalThrowsHandled.

private static void terminalThrowsHandled(boolean onComplete, boolean withLifetime) {
    final AtomicInteger finalizations = new AtomicInteger(0);
    PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
    TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
    toSource(scanWithOperator(fromSource(processor), withLifetime, new ScanWithLifetimeMapper<Integer, Integer>() {

        @Nullable
        @Override
        public Integer mapOnNext(@Nullable final Integer next) {
            return next;
        }

        @Nullable
        @Override
        public Integer mapOnError(final Throwable cause) throws Throwable {
            throw cause;
        }

        @Nullable
        @Override
        public Integer mapOnComplete() {
            throw DELIBERATE_EXCEPTION;
        }

        @Override
        public boolean mapTerminal() {
            return true;
        }

        @Override
        public void afterFinally() {
            finalizations.incrementAndGet();
        }
    })).subscribe(subscriber);
    subscriber.awaitSubscription().request(1);
    if (onComplete) {
        processor.onComplete();
    } else {
        processor.onError(DELIBERATE_EXCEPTION);
    }
    assertThat(subscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
    if (withLifetime) {
        assertThat(finalizations.get(), is(1));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PublisherSource(io.servicetalk.concurrent.PublisherSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nullable(javax.annotation.Nullable)

Example 22 with TestPublisherSubscriber

use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.

the class ScanWithPublisherTest method terminalConcatWithDemand.

private static void terminalConcatWithDemand(boolean demandUpFront, boolean onComplete, boolean withLifetime) {
    final AtomicInteger finalizations = new AtomicInteger(0);
    PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
    TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
    toSource(scanWithOperator(fromSource(processor), withLifetime, new ScanWithLifetimeMapper<Integer, Integer>() {

        private int sum;

        @Override
        public Integer mapOnNext(@Nullable final Integer next) {
            if (next != null) {
                sum += next;
            }
            return sum;
        }

        @Override
        public Integer mapOnError(final Throwable cause) {
            return ++sum;
        }

        @Override
        public Integer mapOnComplete() {
            return ++sum;
        }

        @Override
        public boolean mapTerminal() {
            return true;
        }

        @Override
        public void afterFinally() {
            finalizations.incrementAndGet();
        }
    })).subscribe(subscriber);
    Subscription s = subscriber.awaitSubscription();
    s.request(demandUpFront ? 4 : 3);
    processor.onNext(1);
    assertThat(subscriber.takeOnNext(), is(1));
    processor.onNext(2);
    assertThat(subscriber.takeOnNext(), is(3));
    processor.onNext(3);
    assertThat(subscriber.takeOnNext(), is(6));
    if (onComplete) {
        processor.onComplete();
    } else {
        processor.onError(DELIBERATE_EXCEPTION);
    }
    if (!demandUpFront) {
        assertThat(subscriber.pollOnNext(10, MILLISECONDS), is(nullValue()));
        s.request(1);
    }
    assertThat(subscriber.takeOnNext(), is(7));
    subscriber.awaitOnComplete();
    if (withLifetime) {
        assertThat(finalizations.get(), is(1));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PublisherSource(io.servicetalk.concurrent.PublisherSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription)

Example 23 with TestPublisherSubscriber

use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.

the class ScanWithPublisherTest method scanWithNoTerminalMapper.

private static void scanWithNoTerminalMapper(boolean onComplete) {
    PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
    TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
    toSource(fromSource(processor).scanWith(() -> 0, Integer::sum)).subscribe(subscriber);
    Subscription s = subscriber.awaitSubscription();
    s.request(3);
    processor.onNext(1);
    assertThat(subscriber.takeOnNext(), is(1));
    processor.onNext(2);
    assertThat(subscriber.takeOnNext(), is(3));
    processor.onNext(3);
    assertThat(subscriber.takeOnNext(), is(6));
    if (onComplete) {
        processor.onComplete();
        subscriber.awaitOnComplete();
    } else {
        processor.onError(DELIBERATE_EXCEPTION);
        assertThat(subscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PublisherSource(io.servicetalk.concurrent.PublisherSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription)

Example 24 with TestPublisherSubscriber

use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.

the class ScanWithPublisherTest method mapTerminalSignalThrows.

private static void mapTerminalSignalThrows(boolean onComplete, boolean withLifetime) {
    final AtomicInteger finalizations = new AtomicInteger(0);
    PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
    TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
    toSource(scanWithOperator(fromSource(processor), withLifetime, new ScanWithLifetimeMapper<Integer, Integer>() {

        @Nullable
        @Override
        public Integer mapOnNext(@Nullable final Integer next) {
            return null;
        }

        @Nullable
        @Override
        public Integer mapOnError(final Throwable cause) {
            return null;
        }

        @Nullable
        @Override
        public Integer mapOnComplete() {
            return null;
        }

        @Override
        public boolean mapTerminal() {
            throw DELIBERATE_EXCEPTION;
        }

        @Override
        public void afterFinally() {
            finalizations.incrementAndGet();
        }
    })).subscribe(subscriber);
    subscriber.awaitSubscription();
    if (onComplete) {
        processor.onComplete();
    } else {
        processor.onError(new DeliberateException());
    }
    assertThat(subscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
    if (withLifetime) {
        assertThat(finalizations.get(), is(1));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PublisherSource(io.servicetalk.concurrent.PublisherSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Nullable(javax.annotation.Nullable)

Example 25 with TestPublisherSubscriber

use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.

the class CompletableToPublisherTest method publishOnOriginalIsPreservedOnInvalidRequestN.

@Test
@Disabled("The Publisher subscriber is now not offloaded")
void publishOnOriginalIsPreservedOnInvalidRequestN() throws Exception {
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
    TestCompletable completable = new TestCompletable();
    CountDownLatch analyzed = publishOnOriginalIsPreserved0(errors, subscriber, completable);
    subscriber.awaitSubscription().request(-1);
    analyzed.await();
    assertThat("Unexpected errors observed: " + errors, errors, hasSize(0));
    Throwable err = subscriber.awaitOnError();
    assertThat("Wrong error received.", err, is(instanceOf(IllegalArgumentException.class)));
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)30 Test (org.junit.jupiter.api.Test)16 CountDownLatch (java.util.concurrent.CountDownLatch)13 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 PublisherSource (io.servicetalk.concurrent.PublisherSource)9 Nullable (javax.annotation.Nullable)7 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)6 Buffer (io.servicetalk.buffer.api.Buffer)5 TestCompletable (io.servicetalk.concurrent.api.TestCompletable)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)4 ExecutorService (java.util.concurrent.ExecutorService)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)3 Thread.currentThread (java.lang.Thread.currentThread)3 ArrayList (java.util.ArrayList)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 LegacyTestSingle (io.servicetalk.concurrent.api.LegacyTestSingle)2