Search in sources :

Example 26 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.

the class MultiOnEventTest method testCallWithSubFailure.

@Test
public void testCallWithSubFailure() {
    AtomicInteger res = new AtomicInteger(-1);
    AtomicInteger twoGotCalled = new AtomicInteger(-1);
    Multi<Integer> more = Multi.createFrom().items(1, 2, 3);
    Uni<Integer> failing = Uni.createFrom().item(23).onItem().invoke(twoGotCalled::set).onItem().failWith(k -> new IllegalStateException("boom-" + k));
    assertThatThrownBy(() -> more.onItem().call(i -> {
        res.set(i);
        if (i == 2) {
            return failing;
        }
        return Uni.createFrom().nullItem();
    }).collect().asList().await().indefinitely()).isInstanceOf(IllegalStateException.class).hasMessageContaining("boom-2");
    assertThat(twoGotCalled).hasValue(23);
    assertThat(res).hasValue(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) BroadcastProcessor(io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor) Predicate(java.util.function.Predicate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 27 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.

the class MultiRepetitionTest method testWithEmitterWithSharedState.

@Test
public void testWithEmitterWithSharedState() {
    AtomicInteger shared = new AtomicInteger();
    Multi<Integer> multi = Multi.createBy().repeating().<AtomicInteger, Integer>uni(() -> shared, (state, emitter) -> emitter.complete(state.incrementAndGet())).atMost(2);
    assertThat(shared).hasValue(0);
    AssertSubscriber<Integer> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(1));
    subscriber.assertItems(1);
    assertThat(shared).hasValue(1);
    subscriber.request(1);
    subscriber.assertCompleted().assertItems(1, 2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 28 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.

the class MultiRepetitionTest method testWithUniWithSharedState.

@Test
public void testWithUniWithSharedState() {
    AtomicInteger shared = new AtomicInteger();
    Multi<Integer> multi = Multi.createBy().repeating().uni(() -> shared, (state) -> Uni.createFrom().item(state.incrementAndGet())).atMost(2);
    assertThat(shared).hasValue(0);
    AssertSubscriber<Integer> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(1));
    subscriber.assertItems(1);
    assertThat(shared).hasValue(1);
    subscriber.request(1);
    subscriber.assertCompleted().assertItems(1, 2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 29 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.

the class UniZipTest method testWithArraysAndMultipleFailures.

@Test
public void testWithArraysAndMultipleFailures() {
    // We need 10 unis to avoid being handled as tuples
    Uni<Integer> uni1 = Uni.createFrom().item(1);
    Uni<Integer> uni2 = Uni.createFrom().item(2);
    Uni<Integer> uni3 = Uni.createFrom().item(3);
    Uni<Integer> uni4 = Uni.createFrom().failure(new UncheckedIOException(new IOException("io")));
    Uni<Integer> uni5 = Uni.createFrom().item(5);
    Uni<Integer> uni6 = Uni.createFrom().failure(new ArithmeticException("boom"));
    Uni<Integer> uni7 = Uni.createFrom().item(7);
    Uni<Integer> uni8 = Uni.createFrom().item(8);
    Uni<Integer> uni9 = Uni.createFrom().item(9);
    Uni<Integer> uni10 = Uni.createFrom().failure(new IllegalStateException("state"));
    assertThatThrownBy(() -> Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5, uni6, uni7, uni8, uni9, uni10).combinedWith(l -> l.stream().mapToInt(o -> (Integer) o).sum()).await().indefinitely()).isInstanceOf(UncheckedIOException.class).hasMessageContaining("io");
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CsvSource(org.junit.jupiter.params.provider.CsvSource) Arrays(java.util.Arrays) io.smallrye.mutiny.tuples(io.smallrye.mutiny.tuples) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UniAssertSubscriber(io.smallrye.mutiny.helpers.test.UniAssertSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Nested(org.junit.jupiter.api.Nested) Uni(io.smallrye.mutiny.Uni) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) UniSubscription(io.smallrye.mutiny.subscription.UniSubscription) Awaitility.await(org.awaitility.Awaitility.await) TimeoutException(io.smallrye.mutiny.TimeoutException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LIST(org.assertj.core.api.InstanceOfAssertFactories.LIST) Collections(java.util.Collections) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-mutiny by smallrye.

the class MultiFromResourceFromUniTest method cancellationShouldBePossible.

@Test
public void cancellationShouldBePossible() {
    AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
    Supplier<Uni<Integer>> supplier = () -> Uni.createFrom().item(1);
    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().<Integer, Integer>resourceFromUni(supplier, r -> Multi.createFrom().nothing()).withFinalizer(onCompletionCallback, onFailureCallback, onCancellationCallback).subscribe(subscriber);
    subscriber.cancel();
    assertThat(onFailure).hasValue(0);
    assertThat(onCancellation).hasValue(1);
    assertThat(onComplete).hasValue(0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UniOnCancellationSpy(io.smallrye.mutiny.helpers.spies.UniOnCancellationSpy) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Spy(io.smallrye.mutiny.helpers.spies.Spy) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Uni(io.smallrye.mutiny.Uni) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Aggregations

Uni (io.smallrye.mutiny.Uni)44 Multi (io.smallrye.mutiny.Multi)21 Test (org.junit.jupiter.api.Test)18 List (java.util.List)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Duration (java.time.Duration)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 IOException (java.io.IOException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)13 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)12 Function (java.util.function.Function)12 Supplier (java.util.function.Supplier)12 Map (java.util.Map)11 Consumer (java.util.function.Consumer)10 CompositeException (io.smallrye.mutiny.CompositeException)9 ArrayList (java.util.ArrayList)9 ApplicationScoped (javax.enterprise.context.ApplicationScoped)8 CompletableFuture (java.util.concurrent.CompletableFuture)7