Search in sources :

Example 6 with Cancellable

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");
}
Also used : Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 7 with Cancellable

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("[------------------------------------------------]");
}
Also used : PrintStream(java.io.PrintStream) Cancellable(io.smallrye.mutiny.subscription.Cancellable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 8 with Cancellable

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);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 9 with Cancellable

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);
    });
}
Also used : Cancellable(io.smallrye.mutiny.subscription.Cancellable)

Example 10 with Cancellable

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(io.smallrye.mutiny.TimeoutException) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Aggregations

Cancellable (io.smallrye.mutiny.subscription.Cancellable)26 Test (org.junit.jupiter.api.Test)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Uni (io.smallrye.mutiny.Uni)5 IOException (java.io.IOException)5 Duration (java.time.Duration)4 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 GraphQLClientException (io.smallrye.graphql.client.GraphQLClientException)2 GraphQLError (io.smallrye.graphql.client.GraphQLError)2 InvalidResponseException (io.smallrye.graphql.client.InvalidResponseException)2 ResponseReader (io.smallrye.graphql.client.impl.ResponseReader)2 WebSocketSubprotocolHandler (io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler)2 IncrementingNumberOperationIDGenerator (io.smallrye.graphql.client.vertx.websocket.opid.IncrementingNumberOperationIDGenerator)2 OperationIDGenerator (io.smallrye.graphql.client.vertx.websocket.opid.OperationIDGenerator)2 MultiEmitter (io.smallrye.mutiny.subscription.MultiEmitter)2 WebSocket (io.vertx.core.http.WebSocket)2 StringReader (java.io.StringReader)2