Search in sources :

Example 11 with CompositeException

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

the class UniOnEventTest method testEventuallyFailedUniOnFailure.

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

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

the class UniOnEventTest method testActionsOnTerminationWithFailure.

@Test
public void testActionsOnTerminationWithFailure() {
    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);
        return Uni.createFrom().failure(new IOException("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(IOException.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)

Example 13 with CompositeException

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

the class UniOnFailureInvokeTest method testCallOnFailureWithExceptionThrownByCallback.

@Test
public void testCallOnFailureWithExceptionThrownByCallback() {
    AtomicReference<Throwable> container = new AtomicReference<>();
    assertThatThrownBy(() -> failure.onFailure().call(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 14 with CompositeException

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

the class UniOnEventTest method testActionsOnTerminationWithSupplierOnFailure.

@Test
public void testActionsOnTerminationWithSupplierOnFailure() {
    AtomicInteger Item = new AtomicInteger();
    AtomicReference<Throwable> failure = new AtomicReference<>();
    AtomicReference<Subscription> subscription = new AtomicReference<>();
    AtomicBoolean terminate = new AtomicBoolean();
    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(() -> {
        terminate.set(true);
        return Uni.createFrom().failure(new IOException("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(IOException.class).hasMessageContaining("tada");
    assertThat(Item).doesNotHaveValue(1);
    assertThat(subscription.get()).isNotNull();
    assertThat(terminate.get()).isTrue();
}
Also used : CompositeException(io.smallrye.mutiny.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 15 with CompositeException

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

the class UniOnEventTest method testEventuallyUniThrowingOnFailure.

@Test
public void testEventuallyUniThrowingOnFailure() {
    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)

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