Search in sources :

Example 6 with MultiEmitter

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

the class EmitterBasedMultiTest method testRegistrationOfOnTerminationAndCompletionRace.

@RepeatedTest(100)
public void testRegistrationOfOnTerminationAndCompletionRace() {
    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();
    List<Runnable> runnables = new ArrayList<>();
    runnables.add(() -> reference.get().emit(1).complete());
    runnables.add(() -> reference.get().onTermination(() -> called.set(true)));
    Collections.shuffle(runnables);
    runnables.forEach(r -> new Thread(r).start());
    await().untilAsserted(() -> {
        subscriber.assertItems(1).assertCompleted();
        assertThat(called).isTrue();
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 7 with MultiEmitter

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

the class EmitterBasedMultiTest method testLateReplacementOfOnTerminationAfterCompletion.

@Test
public void testLateReplacementOfOnTerminationAfterCompletion() {
    AtomicBoolean called1 = new AtomicBoolean();
    AtomicBoolean called2 = new AtomicBoolean();
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(e -> {
        e.onTermination(() -> called1.set(true));
        reference.set(e);
    }).subscribe().withSubscriber(AssertSubscriber.create(2));
    subscriber.assertSubscribed().assertNotTerminated();
    reference.get().emit(1);
    reference.get().emit(2);
    subscriber.assertItems(1, 2).assertNotTerminated();
    assertThat(called1).isFalse();
    reference.get().complete();
    subscriber.assertCompleted();
    assertThat(called1).isTrue();
    assertThat(called2).isFalse();
    reference.get().onTermination(() -> called2.set(true));
    assertThat(called2).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 8 with MultiEmitter

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

the class EmitterBasedMultiTest method testSerializedWithConcurrentEmissions.

@Test
public void testSerializedWithConcurrentEmissions() {
    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);
        }
    };
    ExecutorService service = Executors.newFixedThreadPool(2);
    service.submit(r1);
    service.submit(r2);
    await().until(() -> subscriber.getItems().size() == 1200);
    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 9 with MultiEmitter

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

the class MultiConvertToTest method testCreatingAMaybeWithSubscriberAlreadyCancelled.

@Test
public void testCreatingAMaybeWithSubscriberAlreadyCancelled() {
    AtomicBoolean cancelled = new AtomicBoolean();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Maybe<Integer> maybe = Multi.createFrom().<Integer>emitter(emitter::set).onCancellation().invoke(() -> cancelled.set(true)).convert().with(MultiRx3Converters.toMaybe());
    assertThat(maybe).isNotNull();
    TestObserver<Integer> observer = maybe.test();
    observer.dispose();
    emitter.get().emit(1).complete();
    assertThat(observer.isDisposed()).isTrue();
    observer.assertEmpty();
    assertThat(cancelled).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 10 with MultiEmitter

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

the class MultiCreateFromEmitterTest method testThatWeCanHaveMultipleSubscribers.

@Test
public void testThatWeCanHaveMultipleSubscribers() {
    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);
    });
    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)

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