Search in sources :

Example 1 with PublisherSource

use of io.servicetalk.concurrent.PublisherSource 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 2 with PublisherSource

use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.

the class SourceAdaptersTest method publisherFromSourceCancel.

@Test
void publisherFromSourceCancel() {
    PublisherSource.Subscription srcSubscription = mock(PublisherSource.Subscription.class);
    PublisherSource<Integer> source = s -> s.onSubscribe(srcSubscription);
    fromSource(source).firstOrElse(() -> null).toFuture().cancel(true);
    verify(srcSubscription).cancel();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SingleSource(io.servicetalk.concurrent.SingleSource) Cancellable(io.servicetalk.concurrent.Cancellable) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) ArgumentCaptor(org.mockito.ArgumentCaptor) EMPTY_SUBSCRIPTION(io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) PublisherSource(io.servicetalk.concurrent.PublisherSource) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) IGNORE_CANCEL(io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) PublisherSource(io.servicetalk.concurrent.PublisherSource) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test)

Example 3 with PublisherSource

use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.

the class ScanWithPublisherTest method scanWithLifetimeSignalReentry.

@Test
void scanWithLifetimeSignalReentry() throws InterruptedException {
    AtomicInteger finalizations = new AtomicInteger();
    CountDownLatch completed = new CountDownLatch(1);
    PublisherSource<Integer> syncNoReentryProtectionSource = subscriber -> subscriber.onSubscribe(new Subscription() {

        int count;

        @Override
        public void request(final long n) {
            if (count == 2) {
                subscriber.onComplete();
            } else {
                subscriber.onNext(count++);
            }
        }

        @Override
        public void cancel() {
        }
    });
    toSource(fromSource(syncNoReentryProtectionSource).scanWithLifetime(() -> new ScanWithLifetimeMapper<Integer, Integer>() {

        @Override
        public void afterFinally() {
            finalizations.incrementAndGet();
            completed.countDown();
        }

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

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

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

        @Override
        public boolean mapTerminal() {
            return false;
        }
    })).subscribe(new PublisherSource.Subscriber<Integer>() {

        Subscription subscription;

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

        @Override
        public void onNext(@Nullable final Integer integer) {
            subscription.request(1);
        }

        @Override
        public void onError(final Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    completed.await();
    assertThat(finalizations.get(), is(1));
}
Also used : PlatformDependent.throwException(io.servicetalk.utils.internal.PlatformDependent.throwException) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SubscriberUtils.newExceptionForInvalidRequestN(io.servicetalk.concurrent.internal.SubscriberUtils.newExceptionForInvalidRequestN) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) PublisherSource(io.servicetalk.concurrent.PublisherSource) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Arguments(org.junit.jupiter.params.provider.Arguments) Processors.newPublisherProcessor(io.servicetalk.concurrent.api.Processors.newPublisherProcessor) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.is(org.hamcrest.Matchers.is) PublisherSource(io.servicetalk.concurrent.PublisherSource) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Nullable(javax.annotation.Nullable) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with PublisherSource

use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.

the class SourceAdaptersTest method publisherFromSourceError.

@Test
void publisherFromSourceError() {
    PublisherSource<Integer> src = s -> {
        s.onSubscribe(EMPTY_SUBSCRIPTION);
        s.onError(DELIBERATE_EXCEPTION);
    };
    Future<Integer> future = fromSource(src).firstOrElse(() -> null).toFuture();
    Exception e = assertThrows(ExecutionException.class, () -> future.get());
    assertThat(e.getCause(), sameInstance(DELIBERATE_EXCEPTION));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SingleSource(io.servicetalk.concurrent.SingleSource) Cancellable(io.servicetalk.concurrent.Cancellable) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) ArgumentCaptor(org.mockito.ArgumentCaptor) EMPTY_SUBSCRIPTION(io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) PublisherSource(io.servicetalk.concurrent.PublisherSource) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) ScalarValueSubscription(io.servicetalk.concurrent.internal.ScalarValueSubscription) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) IGNORE_CANCEL(io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

PublisherSource (io.servicetalk.concurrent.PublisherSource)4 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)4 SourceAdapters.fromSource (io.servicetalk.concurrent.api.SourceAdapters.fromSource)3 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)3 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)3 Future (java.util.concurrent.Future)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Matchers.is (org.hamcrest.Matchers.is)3 Test (org.junit.jupiter.api.Test)3 Cancellable (io.servicetalk.concurrent.Cancellable)2 IGNORE_CANCEL (io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL)2 CompletableSource (io.servicetalk.concurrent.CompletableSource)2 SingleSource (io.servicetalk.concurrent.SingleSource)2 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)2 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)2 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)2 DeliberateException (io.servicetalk.concurrent.internal.DeliberateException)2 EMPTY_SUBSCRIPTION (io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION)2 ScalarValueSubscription (io.servicetalk.concurrent.internal.ScalarValueSubscription)2 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)2