Search in sources :

Example 21 with MultiEmitter

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

the class MultiCreateFromEmitterTest method testThatWeCanHaveMultipleSubscribersWhenUsingBackPressure.

@Test
public void testThatWeCanHaveMultipleSubscribersWhenUsingBackPressure() {
    AtomicInteger count = new AtomicInteger();
    List<MultiEmitter<? super Integer>> emitters = new ArrayList<>();
    Multi<Integer> multi = Multi.createFrom().emitter(e -> {
        int i = count.incrementAndGet();
        emitters.add(e);
        e.emit(i);
        e.emit(i);
    }, BackPressureStrategy.DROP);
    AssertSubscriber<Integer> subscriber1 = multi.subscribe().withSubscriber(AssertSubscriber.create(10)).assertItems(1, 1).assertNotTerminated();
    AssertSubscriber<Integer> subscriber2 = multi.subscribe().withSubscriber(AssertSubscriber.create(10)).assertItems(2, 2).assertNotTerminated();
    emitters.forEach(MultiEmitter::complete);
    subscriber1.assertCompleted();
    subscriber2.assertCompleted();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 22 with MultiEmitter

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

the class EmitterBasedMultiTest method testSerializedWithConcurrentEmissionsAndFailure.

@Test
public void testSerializedWithConcurrentEmissionsAndFailure() {
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(reference::set).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
    await().until(() -> reference.get() != null);
    CountDownLatch latch = new CountDownLatch(2);
    Runnable r1 = () -> {
        latch.countDown();
        try {
            latch.await();
        } catch (InterruptedException e) {
        // Ignore me.
        }
        for (int i = 0; i < 1000; i++) {
            reference.get().emit(i);
        }
    };
    Runnable r2 = () -> {
        latch.countDown();
        try {
            latch.await();
        } catch (InterruptedException e) {
        // Ignore me.
        }
        for (int i = 0; i < 200; i++) {
            reference.get().emit(i);
        }
        reference.get().fail(new Exception("boom"));
    };
    ExecutorService service = Executors.newFixedThreadPool(2);
    service.submit(r1);
    service.submit(r2);
    subscriber.awaitFailure().assertFailedWith(Exception.class, "boom");
    service.shutdown();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 23 with MultiEmitter

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

the class EmitterBasedMultiTest method testLateRegistrationOfOnTerminationAfterCancellation.

@Test
public void testLateRegistrationOfOnTerminationAfterCancellation() {
    AtomicBoolean called = new AtomicBoolean();
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(reference::set).subscribe().withSubscriber(AssertSubscriber.create(2));
    subscriber.assertSubscribed().assertNotTerminated();
    reference.get().emit(1);
    reference.get().emit(2);
    subscriber.assertItems(1, 2).assertNotTerminated();
    subscriber.cancel();
    reference.get().onTermination(() -> called.set(true));
    assertThat(called).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 24 with MultiEmitter

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

the class EmitterBasedMultiTest method testLateRegistrationOfOnTerminationAfterFailure.

@Test
public void testLateRegistrationOfOnTerminationAfterFailure() {
    AtomicBoolean called = new AtomicBoolean();
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(reference::set).subscribe().withSubscriber(AssertSubscriber.create(2));
    subscriber.assertSubscribed().assertNotTerminated();
    reference.get().emit(1);
    reference.get().emit(2);
    subscriber.assertItems(1, 2).assertNotTerminated();
    reference.get().fail(new Exception("boom"));
    subscriber.assertFailedWith(Exception.class, "boom");
    reference.get().onTermination(() -> called.set(true));
    assertThat(called).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest) 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