use of io.smallrye.mutiny.Uni in project smallrye-fault-tolerance by smallrye.
the class MutinyBulkheadTest method bulkhead.
@Test
public void bulkhead() throws Exception {
FaultTolerance<Uni<String>> guarded = MutinyFaultTolerance.<String>create().withBulkhead().limit(5).queueSize(5).done().withFallback().handler(this::fallback).applyOn(BulkheadException.class).done().withThreadOffload(true).build();
Party party = Party.create(5);
for (int i = 0; i < 10; i++) {
guarded.call(() -> {
party.participant().attend();
return Uni.createFrom().item("ignored");
}).subscribeAsCompletionStage();
}
party.organizer().waitForAll();
assertThat(guarded.call(() -> Uni.createFrom().item("value")).subscribeAsCompletionStage()).succeedsWithin(10, TimeUnit.SECONDS).isEqualTo("fallback");
party.organizer().disband();
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class MultiFromResourceTest method testThatOnFailureFinalizerIsNotCallIfResourceSupplierThrowsAnException.
@Test
public void testThatOnFailureFinalizerIsNotCallIfResourceSupplierThrowsAnException() {
AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
Supplier<Integer> supplier = () -> {
throw new NullPointerException("boom");
};
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().resource(supplier, r -> Multi.createFrom().range(r, 11)).withFinalizer(onCompletionCallback, onFailureCallback, onCancellationCallback).subscribe(subscriber);
subscriber.assertFailedWith(NullPointerException.class, "boom");
assertThat(onFailure).hasValue(0);
assertThat(onCancellation).hasValue(0);
assertThat(onComplete).hasValue(0);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class UniRepeatTest method testRepeatWhilstWithDelay.
@RepeatedTest(10)
public void testRepeatWhilstWithDelay() {
List<Long> times = new ArrayList<>();
Duration delay = Duration.ofMillis(100);
Page page1 = new Page(Arrays.asList(1, 2, 3), 1);
Page page2 = new Page(Arrays.asList(4, 5, 6), 2);
Page page3 = new Page(Arrays.asList(7, 8), -1);
Page[] pages = new Page[] { page1, page2, page3 };
AtomicInteger cursor = new AtomicInteger();
AssertSubscriber<Integer> subscriber = Multi.createBy().repeating().uni(() -> Uni.createFrom().item(pages[cursor.getAndIncrement()]).onItem().invoke(() -> times.add(System.currentTimeMillis()))).withDelay(delay).whilst(p -> p.next != -1).onItem().transformToMulti(p -> Multi.createFrom().iterable(p.items)).concatenate().subscribe().withSubscriber(AssertSubscriber.create(50));
subscriber.await().assertItems(1, 2, 3, 4, 5, 6, 7, 8);
assertThat(times).hasSize(3);
for (int i = 0; i < times.size() - 1; i++) {
assertThat(times.get(i) + delay.toMillis()).isLessThanOrEqualTo(times.get(i + 1));
}
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class UniRepeatTest method testRepeatWhilst.
@RepeatedTest(10)
public void testRepeatWhilst() {
Page page1 = new Page(Arrays.asList(1, 2, 3), 1);
Page page2 = new Page(Arrays.asList(4, 5, 6), 2);
Page page3 = new Page(Arrays.asList(7, 8), -1);
Page[] pages = new Page[] { page1, page2, page3 };
AtomicInteger cursor = new AtomicInteger();
AssertSubscriber<Integer> subscriber = Multi.createBy().repeating().uni(() -> Uni.createFrom().item(pages[cursor.getAndIncrement()])).whilst(p -> p.next != -1).onItem().transformToMulti(p -> Multi.createFrom().iterable(p.items)).concatenate().subscribe().withSubscriber(AssertSubscriber.create(50));
subscriber.assertCompleted().assertItems(1, 2, 3, 4, 5, 6, 7, 8);
}
use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.
the class UniFromPublisherTest method testThatValueIsNotEmittedBeforeSubscription.
@SuppressWarnings("ConstantConditions")
@Test
public void testThatValueIsNotEmittedBeforeSubscription() {
UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
AtomicBoolean called = new AtomicBoolean();
Uni<Integer> uni = Uni.createFrom().publisher(Flowable.generate(emitter -> {
called.set(true);
emitter.onNext(1);
emitter.onComplete();
}));
assertThat(called).isFalse();
uni.subscribe().withSubscriber(subscriber);
subscriber.assertCompleted().assertItem(1);
assertThat(called).isTrue();
}
Aggregations