Search in sources :

Example 6 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class MultiOnTerminationUniInvokeTest method testTerminationWhenErrorIsEmittedButUniInvokeThrowsException.

@Test
public void testTerminationWhenErrorIsEmittedButUniInvokeThrowsException() {
    AssertSubscriber<Object> subscriber = AssertSubscriber.create();
    AtomicReference<Subscription> subscription = new AtomicReference<>();
    AtomicReference<Object> item = new AtomicReference<>();
    AtomicReference<Throwable> failure = new AtomicReference<>();
    AtomicBoolean completion = new AtomicBoolean();
    AtomicLong requests = new AtomicLong();
    AtomicBoolean cancellation = new AtomicBoolean();
    AtomicBoolean termination = new AtomicBoolean();
    AtomicReference<Throwable> terminationException = new AtomicReference<>();
    AtomicBoolean terminationCancelledFlag = new AtomicBoolean();
    Multi.createFrom().failure(new IOException("boom")).onSubscription().invoke(subscription::set).onItem().invoke(item::set).onFailure().invoke(failure::set).onCompletion().invoke(() -> completion.set(true)).onTermination().call((t, c) -> {
        termination.set(true);
        terminationException.set(t);
        terminationCancelledFlag.set(c);
        throw new RuntimeException("tada");
    }).onRequest().invoke(requests::set).onCancellation().invoke(() -> cancellation.set(true)).subscribe().withSubscriber(subscriber);
    subscriber.request(20).assertHasNotReceivedAnyItem().assertFailedWith(CompositeException.class, "boom");
    assertThat(subscriber.getFailure()).isNotNull();
    CompositeException compositeException = (CompositeException) subscriber.getFailure();
    assertThat(compositeException.getCauses()).hasSize(2);
    assertThat(compositeException.getCauses().get(0)).isInstanceOf(IOException.class).hasMessage("boom");
    assertThat(compositeException.getCauses().get(1)).isInstanceOf(RuntimeException.class).hasMessage("tada");
    assertThat(subscription.get()).isNotNull();
    assertThat(item.get()).isNull();
    assertThat(failure.get()).isNotNull().isInstanceOf(IOException.class).hasMessageContaining("boom");
    assertThat(completion.get()).isFalse();
    assertThat(requests.get()).isEqualTo(0L);
    assertThat(cancellation.get()).isFalse();
    assertThat(termination.get()).isTrue();
    assertThat(terminationException.get()).isNotNull().isInstanceOf(IOException.class).hasMessageContaining("boom");
    assertThat(terminationCancelledFlag.get()).isFalse();
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 7 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class MultiMergeTest method testWithFailureCollectionWithConcatenation.

@Test
public void testWithFailureCollectionWithConcatenation() {
    IllegalStateException boom = new IllegalStateException("boom");
    IllegalStateException boom2 = new IllegalStateException("boom2");
    AssertSubscriber<Integer> subscriber = Multi.createBy().concatenating().collectFailures().streams(Multi.createFrom().item(5), Multi.createFrom().failure(boom), Multi.createFrom().item(6), Multi.createFrom().failure(boom2)).subscribe().withSubscriber(new AssertSubscriber<>(5));
    subscriber.assertTerminated().assertItems(5, 6).assertFailedWith(CompositeException.class, "boom").assertFailedWith(CompositeException.class, "boom2");
    assertThat(subscriber.getFailure()).isInstanceOf(CompositeException.class);
    CompositeException ce = (CompositeException) subscriber.getFailure();
    assertThat(ce.getCauses()).hasSize(2);
    subscriber = Multi.createBy().concatenating().streams(Multi.createFrom().item(5), Multi.createFrom().failure(boom), Multi.createFrom().item(6), Multi.createFrom().failure(boom)).subscribe().withSubscriber(new AssertSubscriber<>(5));
    subscriber.assertTerminated().assertItems(5).assertFailedWith(IllegalStateException.class, "boom");
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Test(org.junit.jupiter.api.Test)

Example 8 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class MultiOnEventTest method testThatPredicateFailureProduceCompositeException.

@Test
public void testThatPredicateFailureProduceCompositeException() {
    AtomicBoolean called = new AtomicBoolean();
    AssertSubscriber<Object> subscriber = Multi.createFrom().failure(new IOException("boom")).onFailure(t -> {
        throw new NullPointerException();
    }).invoke(t -> called.set(true)).subscribe().withSubscriber(AssertSubscriber.create(1));
    subscriber.assertFailedWith(CompositeException.class, "boom");
    CompositeException failure = (CompositeException) subscriber.getFailure();
    assertThat(failure.getCauses()).hasSize(2);
    assertThat(called).isFalse();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) BroadcastProcessor(io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor) Predicate(java.util.function.Predicate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompositeException(io.smallrye.mutiny.CompositeException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 9 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class UniOnEventTest method testInvokeOnFailureWithPredicate.

@Test
public void testInvokeOnFailureWithPredicate() {
    AtomicBoolean noPredicate = new AtomicBoolean();
    AtomicBoolean exactClassMatch = new AtomicBoolean();
    AtomicBoolean parentClassMatch = new AtomicBoolean();
    AtomicBoolean predicateMatch = new AtomicBoolean();
    AtomicBoolean predicateNoMatch = new AtomicBoolean();
    AtomicBoolean classNoMatch = new AtomicBoolean();
    UniAssertSubscriber<Object> subscriber = Uni.createFrom().failure(new IOException("boom")).onFailure().invoke(t -> noPredicate.set(true)).onFailure(IOException.class).invoke(t -> exactClassMatch.set(true)).onFailure(Exception.class).invoke(t -> parentClassMatch.set(true)).onFailure(IllegalArgumentException.class).invoke(t -> classNoMatch.set(true)).onFailure(t -> t.getMessage().equalsIgnoreCase("boom")).invoke(t -> predicateMatch.set(true)).onFailure(t -> t.getMessage().equalsIgnoreCase("nope")).invoke(t -> predicateNoMatch.set(true)).subscribe().withSubscriber(UniAssertSubscriber.create());
    subscriber.assertFailed().assertFailedWith(IOException.class, "boom");
    assertThat(noPredicate).isTrue();
    assertThat(exactClassMatch).isTrue();
    assertThat(parentClassMatch).isTrue();
    assertThat(predicateMatch).isTrue();
    assertThat(predicateNoMatch).isFalse();
    assertThat(classNoMatch).isFalse();
    AtomicBoolean called = new AtomicBoolean();
    subscriber = Uni.createFrom().failure(new IOException("boom")).onFailure(t -> {
        throw new NullPointerException();
    }).invoke(t -> called.set(true)).subscribe().withSubscriber(UniAssertSubscriber.create());
    subscriber.assertFailed().assertFailedWith(CompositeException.class, "boom");
    CompositeException composite = (CompositeException) subscriber.getFailure();
    assertThat(composite.getCauses()).hasSize(2).anySatisfy(t -> assertThat(t).isInstanceOf(NullPointerException.class)).anySatisfy(t -> assertThat(t).isInstanceOf(IOException.class));
    assertThat(called).isFalse();
}
Also used : Awaitility.await(org.awaitility.Awaitility.await) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UniAssertSubscriber(io.smallrye.mutiny.helpers.test.UniAssertSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) InfrastructureResource(junit5.support.InfrastructureResource) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeException(io.smallrye.mutiny.CompositeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompositeException(io.smallrye.mutiny.CompositeException) IOException(java.io.IOException) IOException(java.io.IOException) CompositeException(io.smallrye.mutiny.CompositeException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 10 with CompositeException

use of io.smallrye.mutiny.CompositeException in project smallrye-mutiny by smallrye.

the class UniOnEventTest method testActionsOnTerminationWithMapperThrowingException.

@Test
public void testActionsOnTerminationWithMapperThrowingException() {
    AtomicInteger Item = new AtomicInteger();
    AtomicReference<Throwable> failure = new AtomicReference<>();
    AtomicReference<Subscription> subscription = new AtomicReference<>();
    AtomicReference<Throwable> terminate = new AtomicReference<>();
    UniAssertSubscriber<? super Integer> subscriber = Uni.createFrom().<Integer>failure(new IOException("boom")).onItem().invoke(Item::set).onFailure().invoke(failure::set).onSubscription().invoke(subscription::set).onTermination().call((r, f, c) -> {
        terminate.set(f);
        throw new RuntimeException("tada");
    }).subscribe().withSubscriber(UniAssertSubscriber.create());
    subscriber.assertFailed().assertFailedWith(CompositeException.class, "boom");
    CompositeException compositeException = (CompositeException) subscriber.getFailure();
    assertThat(compositeException).getRootCause().isInstanceOf(IOException.class).hasMessageContaining("boom");
    assertThat(compositeException.getCauses().get(1)).isInstanceOf(RuntimeException.class).hasMessageContaining("tada");
    assertThat(Item).doesNotHaveValue(1);
    assertThat(subscription.get()).isNotNull();
    assertThat(terminate.get()).isInstanceOf(IOException.class).hasMessageContaining("boom");
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

CompositeException (io.smallrye.mutiny.CompositeException)20 Test (org.junit.jupiter.api.Test)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 IOException (java.io.IOException)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)8 Subscription (org.reactivestreams.Subscription)8 Uni (io.smallrye.mutiny.Uni)7 Cancellable (io.smallrye.mutiny.subscription.Cancellable)5 Function (java.util.function.Function)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)4 Consumer (java.util.function.Consumer)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 CheckReturnValue (io.smallrye.common.annotation.CheckReturnValue)1 Context (io.smallrye.mutiny.Context)1 Multi (io.smallrye.mutiny.Multi)1