Search in sources :

Example 1 with IGNORE_CANCEL

use of io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL in project servicetalk by apple.

the class CompletableConcatWithCompletablesTest method testSourceSuccessReentrant.

@ParameterizedTest
@ValueSource(ints = { 1, 2, 10000 })
void testSourceSuccessReentrant(int num) {
    Completable[] mockCompletables = new Completable[num];
    for (int i = 0; i < mockCompletables.length; ++i) {
        CompletableSource mockCompletable = mock(CompletableSource.class);
        doAnswer((Answer<Void>) invocation -> {
            CompletableSource.Subscriber sub = invocation.getArgument(0);
            sub.onSubscribe(IGNORE_CANCEL);
            sub.onComplete();
            return null;
        }).when(mockCompletable).subscribe(any());
        mockCompletables[i] = fromSource(mockCompletable);
    }
    toSource(source.concat(mockCompletables)).subscribe(subscriber);
    source.onComplete();
    subscriber.awaitOnComplete();
}
Also used : ValueSource(org.junit.jupiter.params.provider.ValueSource) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Completable(io.servicetalk.concurrent.api.Completable) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Test(org.junit.jupiter.api.Test) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Answer(org.mockito.stubbing.Answer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) IGNORE_CANCEL(io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Mockito.mock(org.mockito.Mockito.mock) Completable(io.servicetalk.concurrent.api.Completable) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) CompletableSource(io.servicetalk.concurrent.CompletableSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with IGNORE_CANCEL

use of io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL in project servicetalk by apple.

the class ReactiveStreamsAdaptersTest method completableToRSFromSourceSuccess.

@Test
void completableToRSFromSourceSuccess() {
    CompletableSource source = s -> {
        s.onSubscribe(IGNORE_CANCEL);
        s.onComplete();
    };
    verifyRSSuccess(toRSPublisherFromSourceAndSubscribe(source), false);
}
Also used : ReactiveStreamsAdapters.toReactiveStreamsPublisher(io.servicetalk.concurrent.reactivestreams.ReactiveStreamsAdapters.toReactiveStreamsPublisher) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestSingle(io.servicetalk.concurrent.api.TestSingle) SingleSource(io.servicetalk.concurrent.SingleSource) Cancellable(io.servicetalk.concurrent.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BiConsumer(java.util.function.BiConsumer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Subscriber(org.reactivestreams.Subscriber) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) PublisherSource(io.servicetalk.concurrent.PublisherSource) Single(io.servicetalk.concurrent.api.Single) Publisher(org.reactivestreams.Publisher) Completable(io.servicetalk.concurrent.api.Completable) CompletableSource(io.servicetalk.concurrent.CompletableSource) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) 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) Publisher.failed(io.servicetalk.concurrent.api.Publisher.failed) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) Subscription(org.reactivestreams.Subscription) ReactiveStreamsAdapters.fromReactiveStreamsPublisher(io.servicetalk.concurrent.reactivestreams.ReactiveStreamsAdapters.fromReactiveStreamsPublisher) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) CompletableSource(io.servicetalk.concurrent.CompletableSource) Test(org.junit.jupiter.api.Test)

Example 3 with IGNORE_CANCEL

use of io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL in project servicetalk by apple.

the class ServiceTalkThreadContextMapTest method testAsyncExecution.

@Test
void testAsyncExecution() throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        MDC.clear();
        MDC.put("a", "1");
        MDC.put("b", "2");
        // human inspection as sanity check
        logger.info("expected a=1 b=2");
        Thread original = Thread.currentThread();
        Single<String> single = new Single<String>() {

            @Override
            protected void handleSubscribe(Subscriber<? super String> singleSubscriber) {
                executor.execute(() -> {
                    singleSubscriber.onSubscribe(IGNORE_CANCEL);
                    singleSubscriber.onSuccess("1");
                });
            }
        }.map(v -> {
            assertNotEquals(Thread.currentThread(), original);
            assertEquals("1", MDC.get("a"));
            assertEquals("2", MDC.get("b"));
            MDC.put("b", "22");
            return v;
        }).beforeFinally(() -> {
            // human inspection as sanity check
            logger.info("expected a=1 b=22");
            assertEquals("1", MDC.get("a"));
            assertEquals("22", MDC.get("b"));
        });
        single.toFuture().get();
    } finally {
        executor.shutdown();
    }
}
Also used : ThreadContext(org.apache.logging.log4j.ThreadContext) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Logger(org.slf4j.Logger) Subscriber(io.servicetalk.concurrent.SingleSource.Subscriber) Single(io.servicetalk.concurrent.api.Single) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) ServiceTalkThreadContextMap.getStorage(io.servicetalk.log4j2.mdc.utils.ServiceTalkThreadContextMap.getStorage) IGNORE_CANCEL(io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL) MDC(org.slf4j.MDC) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) Single(io.servicetalk.concurrent.api.Single) Subscriber(io.servicetalk.concurrent.SingleSource.Subscriber) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 4 with IGNORE_CANCEL

use of io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL in project servicetalk by apple.

the class SourceAdaptersTest method completableFromSourceSuccess.

@Test
void completableFromSourceSuccess() throws Exception {
    CompletableSource src = s -> {
        s.onSubscribe(IGNORE_CANCEL);
        s.onComplete();
    };
    fromSource(src).toFuture().get();
}
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) CompletableSource(io.servicetalk.concurrent.CompletableSource) Test(org.junit.jupiter.api.Test)

Example 5 with IGNORE_CANCEL

use of io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL in project servicetalk by apple.

the class SourceAdaptersTest method completableFromSourceError.

@Test
void completableFromSourceError() {
    CompletableSource src = s -> {
        s.onSubscribe(IGNORE_CANCEL);
        s.onError(DELIBERATE_EXCEPTION);
    };
    Future<Void> future = fromSource(src).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) CompletableSource(io.servicetalk.concurrent.CompletableSource) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

IGNORE_CANCEL (io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL)6 Test (org.junit.jupiter.api.Test)6 CompletableSource (io.servicetalk.concurrent.CompletableSource)5 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 Matchers.is (org.hamcrest.Matchers.is)5 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)5 Mockito.mock (org.mockito.Mockito.mock)5 Cancellable (io.servicetalk.concurrent.Cancellable)4 PublisherSource (io.servicetalk.concurrent.PublisherSource)4 SingleSource (io.servicetalk.concurrent.SingleSource)4 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)4 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)4 SourceAdapters.fromSource (io.servicetalk.concurrent.api.SourceAdapters.fromSource)4 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)4 EMPTY_SUBSCRIPTION (io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION)4 ScalarValueSubscription (io.servicetalk.concurrent.internal.ScalarValueSubscription)4 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)4