Search in sources :

Example 16 with Uni

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

the class MutinyBulkheadTest method bulkhead.

@Test
public void bulkhead() throws Exception {
    FaultTolerance<Uni<String>> guarded = MutinyFaultTolerance.<String>create().withBulkhead().limit(5).queueSize(5).done().withFallback().handler(this::fallback).applyOn(BulkheadException.class).done().withThreadOffload(true).build();
    Party party = Party.create(5);
    for (int i = 0; i < 10; i++) {
        guarded.call(() -> {
            party.participant().attend();
            return Uni.createFrom().item("ignored");
        }).subscribeAsCompletionStage();
    }
    party.organizer().waitForAll();
    assertThat(guarded.call(() -> Uni.createFrom().item("value")).subscribeAsCompletionStage()).succeedsWithin(10, TimeUnit.SECONDS).isEqualTo("fallback");
    party.organizer().disband();
}
Also used : Uni(io.smallrye.mutiny.Uni) Party(io.smallrye.faulttolerance.core.util.party.Party) BulkheadException(org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException) Test(org.junit.jupiter.api.Test)

Example 17 with Uni

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

the class MultiFromResourceTest method testThatOnFailureFinalizerIsNotCallIfResourceSupplierThrowsAnException.

@Test
public void testThatOnFailureFinalizerIsNotCallIfResourceSupplierThrowsAnException() {
    AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
    Supplier<Integer> supplier = () -> {
        throw new NullPointerException("boom");
    };
    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().resource(supplier, r -> Multi.createFrom().range(r, 11)).withFinalizer(onCompletionCallback, onFailureCallback, onCancellationCallback).subscribe(subscriber);
    subscriber.assertFailedWith(NullPointerException.class, "boom");
    assertThat(onFailure).hasValue(0);
    assertThat(onCancellation).hasValue(0);
    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) 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) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Uni(io.smallrye.mutiny.Uni) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 18 with Uni

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

the class UniRepeatTest method testRepeatWhilstWithDelay.

@RepeatedTest(10)
public void testRepeatWhilstWithDelay() {
    List<Long> times = new ArrayList<>();
    Duration delay = Duration.ofMillis(100);
    Page page1 = new Page(Arrays.asList(1, 2, 3), 1);
    Page page2 = new Page(Arrays.asList(4, 5, 6), 2);
    Page page3 = new Page(Arrays.asList(7, 8), -1);
    Page[] pages = new Page[] { page1, page2, page3 };
    AtomicInteger cursor = new AtomicInteger();
    AssertSubscriber<Integer> subscriber = Multi.createBy().repeating().uni(() -> Uni.createFrom().item(pages[cursor.getAndIncrement()]).onItem().invoke(() -> times.add(System.currentTimeMillis()))).withDelay(delay).whilst(p -> p.next != -1).onItem().transformToMulti(p -> Multi.createFrom().iterable(p.items)).concatenate().subscribe().withSubscriber(AssertSubscriber.create(50));
    subscriber.await().assertItems(1, 2, 3, 4, 5, 6, 7, 8);
    assertThat(times).hasSize(3);
    for (int i = 0; i < times.size() - 1; i++) {
        assertThat(times.get(i) + delay.toMillis()).isLessThanOrEqualTo(times.get(i + 1));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) java.util(java.util) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) Supplier(java.util.function.Supplier) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mocks(io.smallrye.mutiny.test.Mocks) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 19 with Uni

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

the class UniRepeatTest method testRepeatWhilst.

@RepeatedTest(10)
public void testRepeatWhilst() {
    Page page1 = new Page(Arrays.asList(1, 2, 3), 1);
    Page page2 = new Page(Arrays.asList(4, 5, 6), 2);
    Page page3 = new Page(Arrays.asList(7, 8), -1);
    Page[] pages = new Page[] { page1, page2, page3 };
    AtomicInteger cursor = new AtomicInteger();
    AssertSubscriber<Integer> subscriber = Multi.createBy().repeating().uni(() -> Uni.createFrom().item(pages[cursor.getAndIncrement()])).whilst(p -> p.next != -1).onItem().transformToMulti(p -> Multi.createFrom().iterable(p.items)).concatenate().subscribe().withSubscriber(AssertSubscriber.create(50));
    subscriber.assertCompleted().assertItems(1, 2, 3, 4, 5, 6, 7, 8);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) java.util(java.util) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) Supplier(java.util.function.Supplier) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mocks(io.smallrye.mutiny.test.Mocks) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 20 with Uni

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

the class UniFromPublisherTest method testThatValueIsNotEmittedBeforeSubscription.

@SuppressWarnings("ConstantConditions")
@Test
public void testThatValueIsNotEmittedBeforeSubscription() {
    UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
    AtomicBoolean called = new AtomicBoolean();
    Uni<Integer> uni = Uni.createFrom().publisher(Flowable.generate(emitter -> {
        called.set(true);
        emitter.onNext(1);
        emitter.onComplete();
    }));
    assertThat(called).isFalse();
    uni.subscribe().withSubscriber(subscriber);
    subscriber.assertCompleted().assertItem(1);
    assertThat(called).isTrue();
}
Also used : Test(org.junit.jupiter.api.Test) BackpressureStrategy(io.reactivex.BackpressureStrategy) Flowable(io.reactivex.Flowable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UniAssertSubscriber(io.smallrye.mutiny.helpers.test.UniAssertSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Uni(io.smallrye.mutiny.Uni) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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