Search in sources :

Example 21 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class PublisherConcatMapIterableTest method exceptionFromOnErrorIsPropagated.

@Test
void exceptionFromOnErrorIsPropagated() {
    toSource(publisher.flatMapConcatIterable(identity()).afterOnError(t -> {
        throw DELIBERATE_EXCEPTION;
    })).subscribe(subscriber);
    subscriber.awaitSubscription();
    DeliberateException exception = assertThrows(DeliberateException.class, () -> publisher.onError(DELIBERATE_EXCEPTION));
    assertThat(exception, is(DELIBERATE_EXCEPTION));
}
Also used : DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 22 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class PublisherConcatMapIterableTest method testExceptionFromBufferedOnNextThenTerminalIsPropagated.

@Test
void testExceptionFromBufferedOnNextThenTerminalIsPropagated() {
    final DeliberateException ex2 = new DeliberateException();
    final AtomicBoolean errored = new AtomicBoolean();
    toSource(publisher.flatMapConcatIterable(identity()).map((Function<String, String>) s -> {
        if (!errored.getAndSet(true)) {
            publisher.onError(DELIBERATE_EXCEPTION);
        }
        throw ex2;
    })).subscribe(subscriber);
    subscriber.awaitSubscription().request(3);
    publisher.onNext(asList("one", "two", "three"));
    assertThat(subscriber.awaitOnError(), is(ex2));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) PlatformDependent.throwException(io.servicetalk.utils.internal.PlatformDependent.throwException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Processor(io.servicetalk.concurrent.PublisherSource.Processor) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Subscriber(io.servicetalk.concurrent.PublisherSource.Subscriber) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Processors.newPublisherProcessor(io.servicetalk.concurrent.api.Processors.newPublisherProcessor) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Publisher.failed(io.servicetalk.concurrent.api.Publisher.failed) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Function.identity(java.util.function.Function.identity) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 23 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class PublisherConcatMapIterableTest method upstreamRecoverWithMakesProgress.

@Test
void upstreamRecoverWithMakesProgress() throws Exception {
    @SuppressWarnings("unchecked") Subscriber<String> mockSubscriber = mock(Subscriber.class);
    CountDownLatch latchOnSubscribe = new CountDownLatch(1);
    CountDownLatch latchOnError = new CountDownLatch(1);
    AtomicReference<Throwable> causeRef = new AtomicReference<>();
    AtomicInteger nextCount = new AtomicInteger();
    List<String> results = new ArrayList<>();
    doAnswer(a -> {
        Subscription s = a.getArgument(0);
        s.request(Long.MAX_VALUE);
        latchOnSubscribe.countDown();
        return null;
    }).when(mockSubscriber).onSubscribe(any(Subscription.class));
    doAnswer(a -> {
        causeRef.set(a.getArgument(0));
        latchOnError.countDown();
        return null;
    }).when(mockSubscriber).onError(eq(DELIBERATE_EXCEPTION));
    doAnswer(a -> {
        results.add(a.getArgument(0));
        if (nextCount.getAndIncrement() == 0) {
            throw new DeliberateException();
        }
        // final exception
        throw DELIBERATE_EXCEPTION;
    }).when(mockSubscriber).onNext(any());
    Processor<List<String>, List<String>> processor = newPublisherProcessor();
    toSource(fromSource(processor).onErrorResume(cause -> {
        if (cause != DELIBERATE_EXCEPTION) {
            // recover!
            return from(singletonList("two"));
        }
        return failed(cause);
    }).flatMapConcatIterable(identity())).subscribe(mockSubscriber);
    latchOnSubscribe.await();
    processor.onNext(asList("one", "ignored!"));
    latchOnError.await();
    assertThat(results, contains("one", "two"));
    assertThat(causeRef.get(), is(DELIBERATE_EXCEPTION));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) PlatformDependent.throwException(io.servicetalk.utils.internal.PlatformDependent.throwException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Processor(io.servicetalk.concurrent.PublisherSource.Processor) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Subscriber(io.servicetalk.concurrent.PublisherSource.Subscriber) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) SourceAdapters.fromSource(io.servicetalk.concurrent.api.SourceAdapters.fromSource) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Processors.newPublisherProcessor(io.servicetalk.concurrent.api.Processors.newPublisherProcessor) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Publisher.failed(io.servicetalk.concurrent.api.Publisher.failed) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Function.identity(java.util.function.Function.identity) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 24 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class PublisherFlatMapSingleTest method testMultipleSingleErrors.

@Test
void testMultipleSingleErrors() {
    Queue<DeliberateException> errors = new ArrayDeque<>();
    toSource(source.flatMapMergeSingleDelayError(integer -> {
        DeliberateException de = new DeliberateException();
        errors.add(de);
        return Single.<Integer>failed(de);
    }, 2, 2)).subscribe(subscriber);
    subscriber.awaitSubscription().request(3);
    source.onNext(1, 1);
    source.onComplete();
    Throwable cause = subscriber.awaitOnError();
    assertThat(errors, hasSize(2));
    assertThat(asList(cause.getSuppressed()), hasSize(1));
    assertThat(cause, is(errors.poll()));
    assertThat(cause.getSuppressed()[0], is(errors.poll()));
}
Also used : DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) ArrayDeque(java.util.ArrayDeque) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with DeliberateException

use of io.servicetalk.concurrent.internal.DeliberateException in project servicetalk by apple.

the class OnErrorCompletableTest method onErrorResumePredicateMatch.

@Test
void onErrorResumePredicateMatch() {
    toSource(first.onErrorResume(t -> t instanceof DeliberateException, t -> failed(DELIBERATE_EXCEPTION))).subscribe(subscriber);
    subscriber.awaitSubscription();
    first.onError(new DeliberateException());
    assertThat(subscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
}
Also used : DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Aggregations

DeliberateException (io.servicetalk.concurrent.internal.DeliberateException)80 Test (org.junit.jupiter.api.Test)77 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)6 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)6 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)6 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)5 PublisherSource (io.servicetalk.concurrent.PublisherSource)4 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)4 TerminalSignalConsumer (io.servicetalk.concurrent.api.TerminalSignalConsumer)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Subscriber (io.servicetalk.concurrent.PublisherSource.Subscriber)3 SourceAdapters.fromSource (io.servicetalk.concurrent.api.SourceAdapters.fromSource)3 PlatformDependent.throwException (io.servicetalk.utils.internal.PlatformDependent.throwException)3 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)3