Search in sources :

Example 1 with Cancellable

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

Example 2 with Cancellable

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

Example 3 with Cancellable

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

Example 4 with Cancellable

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

Example 5 with Cancellable

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

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