Search in sources :

Example 16 with CompositeException

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

the class UniOnEventTest method testEventuallyActionThrowingException.

@Test
public void testEventuallyActionThrowingException() {
    AtomicReference<Object> item = new AtomicReference<>();
    AtomicBoolean eventuallyCalled = new AtomicBoolean();
    UniAssertSubscriber<Object> subscriber = Uni.createFrom().failure(new IOException("boom")).invoke(item::set).eventually(() -> {
        eventuallyCalled.set(true);
        throw new RuntimeException("bam");
    }).subscribe().withSubscriber(UniAssertSubscriber.create());
    subscriber.assertFailedWith(CompositeException.class, "boom");
    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("bam");
    assertThat(item.get()).isNull();
    assertThat(eventuallyCalled).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 17 with CompositeException

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

the class UniOnFailureInvokeTest method testInvokeOnFailureWithExceptionThrownByCallback.

@Test
public void testInvokeOnFailureWithExceptionThrownByCallback() {
    AtomicReference<Throwable> container = new AtomicReference<>();
    assertThatThrownBy(() -> failure.onFailure().invoke(t -> {
        container.set(t);
        throw new IllegalStateException("bad");
    }).await().indefinitely()).isInstanceOf(CompositeException.class).satisfies(t -> {
        CompositeException ex = (CompositeException) t;
        assertThat(ex.getCauses()).hasSize(2).contains(BOOM);
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 18 with CompositeException

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

the class UniOnFailureInvokeTest method testCallOnFailureWithCallbackReturningNull.

@Test
public void testCallOnFailureWithCallbackReturningNull() {
    AtomicReference<Throwable> container = new AtomicReference<>();
    assertThatThrownBy(() -> failure.onFailure().call(t -> {
        container.set(t);
        return null;
    }).await().indefinitely()).isInstanceOf(CompositeException.class).satisfies(t -> {
        CompositeException ex = (CompositeException) t;
        assertThat(ex.getCauses()).hasSize(2).contains(BOOM);
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 19 with CompositeException

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

the class MultiOnTerminationUniInvokeTest method testTerminationWhenErrorIsEmittedButUniInvokeIsFailed.

@Test
public void testTerminationWhenErrorIsEmittedButUniInvokeIsFailed() {
    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);
        return Uni.createFrom().failure(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 20 with CompositeException

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

the class MultiMergeTest method testWithFailureCollectionWithMerge.

@Test
public void testWithFailureCollectionWithMerge() {
    IllegalStateException boom = new IllegalStateException("boom");
    IllegalStateException boom2 = new IllegalStateException("boom2");
    AssertSubscriber<Integer> subscriber = Multi.createBy().merging().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().merging().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)

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