Search in sources :

Example 1 with MultiEmitter

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

the class MultiSubscribeTest method testWith2CallbacksAndFailure.

@Test
public void testWith2CallbacksAndFailure() {
    List<Integer> items = new CopyOnWriteArrayList<>();
    AtomicReference<Throwable> failure = new AtomicReference<>();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi.createFrom().<Integer>emitter(emitter::set).subscribe().with(items::add, failure::set);
    assertThat(items).isEmpty();
    assertThat(failure.get()).isNull();
    emitter.get().emit(1).emit(2).emit(3).fail(new IOException("boom"));
    assertThat(items).containsExactly(1, 2, 3);
    assertThat(failure.get()).isInstanceOf(IOException.class).hasMessage("boom");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 2 with MultiEmitter

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

the class MultiSubscribeTest method testWithEmitterWithFailure.

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

Example 3 with MultiEmitter

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

the class MultiSubscribeTest method testWithEmitterWithCompletion.

@Test
public void testWithEmitterWithCompletion() {
    List<Integer> items = new CopyOnWriteArrayList<>();
    AtomicBoolean completion = new AtomicBoolean();
    AtomicReference<Throwable> failure = new AtomicReference<>();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi.createFrom().<Integer>emitter(emitter::set).subscribe().with(items::add, failure::set, () -> completion.set(true));
    assertThat(items).isEmpty();
    assertThat(failure.get()).isNull();
    assertThat(completion).isFalse();
    emitter.get().emit(1).emit(2).emit(3).complete();
    assertThat(items).containsExactly(1, 2, 3);
    assertThat(failure.get()).isNull();
    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 4 with MultiEmitter

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

the class EmitterBasedMultiTest method testReplacementOfOnTerminationBeforeCompletion.

@Test
public void testReplacementOfOnTerminationBeforeCompletion() {
    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().onTermination(() -> called2.set(true));
    reference.get().complete();
    subscriber.assertCompleted();
    assertThat(called1).isFalse();
    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 5 with MultiEmitter

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

the class EmitterBasedMultiTest method testLateRegistrationOfOnTerminationAfterCompletion.

@Test
public void testLateRegistrationOfOnTerminationAfterCompletion() {
    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().complete();
    subscriber.assertCompleted();
    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