Search in sources :

Example 11 with MultiEmitter

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

the class MultiCreateFromEmitterTest method testWithDropBackPressure.

@Test
public void testWithDropBackPressure() {
    AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create(20)).request(Long.MAX_VALUE).assertCompleted();
    // 20 because the 20 first are consumed, others are dropped
    assertThat(subscriber.getItems()).hasSize(20);
    subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertCompleted();
    assertThat(subscriber.getItems()).isEmpty();
    Multi.createFrom().<Integer>emitter(MultiEmitter::complete, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create(20)).assertCompleted().assertHasNotReceivedAnyItem();
    Multi.createFrom().<Integer>emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.fail(new IOException("boom"));
    }, BackPressureStrategy.DROP).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertFailedWith(IOException.class, "boom").assertHasNotReceivedAnyItem();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 12 with MultiEmitter

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

the class MultiCreateFromEmitterTest method testWithMoreRequestsThanValue.

@Test
public void testWithMoreRequestsThanValue() {
    AtomicInteger terminated = new AtomicInteger();
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    Multi.createFrom().<Integer>emitter(emitter -> {
        reference.set(emitter);
        emitter.onTermination(terminated::incrementAndGet);
        emitter.emit(1);
        emitter.emit(2);
        emitter.emit(3);
        emitter.complete();
    }).subscribe().withSubscriber(AssertSubscriber.create()).assertSubscribed().run(() -> {
        assertThat(reference.get()).isNotNull();
        assertThat(reference.get().requested()).isEqualTo(0);
    }).request(2).run(() -> {
        // Already emitted
        assertThat(reference.get().requested()).isEqualTo(0);
    }).assertNotTerminated().assertItems(1, 2).request(10).assertItems(1, 2, 3).run(() -> {
        assertThat(reference.get().requested()).isEqualTo(10);
    }).assertCompleted();
    assertThat(terminated).hasValue(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 13 with MultiEmitter

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

the class MultiCreateFromEmitterTest method testWithRequests.

@Test
public void testWithRequests() {
    AtomicInteger terminated = new AtomicInteger();
    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    Multi.createFrom().<Integer>emitter(emitter -> {
        reference.set(emitter);
        emitter.onTermination(terminated::incrementAndGet);
        emitter.emit(1);
        emitter.emit(2);
        emitter.emit(3);
        emitter.complete();
    }).subscribe().withSubscriber(AssertSubscriber.create()).assertSubscribed().run(() -> {
        assertThat(reference.get()).isNotNull();
        assertThat(reference.get().requested()).isEqualTo(0);
    }).request(2).run(() -> {
        // Already emitted
        assertThat(reference.get().requested()).isEqualTo(0);
    }).assertNotTerminated().assertItems(1, 2).request(2).assertItems(1, 2, 3).assertCompleted();
    assertThat(terminated).hasValue(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Awaitility.await(org.awaitility.Awaitility.await) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) BufferOverflowException(java.nio.BufferOverflowException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackPressureStrategy(io.smallrye.mutiny.subscription.BackPressureStrategy) IOException(java.io.IOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BackPressureFailure(io.smallrye.mutiny.subscription.BackPressureFailure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) Subscriber(org.reactivestreams.Subscriber) 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 14 with MultiEmitter

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

the class MultiOnOverflowTest method testDropCallFailedUni.

@Test
public void testDropCallFailedUni() {
    AssertSubscriber<Integer> sub = AssertSubscriber.create();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set).onOverflow().call(item -> Uni.createFrom().failure(new IOException("boom :: " + item))).drop();
    multi.subscribe(sub);
    emitter.get().emit(1);
    sub.assertFailedWith(IOException.class, "boom :: 1");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) BackPressureFailure(io.smallrye.mutiny.subscription.BackPressureFailure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) MultiOnCancellationSpy(io.smallrye.mutiny.helpers.spies.MultiOnCancellationSpy) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) InfrastructureResource(junit5.support.InfrastructureResource) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) Subscription(org.reactivestreams.Subscription) Spy(io.smallrye.mutiny.helpers.spies.Spy) Mockito.mock(org.mockito.Mockito.mock) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Consumer(java.util.function.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) Test(org.junit.jupiter.api.Test)

Example 15 with MultiEmitter

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

the class MultiCreateFromEmitterTest method testWithLatestBackPressure.

@Test
public void testWithLatestBackPressure() {
    AssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create(20)).request(Long.MAX_VALUE).assertCompleted();
    // 21 because the 20 first are consumed, and then only the latest is kept.
    assertThat(subscriber.getItems()).hasSize(21);
    subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertCompleted();
    assertThat(subscriber.getItems()).hasSize(1).containsExactly(999);
    Multi.createFrom().<Integer>emitter(MultiEmitter::complete, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create(20)).assertCompleted().assertHasNotReceivedAnyItem();
    subscriber = Multi.createFrom().<Integer>emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.fail(new IOException("boom"));
    }, BackPressureStrategy.LATEST).subscribe().withSubscriber(AssertSubscriber.create()).request(20).request(Long.MAX_VALUE).assertFailedWith(IOException.class, "boom");
    assertThat(subscriber.getItems()).hasSize(1).containsExactly(999);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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