Search in sources :

Example 21 with Uni

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

the class UniOrCombination method subscribe.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void subscribe(UniSubscriber<? super T> subscriber) {
    if (challengers.isEmpty()) {
        subscriber.onSubscribe(EmptyUniSubscription.DONE);
        subscriber.onItem(null);
        return;
    }
    if (challengers.size() == 1) {
        // Just subscribe to the first and unique uni.
        Uni<? super T> uni = challengers.get(0);
        AbstractUni.subscribe(uni, (UniSubscriber) subscriber);
        return;
    }
    // Barrier - once set to {@code true} the signals are ignored
    AtomicBoolean completedOrCancelled = new AtomicBoolean();
    List<CompletableFuture<? super T>> futures = new ArrayList<>();
    challengers.forEach(uni -> {
        CompletableFuture<? super T> future = uni.subscribe().asCompletionStage(subscriber.context());
        futures.add(future);
    });
    // Do not call unSubscribe until we get all the futures.
    // But at the same time we can't start resolving as we didn't give a subscription to the subscriber
    // yet.
    subscriber.onSubscribe(() -> {
        if (completedOrCancelled.compareAndSet(false, true)) {
            // Cancel all
            futures.forEach(cf -> cf.cancel(false));
        }
    });
    // Once the subscription has been given, start resolving
    futures.forEach(future -> future.whenComplete((res, fail) -> {
        if (completedOrCancelled.compareAndSet(false, true)) {
            // Cancel other
            futures.forEach(cf -> {
                if (cf != future) {
                    cf.cancel(false);
                }
            });
            if (fail != null) {
                subscriber.onFailure(fail);
            } else {
                subscriber.onItem((T) res);
            }
        }
    }));
}
Also used : ParameterValidation.nonNull(io.smallrye.mutiny.helpers.ParameterValidation.nonNull) List(java.util.List) AbstractUni(io.smallrye.mutiny.operators.AbstractUni) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) UniOperator(io.smallrye.mutiny.operators.UniOperator) EmptyUniSubscription(io.smallrye.mutiny.helpers.EmptyUniSubscription) UniSubscriber(io.smallrye.mutiny.subscription.UniSubscriber) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList)

Example 22 with Uni

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

the class UniContextPropagationTest method testContextOverride.

@Test
public void testContextOverride() {
    MyContext ctx = MyContext.get();
    assertThat(ctx).isNotNull();
    SmallRyeThreadContext emptyContext = SmallRyeThreadContext.builder().cleared(ThreadContext.ALL_REMAINING).propagated(ThreadContext.NONE).build();
    Uni<Integer> uni;
    // remove context propagation in this scope
    try (CleanAutoCloseable ac = SmallRyeThreadContext.withThreadContext(emptyContext)) {
        uni = Uni.createFrom().item(() -> {
            assertThat(MyContext.get()).isNull();
            return 2;
        }).runSubscriptionOn(executor).map(r -> {
            assertThat(MyContext.get()).isNull();
            return r;
        });
    }
    Uni<Integer> latch = Uni.createFrom().emitter(emitter -> new Thread(() -> {
        try {
            int result = uni.await().indefinitely();
            emitter.complete(result);
        } catch (Throwable t) {
            emitter.fail(t);
        }
    }).start());
    int result = latch.await().indefinitely();
    assertThat(result).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) ThreadContext(org.eclipse.microprofile.context.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.junit.jupiter.api(org.junit.jupiter.api) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext) Duration(java.time.Duration) Cancellable(io.smallrye.mutiny.subscription.Cancellable) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext)

Example 23 with Uni

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

the class MultiContextPropagationTest method testContextOverride.

@Test
public void testContextOverride() {
    MyContext ctx = MyContext.get();
    assertThat(ctx).isNotNull();
    SmallRyeThreadContext emptyContext = SmallRyeThreadContext.builder().cleared(ThreadContext.ALL_REMAINING).propagated(ThreadContext.NONE).build();
    Multi<Integer> multi;
    // remove context propagation in this scope
    try (CleanAutoCloseable ac = SmallRyeThreadContext.withThreadContext(emptyContext)) {
        multi = Multi.createFrom().item(() -> {
            assertThat(MyContext.get()).isNull();
            return 2;
        }).runSubscriptionOn(executor).map(r -> {
            assertThat(MyContext.get()).isNull();
            return r;
        });
    }
    Uni<Integer> latch = Multi.createFrom().<Integer>emitter(emitter -> new Thread(() -> {
        try {
            int result = multi.toUni().await().indefinitely();
            emitter.emit(result);
            emitter.complete();
        } catch (Throwable t) {
            emitter.fail(t);
        }
    }).start()).toUni();
    int result = latch.await().indefinitely();
    assertThat(result).isEqualTo(2);
}
Also used : AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ThreadContext(org.eclipse.microprofile.context.ThreadContext) Multi(io.smallrye.mutiny.Multi) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) Uni(io.smallrye.mutiny.Uni) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) org.junit.jupiter.api(org.junit.jupiter.api) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext) Duration(java.time.Duration) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext)

Example 24 with Uni

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

the class UncheckedTest method invokeExample.

@Test
public void invokeExample() {
    Uni<Integer> item = Uni.createFrom().item(1);
    // tag::invoke[]
    Uni<Integer> uni = item.onItem().invoke(Unchecked.consumer(i -> {
        // Can throw checked exception
        throw new IOException("boom");
    }));
    // end::invoke[]
    assertThatThrownBy(() -> uni.await().indefinitely()).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class);
}
Also used : Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) Unchecked(io.smallrye.mutiny.unchecked.Unchecked) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) IOException(java.io.IOException) Uni(io.smallrye.mutiny.Uni) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 25 with Uni

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

the class UniConvertFromTest method testCreatingFromAMaybeNeverEmitting.

@Test
public void testCreatingFromAMaybeNeverEmitting() {
    AtomicBoolean cancelled = new AtomicBoolean();
    Uni<Integer> uni = Uni.createFrom().converter(UniRx3Converters.fromMaybe(), Maybe.<Integer>never().doOnDispose(() -> cancelled.set(true)));
    assertThat(uni).isNotNull();
    uni.subscribe().with(i -> {
    }).cancel();
    assertThat(cancelled).isTrue();
}
Also used : Test(org.junit.jupiter.api.Test) Maybe(io.reactivex.rxjava3.core.Maybe) Single(io.reactivex.rxjava3.core.Single) UniRx3Converters(io.smallrye.mutiny.converters.uni.UniRx3Converters) Assertions.fail(org.assertj.core.api.Assertions.fail) Flowable(io.reactivex.rxjava3.core.Flowable) Observable(io.reactivex.rxjava3.core.Observable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Completable(io.reactivex.rxjava3.core.Completable) Uni(io.smallrye.mutiny.Uni) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Aggregations

Uni (io.smallrye.mutiny.Uni)44 Multi (io.smallrye.mutiny.Multi)21 Test (org.junit.jupiter.api.Test)18 List (java.util.List)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Duration (java.time.Duration)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 IOException (java.io.IOException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)13 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)12 Function (java.util.function.Function)12 Supplier (java.util.function.Supplier)12 Map (java.util.Map)11 Consumer (java.util.function.Consumer)10 CompositeException (io.smallrye.mutiny.CompositeException)9 ArrayList (java.util.ArrayList)9 ApplicationScoped (javax.enterprise.context.ApplicationScoped)8 CompletableFuture (java.util.concurrent.CompletableFuture)7