Search in sources :

Example 26 with TestPublisherSubscriber

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

the class CompletableToPublisherTest method subscribeOnOriginalIsPreserved.

@Test
void subscribeOnOriginalIsPreserved() throws Exception {
    final Thread testThread = currentThread();
    final CountDownLatch analyzed = new CountDownLatch(1);
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    TestCompletable completable = new TestCompletable.Builder().disableAutoOnSubscribe().build();
    TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
    toSource(completable.beforeCancel(() -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked cancel. Thread: " + currentThread()));
        }
    }).afterCancel(analyzed::countDown).subscribeOn(EXEC.executor()).<String>toPublisher()).subscribe(subscriber);
    TestCancellable cancellable = new TestCancellable();
    // waits till subscribed.
    completable.onSubscribe(cancellable);
    assertThat("Completable not subscribed.", completable.isSubscribed(), is(true));
    subscriber.awaitSubscription().cancel();
    analyzed.await();
    assertThat("Completable did not get a cancel.", cancellable.isCancelled(), is(true));
    assertThat("Unexpected errors observed: " + errors, errors, hasSize(0));
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Thread.currentThread(java.lang.Thread.currentThread) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) Test(org.junit.jupiter.api.Test)

Example 27 with TestPublisherSubscriber

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

the class CompletableToPublisherTest method publishOnOriginalIsPreservedOnError.

@Test
void publishOnOriginalIsPreservedOnError() throws Exception {
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
    TestCompletable completable = new TestCompletable();
    CountDownLatch analyzed = publishOnOriginalIsPreserved0(errors, subscriber, completable);
    completable.onError(DELIBERATE_EXCEPTION);
    analyzed.await();
    assertThat("Unexpected errors observed: " + errors, errors, hasSize(0));
    Throwable err = subscriber.awaitOnError();
    assertThat("Wrong error received.", err, is(sameInstance(DELIBERATE_EXCEPTION)));
}
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)

Example 28 with TestPublisherSubscriber

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

the class SingleToPublisherTest method subscribeOnOriginalIsPreserved.

@Test
void subscribeOnOriginalIsPreserved() throws Exception {
    final Thread testThread = currentThread();
    final CountDownLatch analyzed = new CountDownLatch(1);
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    TestSingle<String> single = new TestSingle.Builder<String>().disableAutoOnSubscribe().build();
    TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
    toSource(single.beforeCancel(() -> {
        if (currentThread() == testThread) {
            errors.add(new AssertionError("Invalid thread invoked cancel. Thread: " + currentThread()));
        }
    }).afterCancel(analyzed::countDown).subscribeOn(EXEC.executor()).toPublisher()).subscribe(subscriber);
    TestCancellable cancellable = new TestCancellable();
    // waits till subscribed.
    single.onSubscribe(cancellable);
    assertThat("Single not subscribed.", single.isSubscribed(), is(true));
    subscriber.awaitSubscription().cancel();
    analyzed.await();
    assertThat("Single did not get a cancel.", cancellable.isCancelled(), is(true));
    assertNoAsyncErrors(errors);
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Thread.currentThread(java.lang.Thread.currentThread) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) Test(org.junit.jupiter.api.Test)

Example 29 with TestPublisherSubscriber

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

the class PublisherBufferConcurrencyTest method addingAndBoundaryEmission.

@Test
void addingAndBoundaryEmission() throws Exception {
    TestPublisher<Integer> original = new TestPublisher<>();
    TestPublisher<Accumulator<Integer, Integer>> boundaries = new TestPublisher<>();
    TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
    CountDownLatch waitForBoundary = new CountDownLatch(1);
    CountDownLatch waitForAdd = new CountDownLatch(1);
    Accumulator<Integer, Integer> accumulator = new Accumulator<Integer, Integer>() {

        private int added;

        @Override
        public void accumulate(@Nullable final Integer integer) {
            waitForAdd.countDown();
            try {
                waitForBoundary.await();
                if (integer == null) {
                    return;
                }
                added = integer;
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throwException(e);
            }
        }

        @Override
        public Integer finish() {
            return added;
        }
    };
    toSource(original.buffer(new BufferStrategy<Integer, Accumulator<Integer, Integer>, Integer>() {

        @Override
        public Publisher<Accumulator<Integer, Integer>> boundaries() {
            return boundaries;
        }

        @Override
        public int bufferSizeHint() {
            return 8;
        }
    })).subscribe(subscriber);
    subscriber.awaitSubscription().request(1);
    // initial boundary
    boundaries.onNext(accumulator);
    assertThat(subscriber.pollOnNext(10, MILLISECONDS), is(nullValue()));
    CountDownLatch waitForOnNextReturn = new CountDownLatch(1);
    EXEC.executor().submit(() -> original.onNext(1)).beforeFinally(waitForOnNextReturn::countDown).subscribe();
    waitForAdd.await();
    subscriber.awaitSubscription().request(1);
    boundaries.onNext(new SummingAccumulator());
    waitForBoundary.countDown();
    waitForOnNextReturn.await();
    // Last accumulator will be overwritten by add()
    boundaries.onNext(new SummingAccumulator());
    assertThat("Unexpected result.", subscriber.takeOnNext(), is(1));
    original.onComplete();
    // Boundary has to complete for terminal to be emitted
    boundaries.onNext(new SummingAccumulator());
    // empty accumulator
    assertThat("Unexpected result.", subscriber.takeOnNext(), is(0));
    subscriber.awaitOnComplete();
}
Also used : Accumulator(io.servicetalk.concurrent.api.BufferStrategy.Accumulator) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(javax.annotation.Nullable) Test(org.junit.jupiter.api.Test)

Example 30 with TestPublisherSubscriber

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

the class NettyChannelPublisherTest method testCancelThenResubscribeDeliversErrorAndNotQueuedData.

private void testCancelThenResubscribeDeliversErrorAndNotQueuedData(boolean doChannelRead, boolean setupFireReadOnClose) throws Exception {
    if (setupFireReadOnClose) {
        setupFireReadOnCloseEvents();
    }
    TestPublisherSubscriber<Integer> subscriber1 = new TestPublisherSubscriber<>();
    TestPublisherSubscriber<Integer> subscriber2 = new TestPublisherSubscriber<>();
    toSource(publisher).subscribe(subscriber1);
    Subscription subscription1 = subscriber1.awaitSubscription();
    subscription1.request(1);
    assertFalse(channel.writeInbound(1));
    Integer next = subscriber1.takeOnNext();
    assertThat(next, is(1));
    // this write should be queued, because there isn't any requestN demand.
    assertFalse(channel.writeInbound(2));
    // cancel of active subscription should clear the queue and fail future Subscribers.
    subscription1.cancel();
    if (doChannelRead) {
        try {
            assertFalse(channel.writeInbound(3));
        } catch (Exception e) {
            assertThat(e, instanceOf(ClosedChannelException.class));
            return;
        }
    }
    toSource(publisher).subscribe(subscriber2);
    subscriber2.awaitSubscription().request(Long.MAX_VALUE);
    assertThat(subscriber2.pollAllOnNext(), is(empty()));
    assertThat(subscriber2.awaitOnError(), is(instanceOf(ClosedChannelException.class)));
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) DuplicateSubscribeException(io.servicetalk.concurrent.internal.DuplicateSubscribeException) ClosedChannelException(java.nio.channels.ClosedChannelException)

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