Search in sources :

Example 16 with MultiEmitter

use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-reactive-messaging by smallrye.

the class ThrowingEmitter method create.

public static <T> Multi<T> create(Consumer<MultiEmitter<? super T>> deferred, long bufferSize) {
    // ThrowingEmitter works by wrapping around a delegate emitter and tracking the requests from downstream so that it can throw an exception from emit() if there aren't sufficient requests
    // If there's no buffer we can use IGNORE since we do our own counting of requests, otherwise we need the delegate to buffer requests for us
    BackPressureStrategy backPressureStrategy = bufferSize == 0 ? BackPressureStrategy.IGNORE : BackPressureStrategy.BUFFER;
    // Use deferred so that we can add a separate on request callback for each subscriber
    return Multi.createFrom().deferred(() -> {
        ThrowingEmitter<T> throwingEmitter = new ThrowingEmitter<>(bufferSize);
        // When someone subscribes, wrap the emitter with our throwing emitter
        Consumer<MultiEmitter<? super T>> consumer = emitter -> {
            throwingEmitter.delegate = emitter;
            deferred.accept(throwingEmitter);
        };
        // Create the Multi and attach the request callback
        return Multi.createFrom().emitter(consumer, backPressureStrategy).onRequest().invoke(throwingEmitter::request);
    });
}
Also used : Consumer(java.util.function.Consumer) Subscriptions(io.smallrye.mutiny.helpers.Subscriptions) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProviderExceptions.ex(io.smallrye.reactive.messaging.providers.i18n.ProviderExceptions.ex) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) BackPressureStrategy(io.smallrye.mutiny.subscription.BackPressureStrategy) Multi(io.smallrye.mutiny.Multi) BackPressureStrategy(io.smallrye.mutiny.subscription.BackPressureStrategy) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter)

Example 17 with MultiEmitter

use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.

the class MultiSubscribeTest method testWith2CallbacksAndCompletion.

@Test
public void testWith2CallbacksAndCompletion() {
    List<Integer> items = new CopyOnWriteArrayList<>();
    AtomicBoolean completion = new AtomicBoolean();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi.createFrom().<Integer>emitter(emitter::set).subscribe().with(items::add, () -> completion.set(true));
    assertThat(items).isEmpty();
    assertThat(completion).isFalse();
    emitter.get().emit(1).emit(2).emit(3).complete();
    assertThat(items).containsExactly(1, 2, 3);
    assertThat(completion).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 18 with MultiEmitter

use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.

the class MultiGroupTest method testGroupByWithUpstreamFailure.

@RepeatedTest(10)
public void testGroupByWithUpstreamFailure() {
    AtomicReference<MultiEmitter<? super Long>> emitter = new AtomicReference<>();
    Multi<Long> multi = Multi.createBy().merging().streams(Multi.createFrom().ticks().every(Duration.ofMillis(2)), Multi.createFrom().emitter((Consumer<MultiEmitter<? super Long>>) emitter::set));
    AssertSubscriber<GroupedMulti<Long, Long>> subscriber = multi.group().by(l -> l % 2).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    subscriber.assertSubscribed();
    await().until(() -> subscriber.getItems().size() == 2);
    AssertSubscriber<Long> s1 = subscriber.getItems().get(0).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    s1.assertSubscribed();
    AssertSubscriber<Long> s2 = subscriber.getItems().get(1).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    s2.assertSubscribed();
    await().until(() -> s1.getItems().size() >= 3);
    await().until(() -> s2.getItems().size() >= 3);
    emitter.get().fail(new TestException("boom"));
    s1.awaitFailure().assertFailedWith(TestException.class, "boom");
    s2.awaitFailure().assertFailedWith(TestException.class, "boom");
    subscriber.assertFailedWith(TestException.class, "boom");
}
Also used : Consumer(java.util.function.Consumer) TestException(io.smallrye.mutiny.TestException) GroupedMulti(io.smallrye.mutiny.GroupedMulti) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 19 with MultiEmitter

use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.

the class MultiCacheTest method testCachingWithDeferredResult.

@Test
public void testCachingWithDeferredResult() {
    AtomicInteger count = new AtomicInteger();
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    Multi<Integer> multi = Multi.createFrom().<Integer>emitter(emitter -> {
        reference.set(emitter);
        emitter.emit(count.incrementAndGet()).emit(count.incrementAndGet());
    }).cache();
    AssertSubscriber<Integer> s1 = multi.subscribe().withSubscriber(AssertSubscriber.create(2)).assertItems(1, 2).assertNotTerminated();
    AssertSubscriber<Integer> s2 = multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)).assertItems(1, 2).assertNotTerminated();
    reference.get().emit(count.incrementAndGet()).complete();
    s1.assertItems(1, 2).request(1).assertItems(1, 2, 3).assertCompleted();
    s2.assertItems(1, 2, 3).assertCompleted();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Multi(io.smallrye.mutiny.Multi) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 20 with MultiEmitter

use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.

the class MultiCombineTest method testCombiningWithFailuresAtSameTimeAndCollectFailure.

@Test
public void testCombiningWithFailuresAtSameTimeAndCollectFailure() {
    AtomicReference<MultiEmitter<? super Integer>> emitter1 = new AtomicReference<>();
    AtomicReference<MultiEmitter<? super Integer>> emitter2 = new AtomicReference<>();
    Multi<Integer> multi = Multi.createFrom().items(1, 2, 3);
    Multi<Integer> multi2 = Multi.createFrom().emitter(e -> {
        emitter1.set(e);
        e.emit(1);
    });
    Multi<Integer> multi3 = Multi.createFrom().emitter(e -> {
        emitter2.set(e);
        e.emit(1);
    });
    Multi.createBy().combining().streams(multi, multi2, multi, multi3).collectFailures().asTuple().subscribe().withSubscriber(AssertSubscriber.create(1)).assertItems(Tuple4.of(1, 1, 1, 1)).request(2).run(() -> {
        emitter1.get().emit(2).fail(new IOException("boomA"));
        emitter2.get().emit(2).fail(new IOException("boomB"));
    }).assertItems(Tuple4.of(1, 1, 1, 1), Tuple4.of(2, 2, 2, 2)).assertFailedWith(IOException.class, "boomA");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Aggregations

MultiEmitter (io.smallrye.mutiny.subscription.MultiEmitter)24 Test (org.junit.jupiter.api.Test)21 AtomicReference (java.util.concurrent.atomic.AtomicReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 RepeatedTest (org.junit.jupiter.api.RepeatedTest)9 IOException (java.io.IOException)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 Multi (io.smallrye.mutiny.Multi)4 ArrayList (java.util.ArrayList)4 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)3 Consumer (java.util.function.Consumer)3 BackPressureFailure (io.smallrye.mutiny.subscription.BackPressureFailure)2 BackPressureStrategy (io.smallrye.mutiny.subscription.BackPressureStrategy)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 Awaitility.await (org.awaitility.Awaitility.await)2 Subscription (org.reactivestreams.Subscription)2 GroupedMulti (io.smallrye.mutiny.GroupedMulti)1