Search in sources :

Example 6 with Context

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

the class UniSerializedSubscriberTest method testDroppedExceptionsWhenOnFailureThrowsAnException.

@Test
public void testDroppedExceptionsWhenOnFailureThrowsAnException() {
    AtomicReference<Throwable> captured = new AtomicReference<>();
    Infrastructure.setDroppedExceptionHandler(captured::set);
    AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
    AbstractUni<Integer> uni = (AbstractUni<Integer>) Uni.createFrom().<Integer>emitter(reference::set);
    UniSerializedSubscriber.subscribe(uni, new UniSubscriber<Integer>() {

        @Override
        public Context context() {
            return Context.empty();
        }

        @Override
        public void onSubscribe(UniSubscription subscription) {
        // Do nothing
        }

        @Override
        public void onItem(Integer item) {
        // Do nothing
        }

        @Override
        public void onFailure(Throwable failure) {
            throw new IllegalArgumentException("boom");
        }
    });
    try {
        reference.get().fail(new IOException("I/O"));
        fail("Exception expected");
    } catch (IllegalArgumentException e) {
    // Expected.
    }
    assertThat(captured.get()).isNotNull().isInstanceOf(CompositeException.class).hasMessageContaining("boom").hasMessageContaining("I/O");
}
Also used : Context(io.smallrye.mutiny.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) IOException(java.io.IOException) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 7 with Context

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

the class UniSerializedSubscriberTest method testDroppedExceptionsWhenOnItemThrowsAnException.

@Test
public void testDroppedExceptionsWhenOnItemThrowsAnException() {
    AtomicBoolean called = new AtomicBoolean();
    AtomicReference<Throwable> captured = new AtomicReference<>();
    Infrastructure.setDroppedExceptionHandler(captured::set);
    AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
    AbstractUni<Integer> uni = (AbstractUni<Integer>) Uni.createFrom().<Integer>emitter(reference::set);
    UniSerializedSubscriber.subscribe(uni, new UniSubscriber<Integer>() {

        @Override
        public Context context() {
            return Context.empty();
        }

        @Override
        public void onSubscribe(UniSubscription subscription) {
        // Do nothing
        }

        @Override
        public void onItem(Integer item) {
            throw new IllegalArgumentException("boom");
        }

        @Override
        public void onFailure(Throwable failure) {
            called.set(true);
        }
    });
    try {
        reference.get().complete(1);
        fail("Exception expected");
    } catch (IllegalArgumentException e) {
    // Exception expected
    }
    assertThat(captured.get()).isNotNull().isInstanceOf(IllegalArgumentException.class).hasMessageContaining("boom");
    assertThat(called).isFalse();
}
Also used : Context(io.smallrye.mutiny.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 8 with Context

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

the class UniCreateFromEmitterTest method testFailureThrownBySubscribersOnFailure.

@Test
public void testFailureThrownBySubscribersOnFailure() {
    AtomicBoolean onTerminationCalled = new AtomicBoolean();
    Uni.createFrom().<Integer>emitter(emitter -> {
        emitter.onTermination(() -> onTerminationCalled.set(true));
        try {
            emitter.fail(new Exception("boom"));
            fail("Exception expected");
        } catch (Exception ex) {
        // expected
        }
    }).subscribe().withSubscriber(new UniSubscriber<Integer>() {

        @Override
        public Context context() {
            return Context.empty();
        }

        @Override
        public void onSubscribe(UniSubscription subscription) {
        }

        @Override
        public void onItem(Integer ignored) {
        }

        @Override
        public void onFailure(Throwable failure) {
            throw new NullPointerException("boom");
        }
    });
    assertThat(onTerminationCalled).isFalse();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(io.smallrye.mutiny.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) IOException(java.io.IOException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 9 with Context

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

the class UniCreateFromEmitterTest method testFailureThrownBySubscribersOnItem.

@Test
public void testFailureThrownBySubscribersOnItem() {
    AtomicBoolean called = new AtomicBoolean();
    AtomicBoolean onTerminationCalled = new AtomicBoolean();
    Uni.createFrom().<Integer>emitter(emitter -> {
        emitter.onTermination(() -> onTerminationCalled.set(true));
        try {
            emitter.complete(1);
            fail("Exception expected");
        } catch (Exception ex) {
        // expected
        }
    }).subscribe().withSubscriber(new UniSubscriber<Integer>() {

        @Override
        public Context context() {
            return Context.empty();
        }

        @Override
        public void onSubscribe(UniSubscription subscription) {
        }

        @Override
        public void onItem(Integer ignored) {
            throw new NullPointerException("boom");
        }

        @Override
        public void onFailure(Throwable failure) {
            called.set(true);
        }
    });
    assertThat(called).isFalse();
    assertThat(onTerminationCalled).isFalse();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(io.smallrye.mutiny.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) IOException(java.io.IOException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 10 with Context

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

the class UniWithContext method subscribe.

@Override
public void subscribe(UniSubscriber<? super O> downstream) {
    Context context = downstream.context();
    Uni<O> uni;
    try {
        uni = builder.apply(upstream, context);
        if (uni == null) {
            downstream.onSubscribe(EmptyUniSubscription.DONE);
            downstream.onFailure(new NullPointerException("The builder function returned null"));
            return;
        }
    } catch (Throwable err) {
        downstream.onSubscribe(EmptyUniSubscription.DONE);
        downstream.onFailure(err);
        return;
    }
    Uni<O> pipelineWithContext = Infrastructure.onUniCreation(uni);
    AbstractUni.subscribe(pipelineWithContext, downstream);
}
Also used : Context(io.smallrye.mutiny.Context)

Aggregations

Context (io.smallrye.mutiny.Context)15 Test (org.junit.jupiter.api.Test)9 UniSubscription (io.smallrye.mutiny.subscription.UniSubscription)5 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 IOException (java.io.IOException)4 ContextSupport (io.smallrye.mutiny.subscription.ContextSupport)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Multi (io.smallrye.mutiny.Multi)2 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Uni (io.smallrye.mutiny.Uni)1 UniSubscriber (io.smallrye.mutiny.subscription.UniSubscriber)1 Assertions (org.junit.jupiter.api.Assertions)1