Search in sources :

Example 6 with TestPublisherSubscriber

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

the class DefaultSerializerDeserializationTest method publisherCompletesWithLeftOverData.

@Test
void publisherCompletesWithLeftOverData() {
    Buffer first = mock(Buffer.class);
    Buffer second = mock(Buffer.class);
    when(deSerializer.deserialize(first)).thenReturn(singletonList("Hello1"));
    when(deSerializer.deserialize(second)).thenReturn(emptyList());
    when(deSerializer.hasData()).thenReturn(true);
    final IllegalStateException e = new IllegalStateException();
    doThrow(e).when(deSerializer).close();
    final Publisher<String> deserialized = factory.deserialize(from(first, second), String.class);
    TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
    toSource(deserialized).subscribe(subscriber);
    subscriber.awaitSubscription().request(2);
    verify(provider).getDeserializer(String.class);
    verify(deSerializer).deserialize(first);
    verify(deSerializer).deserialize(second);
    assertThat(subscriber.takeOnNext(), is("Hello1"));
    assertThat(subscriber.awaitOnError(), sameInstance(e));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) Test(org.junit.jupiter.api.Test)

Example 7 with TestPublisherSubscriber

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

the class MulticastPublisherTest method concurrentRequestN.

@Test
void concurrentRequestN() throws InterruptedException {
    final int expectedSubscribers = 50;
    Publisher<Integer> multicast = source.multicast(expectedSubscribers);
    @SuppressWarnings("unchecked") TestPublisherSubscriber<Integer>[] subscribers = (TestPublisherSubscriber<Integer>[]) new TestPublisherSubscriber[expectedSubscribers];
    final int expectedSubscribersMinus1 = expectedSubscribers - 1;
    for (int i = 0; i < expectedSubscribersMinus1; ++i) {
        subscribers[i] = new TestPublisherSubscriber<>();
        toSource(multicast).subscribe(subscribers[i]);
    }
    subscribers[expectedSubscribersMinus1] = new TestPublisherSubscriber<>();
    toSource(multicast).subscribe(subscribers[expectedSubscribersMinus1]);
    for (int i = 0; i < expectedSubscribersMinus1; ++i) {
        subscribers[i].awaitSubscription();
    }
    ExecutorService executorService = new ThreadPoolExecutor(0, expectedSubscribers, 1, SECONDS, new SynchronousQueue<>());
    try {
        CyclicBarrier barrier = new CyclicBarrier(expectedSubscribers);
        CountDownLatch doneLatch = new CountDownLatch(expectedSubscribers);
        AtomicReference<Throwable> throwableRef = new AtomicReference<>();
        for (int i = 1; i <= expectedSubscribers; ++i) {
            executorService.execute(requestIRunnable(subscribers, i, i, barrier, throwableRef, doneLatch));
        }
        doneLatch.await();
        assertThat(throwableRef.get(), is(nullValue()));
        assertThat(subscription.requested(), is(1L));
        assertThat(subscription.isCancelled(), is(false));
    } finally {
        executorService.shutdown();
    }
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with TestPublisherSubscriber

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

the class MulticastPublisherTest method concurrentRequestNAndOnNext.

@Test
void concurrentRequestNAndOnNext() throws BrokenBarrierException, InterruptedException {
    final int expectedSubscribers = 400;
    Publisher<Integer> multicast = source.multicast(expectedSubscribers);
    @SuppressWarnings("unchecked") TestPublisherSubscriber<Integer>[] subscribers = (TestPublisherSubscriber<Integer>[]) new TestPublisherSubscriber[expectedSubscribers];
    final int expectedSubscribersMinus1 = expectedSubscribers - 1;
    for (int i = 0; i < expectedSubscribersMinus1; ++i) {
        subscribers[i] = new TestPublisherSubscriber<>();
        toSource(multicast).subscribe(subscribers[i]);
    }
    subscribers[expectedSubscribersMinus1] = new TestPublisherSubscriber<>();
    toSource(multicast).subscribe(subscribers[expectedSubscribersMinus1]);
    for (int i = 0; i < expectedSubscribersMinus1; ++i) {
        subscribers[i].awaitSubscription();
    }
    ExecutorService executorService = new ThreadPoolExecutor(0, expectedSubscribers, 1, SECONDS, new SynchronousQueue<>());
    try {
        CyclicBarrier barrier = new CyclicBarrier(expectedSubscribers + 1);
        CountDownLatch doneLatch = new CountDownLatch(expectedSubscribers);
        AtomicReference<Throwable> throwableRef = new AtomicReference<>();
        for (int i = 1; i <= expectedSubscribers; ++i) {
            executorService.execute(requestIRunnable(subscribers, i, expectedSubscribers, barrier, throwableRef, doneLatch));
        }
        barrier.await();
        for (int i = 0; i < expectedSubscribers; ++i) {
            subscription.awaitRequestN(i + 1);
            source.onNext(i);
        }
        doneLatch.await();
        assertThat(throwableRef.get(), is(nullValue()));
        List<Integer> expectedItems = new ArrayList<>(expectedSubscribers);
        for (int x = 0; x < expectedSubscribers; ++x) {
            expectedItems.add(x);
        }
        for (int i = 0; i < expectedSubscribers; ++i) {
            final Integer[] expectedSubset = expectedItems.subList(0, i).toArray(new Integer[0]);
            List<Integer> actual = subscribers[i].takeOnNext(i);
            if (expectedSubset.length == 0) {
                assertThat(actual.isEmpty(), is(true));
            } else {
                assertThat(actual, contains(expectedSubset));
            }
        }
        assertThat(subscription.requested(), is((long) expectedSubscribers));
        assertThat(subscription.isCancelled(), is(false));
    } finally {
        executorService.shutdown();
    }
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with TestPublisherSubscriber

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

the class ScanWithPublisherTest method scanWithFinalizationOnCancelDifferentThreads.

@ParameterizedTest(name = "{displayName} [{index}] {arguments}")
@ValueSource(booleans = { true, false })
void scanWithFinalizationOnCancelDifferentThreads(final boolean interleaveCancellation) {
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    try {
        final AtomicInteger finalizations = new AtomicInteger(0);
        PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
        final CountDownLatch checkpoint = new CountDownLatch(1);
        final CountDownLatch requested = new CountDownLatch(1);
        final CountDownLatch resume = new CountDownLatch(1);
        final CountDownLatch nextDelivered = new CountDownLatch(1);
        final CountDownLatch nextDeliveredResume = new CountDownLatch(1);
        final PublisherSource<Integer> source = toSource(scanWithOperator(fromSource(processor), true, new ScanWithLifetimeMapper<Integer, Integer>() {

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

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

            @Override
            public Integer mapOnComplete() {
                return 5;
            }

            @Override
            public boolean mapTerminal() {
                if (interleaveCancellation) {
                    checkpoint.countDown();
                    try {
                        resume.await();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        throwException(e);
                    }
                }
                return true;
            }

            @Override
            public void afterFinally() {
                finalizations.incrementAndGet();
            }
        }));
        Future<Void> f = executorService.submit(() -> {
            try {
                TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
                source.subscribe(subscriber);
                Subscription s = subscriber.awaitSubscription();
                s.request(2);
                requested.countDown();
                if (interleaveCancellation) {
                    checkpoint.await();
                } else {
                    nextDelivered.await();
                }
                s.cancel();
                if (!interleaveCancellation) {
                    nextDeliveredResume.countDown();
                }
                resume.countDown();
            } catch (Exception e) {
                throw new AssertionError(e);
            }
            return null;
        });
        requested.await();
        processor.onNext(1);
        if (!interleaveCancellation) {
            nextDelivered.countDown();
            nextDeliveredResume.await();
        }
        processor.onComplete();
        f.get();
        assertThat(finalizations.get(), is(1));
    } catch (Throwable e) {
        throw new AssertionError(e);
    } finally {
        executorService.shutdown();
    }
}
Also used : PublisherSource(io.servicetalk.concurrent.PublisherSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) PlatformDependent.throwException(io.servicetalk.utils.internal.PlatformDependent.throwException) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Nullable(javax.annotation.Nullable) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with TestPublisherSubscriber

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

the class ScanWithPublisherTest method scanWithFinalizationOnCancel.

@Test
void scanWithFinalizationOnCancel() {
    final AtomicInteger finalizations = new AtomicInteger(0);
    PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
    TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
    toSource(scanWithOperator(fromSource(processor), true, new ScanWithLifetimeMapper<Integer, Integer>() {

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

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

        @Override
        public Integer mapOnComplete() {
            return 5;
        }

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

        @Override
        public void afterFinally() {
            finalizations.incrementAndGet();
        }
    })).subscribe(subscriber);
    Subscription s = subscriber.awaitSubscription();
    s.request(1);
    processor.onNext(1);
    processor.onComplete();
    s.cancel();
    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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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