Search in sources :

Example 11 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription 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 12 with UniSubscription

use of io.smallrye.mutiny.subscription.UniSubscription in project smallrye-mutiny by smallrye.

the class UniBlockingAwait method await.

public static <T> T await(Uni<T> upstream, Duration duration, Context context) {
    nonNull(upstream, "upstream");
    validate(duration);
    if (!Infrastructure.canCallerThreadBeBlocked()) {
        throw new IllegalStateException("The current thread cannot be blocked: " + Thread.currentThread().getName());
    }
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<T> reference = new AtomicReference<>();
    AtomicReference<Throwable> referenceToFailure = new AtomicReference<>();
    UniSubscriber<T> subscriber = new UniSubscriber<T>() {

        @Override
        public Context context() {
            return (context != null) ? context : Context.empty();
        }

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

        @Override
        public void onItem(T item) {
            reference.set(item);
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable failure) {
            referenceToFailure.compareAndSet(null, failure);
            latch.countDown();
        }
    };
    AbstractUni.subscribe(upstream, subscriber);
    try {
        if (duration != null) {
            if (!latch.await(duration.toMillis(), TimeUnit.MILLISECONDS)) {
                referenceToFailure.compareAndSet(null, new TimeoutException());
            }
        } else {
            latch.await();
        }
    } catch (InterruptedException e) {
        referenceToFailure.compareAndSet(null, e);
        Thread.currentThread().interrupt();
    }
    Throwable throwable = referenceToFailure.get();
    if (throwable != null) {
        if (throwable instanceof RuntimeException) {
            throw (RuntimeException) throwable;
        }
        throw new CompletionException(throwable);
    } else {
        return reference.get();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) CountDownLatch(java.util.concurrent.CountDownLatch) CompletionException(java.util.concurrent.CompletionException) UniSubscriber(io.smallrye.mutiny.subscription.UniSubscriber) TimeoutException(io.smallrye.mutiny.TimeoutException)

Aggregations

UniSubscription (io.smallrye.mutiny.subscription.UniSubscription)12 Test (org.junit.jupiter.api.Test)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Context (io.smallrye.mutiny.Context)5 IOException (java.io.IOException)5 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)2 UniSubscriber (io.smallrye.mutiny.subscription.UniSubscriber)2 TimeoutException (io.smallrye.mutiny.TimeoutException)1 CompletionException (java.util.concurrent.CompletionException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1