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);
}
}
}));
}
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);
}
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);
}
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);
}
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();
}
Aggregations