use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class DroppedExceptionTest method droppedExceptionTest.
@Test
public void droppedExceptionTest(SystemOut out) {
// tag::override-handler[]
Infrastructure.setDroppedExceptionHandler(err -> log(Level.SEVERE, "Mutiny dropped exception", err));
// end::override-handler[]
// tag::code[]
Cancellable cancellable = Uni.createFrom().emitter(this::emitter).onCancellation().call(() -> Uni.createFrom().failure(new IOException("boom"))).subscribe().with(this::onItem, this::onFailure);
cancellable.cancel();
// end::code[]
Assertions.assertThat(out.get()).contains("Mutiny dropped exception");
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class CreatingUniTest method subscription.
@Test
void subscription(SystemOut out) {
Uni<Integer> uni = Uni.createFrom().item(1);
// tag::subscription[]
Cancellable cancellable = uni.subscribe().with(item -> System.out.println(item), failure -> System.out.println("Failed with " + failure));
// end::subscription[]
assertThat(cancellable).isNotNull();
assertThat(out.get()).contains("1").doesNotContain("Failed");
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class UniContextPropagationTest method testOnTermination.
@Test
public void testOnTermination() throws InterruptedException {
MyContext ctx = MyContext.get();
AtomicInteger count = new AtomicInteger();
AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
Uni<Integer> uni = Uni.createFrom().emitter((Consumer<UniEmitter<? super Integer>>) reference::set).onTermination().invoke((value, failure, cancellation) -> {
count.incrementAndGet();
assertThat(ctx).isSameAs(MyContext.get());
});
// Test completion
CountDownLatch latch1 = new CountDownLatch(1);
uni.subscribe().with(x -> {
latch1.countDown();
});
new Thread(() -> {
reference.get().complete(23);
}).start();
latch1.await(10, TimeUnit.MILLISECONDS);
assertThat(count).hasValue(1);
// Test failure
CountDownLatch latch2 = new CountDownLatch(1);
uni.subscribe().with(x -> {
}, fail -> {
latch2.countDown();
});
new Thread(() -> {
reference.get().fail(new Exception("boom"));
}).start();
latch2.await(10, TimeUnit.MILLISECONDS);
assertThat(count).hasValue(2);
// Test cancellation
Cancellable cancellable = uni.subscribe().with(x -> {
}, fail -> {
});
new Thread(cancellable::cancel).start();
await().until(() -> count.get() == 3);
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class MultiOnFailure method call.
/**
* Produces a new {@link Multi} invoking the given @{code action} when a {@code failure} event is received.
* <p>
* Unlike {@link #invoke(Consumer)}, the passed function returns a {@link Uni}. When the produced {@code Uni} sends
* its result, the result is discarded, and the original {@code failure} is forwarded downstream. If the produced
* {@code Uni} fails, a {@link io.smallrye.mutiny.CompositeException} composed by the original failure and the
* caught failure is propagated downstream.
* <p>
* If the asynchronous action throws an exception, this exception is propagated downstream as a
* {@link io.smallrye.mutiny.CompositeException} composed with the original failure and the caught exception.
* <p>
* This method preserves the order of the items, meaning that the downstream received the items in the same order
* as the upstream has emitted them.
*
* @param action the function taking the failure and returning a {@link Uni}, must not be {@code null}
* @return the new {@link Multi}
*/
@CheckReturnValue
public Multi<T> call(Function<Throwable, Uni<?>> action) {
Function<Throwable, Uni<?>> actual = Infrastructure.decorate(nonNull(action, "action"));
return recoverWithMulti(failure -> {
Uni<?> uni = actual.apply(failure);
if (uni == null) {
throw new NullPointerException("The `action` produced a `null` Uni");
}
return Multi.createFrom().emitter(emitter -> {
Cancellable cancellable = uni.subscribe().with(success -> emitter.fail(failure), subFailure -> emitter.fail(new CompositeException(failure, subFailure)));
emitter.onTermination(cancellable::cancel);
});
});
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class MultiSubscribeTest method testSubscribeWithItemAndFailure.
@Test
public void testSubscribeWithItemAndFailure() {
List<Long> items = new CopyOnWriteArrayList<>();
AtomicReference<Throwable> failure = new AtomicReference<>();
Cancellable cancellable = Multi.createFrom().ticks().every(Duration.ofMillis(10)).subscribe().with(items::add, failure::set);
await().until(() -> items.size() > 5);
cancellable.cancel();
int s = items.size();
assertThat(items).contains(1L, 2L, 3L, 4L, 5L);
assertThat(failure.get()).isNull();
await().pollDelay(10, TimeUnit.MILLISECONDS).until(() -> items.size() == s);
}
Aggregations