use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class DroppedExceptionsTest method testCaptureOfDroppedExeption.
@Test
public void testCaptureOfDroppedExeption() {
AtomicReference<Throwable> box = new AtomicReference<>();
Infrastructure.setDroppedExceptionHandler(box::set);
Cancellable cancellable = Uni.createFrom().emitter(e -> {
// Never emit anything
}).onCancellation().call(() -> Uni.createFrom().failure(new IOException("boom"))).subscribe().with(item -> {
// Nothing to see here anyway
});
cancellable.cancel();
assertThat(box.get()).isInstanceOf(IOException.class).hasMessage("boom");
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class DroppedExceptionsTest method testCaptureOfDroppedExeptionWithDefaultHandler.
@Test
public void testCaptureOfDroppedExeptionWithDefaultHandler() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
System.setErr(printStream);
Cancellable cancellable = Uni.createFrom().emitter(e -> {
// Never emit anything
}).onCancellation().call(() -> Uni.createFrom().failure(new IOException("boom"))).subscribe().with(item -> {
// Nothing to see here anyway
});
cancellable.cancel();
assertThat(outputStream.toString()).contains("[-- Mutiny had to drop the following exception --]").contains("boom").contains("java.io.IOException").contains("io.smallrye.mutiny.infrastructure.DroppedExceptionsTest").contains("[------------------------------------------------]");
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class UniSubscribeToCompletionStage method subscribe.
public static <T> CompletableFuture<T> subscribe(Uni<T> uni, Context context) {
final AtomicReference<Cancellable> cancellable = new AtomicReference<>();
CompletableFuture<T> future = new CompletableFuture<T>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = super.cancel(mayInterruptIfRunning);
if (cancelled) {
Cancellable c = cancellable.get();
if (c != null) {
c.cancel();
}
}
return cancelled;
}
};
cancellable.set(uni.subscribe().with(context, future::complete, future::completeExceptionally));
return Infrastructure.wrapCompletableFuture(future);
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class ToMaybe method apply.
@Override
public Maybe<T> apply(Multi<T> multi) {
return Maybe.create(emitter -> {
Cancellable cancellable = multi.subscribe().with(item -> {
emitter.onSuccess(item);
emitter.onComplete();
}, emitter::onError, emitter::onComplete);
emitter.setCancellable(cancellable::cancel);
});
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class UniMemoizeTest method testTimeoutOfSecondSubscriber.
/**
* Test reproducing https://github.com/smallrye/smallrye-mutiny/issues/460
*/
@RepeatedTest(10)
public void testTimeoutOfSecondSubscriber() {
Uni<String> uni = Uni.createFrom().item("hello").onItem().delayIt().by(Duration.ofMillis(500)).memoize().indefinitely();
AtomicReference<String> reference = new AtomicReference<>();
AtomicBoolean cancelled = new AtomicBoolean();
Cancellable cancellable = uni.onCancellation().invoke(() -> cancelled.set(true)).subscribe().with(reference::set);
uni.ifNoItem().after(Duration.ofMillis(100)).fail().onFailure(TimeoutException.class).invoke(failure -> cancellable.cancel()).onFailure().recoverWithItem(() -> null).await().indefinitely();
assertThat(reference).hasValue(null);
assertThat(cancelled).isTrue();
}
Aggregations