use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiOnEventTest method testCallWithSubFailure.
@Test
public void testCallWithSubFailure() {
AtomicInteger res = new AtomicInteger(-1);
AtomicInteger twoGotCalled = new AtomicInteger(-1);
Multi<Integer> more = Multi.createFrom().items(1, 2, 3);
Uni<Integer> failing = Uni.createFrom().item(23).onItem().invoke(twoGotCalled::set).onItem().failWith(k -> new IllegalStateException("boom-" + k));
assertThatThrownBy(() -> more.onItem().call(i -> {
res.set(i);
if (i == 2) {
return failing;
}
return Uni.createFrom().nullItem();
}).collect().asList().await().indefinitely()).isInstanceOf(IllegalStateException.class).hasMessageContaining("boom-2");
assertThat(twoGotCalled).hasValue(23);
assertThat(res).hasValue(2);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiRepetitionTest method testWithEmitterWithSharedState.
@Test
public void testWithEmitterWithSharedState() {
AtomicInteger shared = new AtomicInteger();
Multi<Integer> multi = Multi.createBy().repeating().<AtomicInteger, Integer>uni(() -> shared, (state, emitter) -> emitter.complete(state.incrementAndGet())).atMost(2);
assertThat(shared).hasValue(0);
AssertSubscriber<Integer> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(1));
subscriber.assertItems(1);
assertThat(shared).hasValue(1);
subscriber.request(1);
subscriber.assertCompleted().assertItems(1, 2);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiRepetitionTest method testWithUniWithSharedState.
@Test
public void testWithUniWithSharedState() {
AtomicInteger shared = new AtomicInteger();
Multi<Integer> multi = Multi.createBy().repeating().uni(() -> shared, (state) -> Uni.createFrom().item(state.incrementAndGet())).atMost(2);
assertThat(shared).hasValue(0);
AssertSubscriber<Integer> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(1));
subscriber.assertItems(1);
assertThat(shared).hasValue(1);
subscriber.request(1);
subscriber.assertCompleted().assertItems(1, 2);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class UniZipTest method testWithArraysAndMultipleFailures.
@Test
public void testWithArraysAndMultipleFailures() {
// We need 10 unis to avoid being handled as tuples
Uni<Integer> uni1 = Uni.createFrom().item(1);
Uni<Integer> uni2 = Uni.createFrom().item(2);
Uni<Integer> uni3 = Uni.createFrom().item(3);
Uni<Integer> uni4 = Uni.createFrom().failure(new UncheckedIOException(new IOException("io")));
Uni<Integer> uni5 = Uni.createFrom().item(5);
Uni<Integer> uni6 = Uni.createFrom().failure(new ArithmeticException("boom"));
Uni<Integer> uni7 = Uni.createFrom().item(7);
Uni<Integer> uni8 = Uni.createFrom().item(8);
Uni<Integer> uni9 = Uni.createFrom().item(9);
Uni<Integer> uni10 = Uni.createFrom().failure(new IllegalStateException("state"));
assertThatThrownBy(() -> Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5, uni6, uni7, uni8, uni9, uni10).combinedWith(l -> l.stream().mapToInt(o -> (Integer) o).sum()).await().indefinitely()).isInstanceOf(UncheckedIOException.class).hasMessageContaining("io");
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiFromResourceFromUniTest method cancellationShouldBePossible.
@Test
public void cancellationShouldBePossible() {
AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
Supplier<Uni<Integer>> supplier = () -> Uni.createFrom().item(1);
AtomicInteger onFailure = new AtomicInteger();
AtomicInteger onComplete = new AtomicInteger();
AtomicInteger onCancellation = new AtomicInteger();
BiFunction<Integer, Throwable, Uni<Void>> onFailureCallback = (s, f) -> {
onFailure.set(s);
return Uni.createFrom().voidItem();
};
Function<Integer, Uni<Void>> onCompletionCallback = s -> {
onComplete.set(s);
return Uni.createFrom().voidItem();
};
Function<Integer, Uni<Void>> onCancellationCallback = s -> {
onCancellation.set(s);
return Uni.createFrom().voidItem();
};
Multi.createFrom().<Integer, Integer>resourceFromUni(supplier, r -> Multi.createFrom().nothing()).withFinalizer(onCompletionCallback, onFailureCallback, onCancellationCallback).subscribe(subscriber);
subscriber.cancel();
assertThat(onFailure).hasValue(0);
assertThat(onCancellation).hasValue(1);
assertThat(onComplete).hasValue(0);
}
Aggregations