io.smallrye.mutiny.subscription.MultiEmitter Java Examples

The following examples show how to use io.smallrye.mutiny.subscription.MultiEmitter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultiSubscribeTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #2
Source File: ThrowingEmitter.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
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).on().request(throwingEmitter::request);
    });
}
 
Example #3
Source File: MultiOnFailureTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecoverWithMultiWithSomeResulsubscriberBeforeFailingWithRequessubscriber() {
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(3);

    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    Multi.createFrom().<Integer> emitter(reference::set)
            .onFailure().recoverWithMulti(v -> Multi.createFrom().range(50, 55))
            .subscribe().withSubscriber(subscriber);

    subscriber.assertSubscribed();

    reference.get().emit(1)
            .emit(2)
            .emit(3)
            .emit(4)
            .emit(5)
            .fail(new IllegalStateException("boom"));

    subscriber.assertReceived(1, 2, 3)
            .request(5)
            .assertReceived(1, 2, 3, 4, 5, 50, 51, 52)
            .request(10)
            .assertHasNotFailed()
            .assertCompletedSuccessfully();
}
 
Example #4
Source File: MultiOnFailureTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecoverWithMultiWithSomeResulsubscriberBeforeFailing() {
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(20);

    AtomicReference<MultiEmitter<? super Integer>> reference = new AtomicReference<>();
    Multi.createFrom().<Integer> emitter(reference::set)
            .onFailure().recoverWithMulti(v -> Multi.createFrom().range(50, 55))
            .subscribe().withSubscriber(subscriber);

    subscriber.assertSubscribed();

    reference.get().emit(1)
            .emit(2)
            .emit(3)
            .emit(4)
            .emit(5)
            .fail(new IllegalStateException("boom"));

    subscriber.assertReceived(1, 2, 3, 4, 5, 50, 51, 52, 53, 54)
            .assertHasNotFailed()
            .assertCompletedSuccessfully();
}
 
Example #5
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorBackPressureBehavior() {
    Multi<Integer> multi = Multi.createFrom().emitter(e -> e.emit(1).emit(2).emit(3).complete(),
            BackPressureStrategy.ERROR);

    multi.subscribe().withSubscriber(MultiAssertSubscriber.create(1))
            .assertSubscribed()
            .assertReceived(1)
            .assertHasFailedWith(BackPressureFailure.class, "requests");

    multi.subscribe().withSubscriber(MultiAssertSubscriber.create(3))
            .assertSubscribed()
            .assertReceived(1, 2, 3)
            .assertCompletedSuccessfully();

    Multi.createFrom().emitter(MultiEmitter::complete, BackPressureStrategy.ERROR)
            .subscribe().withSubscriber(MultiAssertSubscriber.create())
            .assertCompletedSuccessfully()
            .assertHasNotReceivedAnyItem();
}
 
Example #6
Source File: MultiCacheTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@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();
    MultiAssertSubscriber<Integer> s1 = multi
            .subscribe().withSubscriber(MultiAssertSubscriber.create(2))
            .assertReceived(1, 2)
            .assertNotTerminated();

    MultiAssertSubscriber<Integer> s2 = multi.subscribe()
            .withSubscriber(MultiAssertSubscriber.create(Long.MAX_VALUE))
            .assertReceived(1, 2)
            .assertNotTerminated();

    reference.get().emit(count.incrementAndGet()).complete();
    s1.assertReceived(1, 2).request(1).assertReceived(1, 2, 3).assertCompletedSuccessfully();
    s2.assertReceived(1, 2, 3).assertCompletedSuccessfully();
}
 
Example #7
Source File: MultiSubscribeTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #8
Source File: MultiSubscribeTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #9
Source File: MultiSubscribeTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #10
Source File: MultiOnOverflowTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testDropStrategyWithEmitterWithoutCallback() {
    MultiAssertSubscriber<Integer> sub = MultiAssertSubscriber.create();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set)
            .onOverflow().drop();
    multi.subscribe(sub);
    emitter.get().emit(1);
    sub.request(2);
    emitter.get().emit(2).emit(3).emit(4);
    sub.request(1);
    emitter.get().emit(5).complete();
    sub
            .assertCompletedSuccessfully()
            .assertReceived(2, 3, 5);
}
 
Example #11
Source File: MultiOnOverflowTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testDropStrategyWithEmitter() {
    MultiAssertSubscriber<Integer> sub = MultiAssertSubscriber.create();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    List<Integer> list = new CopyOnWriteArrayList<>();
    Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set)
            .onOverflow().drop(list::add);
    multi.subscribe(sub);
    emitter.get().emit(1);
    sub.request(2);
    emitter.get().emit(2).emit(3).emit(4);
    sub.request(1);
    emitter.get().emit(5).complete();
    sub
            .assertCompletedSuccessfully()
            .assertReceived(2, 3, 5);
    assertThat(list).containsExactly(1, 4);
}
 
Example #12
Source File: EmitterBasedMulti.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public final MultiEmitter<T> emit(T t) {
    if (isCancelled()) {
        return this;
    }

    if (t == null) {
        fail(new NullPointerException(
                "`emit` called with `null`."));
        return this;
    }

    if (requested.get() != 0) {
        downstream.onItem(t);
        Subscriptions.produced(requested, 1);
    } else {
        onOverflow();
    }
    return this;
}
 
Example #13
Source File: EmitterBasedMulti.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public MultiEmitter<T> emit(T item) {
    if (isCancelled()) {
        return this;
    }

    if (item != null) {
        downstream.onItem(item);
    } else {
        fail(new NullPointerException("`emit` called with `null`."));
        return this;
    }

    for (;;) {
        long r = requested.get();
        if (r == 0L || requested.compareAndSet(r, r - 1)) {
            return this;
        }
    }
}
 
Example #14
Source File: PeopleProducer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Outgoing("people-out")
public Multi<String> generatePeople() {
    //TODO: this can be replaced with Multi.onItem().delayIt when it exists
    //TODO: this delay should not even be necessary, the queue is created on
    //subscriber connect, so we delay to make sure it is connected
    //we should be able to just define the queue in broker.xml, but that does not
    //work atm, see https://github.com/smallrye/smallrye-reactive-messaging/issues/555
    return Multi.createFrom().emitter(new Consumer<MultiEmitter<? super Person>>() {
        @Override
        public void accept(MultiEmitter<? super Person> multiEmitter) {
            Uni.createFrom().item("dummy").onItem().delayIt().by(Duration.ofSeconds(2))
                    .subscribe().with(new Consumer<String>() {
                        @Override
                        public void accept(String s) {
                            multiEmitter.emit(new Person("bob"));
                            multiEmitter.emit(new Person("alice"));
                            multiEmitter.emit(new Person("tom"));
                            multiEmitter.emit(new Person("jerry"));
                            multiEmitter.emit(new Person("anna"));
                            multiEmitter.emit(new Person("ken"));
                        }
                    });
        }
    }).map(Json::encode);
}
 
Example #15
Source File: MultiIfEmpty.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
static <T> Consumer<MultiEmitter<? super T>> createMultiFromFailureSupplier(Supplier<? extends Throwable> supplier) {
    return emitter -> {
        Throwable throwable;
        try {
            throwable = supplier.get();
        } catch (Throwable e) {
            emitter.fail(e);
            return;
        }

        if (throwable == null) {
            emitter.fail(new NullPointerException(SUPPLIER_PRODUCED_NULL));
        } else {
            emitter.fail(throwable);
        }
    };
}
 
Example #16
Source File: BufferItemMultiEmitter.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public MultiEmitter<T> emit(T t) {
    if (done || isCancelled()) {
        return this;
    }

    if (t == null) {
        fail(new NullPointerException("`emit` called with `null`."));
        return this;
    }
    queue.offer(t);
    drain();
    return this;
}
 
Example #17
Source File: ThrowingEmitter.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public MultiEmitter<T> emit(T item) {
    // Decrement requested without going below zero
    long requests;
    do {
        requests = requested.get();
    } while (requests > 0 && !requested.compareAndSet(requests, requests - 1));

    if (requests <= 0) {
        throw ex.illegalStateInsufficientDownstreamRequests();
    }

    delegate.emit(item);
    return this;
}
 
Example #18
Source File: EmitterImpl.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public EmitterImpl(EmitterConfiguration config, long defaultBufferSize) {
    this.name = config.name;
    if (defaultBufferSize <= 0) {
        throw ex.illegalArgumentForDefaultBuffer();
    }

    Consumer<MultiEmitter<? super Message<? extends T>>> deferred = fe -> {
        MultiEmitter<? super Message<? extends T>> previous = internal.getAndSet(fe);
        if (previous != null) {
            previous.complete();
        }
    };

    Multi<Message<? extends T>> tempPublisher;
    if (config.overflowBufferStrategy == null) {
        Multi<Message<? extends T>> multi = Multi.createFrom().emitter(deferred, BackPressureStrategy.BUFFER);
        tempPublisher = getPublisherUsingBufferStrategy(defaultBufferSize, multi);
    } else {
        tempPublisher = getPublisherForStrategy(config.overflowBufferStrategy, config.overflowBufferSize,
                defaultBufferSize, deferred);
    }

    if (config.broadcast) {
        publisher = (Multi<Message<? extends T>>) BroadcastHelper
                .broadcastPublisher(tempPublisher, config.numberOfSubscriberBeforeConnecting).buildRs();
    } else {
        publisher = tempPublisher;
    }
}
 
Example #19
Source File: MultiTakeTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeLastWithBackPressure() {
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(0);

    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi.createFrom().<Integer> emitter(emitter::set)
            .transform().byTakingLastItems(3)
            .subscribe(subscriber);

    subscriber.assertNotTerminated()
            .assertHasNotReceivedAnyItem();

    emitter.get().emit(1).emit(2);

    subscriber.request(2)
            .assertNotTerminated()
            .assertHasNotReceivedAnyItem();

    emitter.get().emit(3).emit(4);

    subscriber.request(5)
            .assertNotTerminated()
            .assertHasNotReceivedAnyItem();

    emitter.get().emit(5).emit(6).emit(7).emit(8).emit(9).emit(10).complete();

    subscriber.request(5)
            .assertCompletedSuccessfully()
            .assertReceived(8, 9, 10);
}
 
Example #20
Source File: MultiSkipTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipLastWithBackPressure() {
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(0);

    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi.createFrom().<Integer> emitter(emitter::set)
            .transform().bySkippingLastItems(3)
            .subscribe(subscriber);

    subscriber.assertNotTerminated()
            .assertHasNotReceivedAnyItem();

    emitter.get().emit(1).emit(2);

    subscriber.request(2)
            .assertNotTerminated()
            .assertHasNotReceivedAnyItem();

    emitter.get().emit(3).emit(4);

    subscriber.request(5)
            .assertNotTerminated()
            .assertReceived(1);

    emitter.get().emit(5).emit(6).emit(7).emit(8).emit(9).emit(10).complete();

    subscriber.request(5)
            .assertCompletedSuccessfully()
            .assertReceived(1, 2, 3, 4, 5, 6, 7);
}
 
Example #21
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@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);

    MultiAssertSubscriber<Integer> subscriber1 = multi.subscribe()
            .withSubscriber(MultiAssertSubscriber.create(10))
            .assertReceived(1, 1)
            .assertNotTerminated();

    MultiAssertSubscriber<Integer> subscriber2 = multi.subscribe()
            .withSubscriber(MultiAssertSubscriber.create(10))
            .assertReceived(2, 2)
            .assertNotTerminated();

    emitters.forEach(MultiEmitter::complete);
    subscriber1.assertCompletedSuccessfully();
    subscriber2.assertCompletedSuccessfully();

}
 
Example #22
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@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);
    });

    MultiAssertSubscriber<Integer> subscriber1 = multi.subscribe()
            .withSubscriber(MultiAssertSubscriber.create(10))
            .assertReceived(1, 1)
            .assertNotTerminated();

    MultiAssertSubscriber<Integer> subscriber2 = multi.subscribe()
            .withSubscriber(MultiAssertSubscriber.create(10))
            .assertReceived(2, 2)
            .assertNotTerminated();

    emitters.forEach(MultiEmitter::complete);
    subscriber1.assertCompletedSuccessfully();
    subscriber2.assertCompletedSuccessfully();

}
 
Example #23
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithDropBackPressure() {
    MultiAssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer> emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.DROP).subscribe()
            .withSubscriber(MultiAssertSubscriber.create(20))
            .request(Long.MAX_VALUE)
            .assertCompletedSuccessfully();
    // 20 because the 20 first are consumed, others are dropped
    assertThat(subscriber.items()).hasSize(20);

    subscriber = Multi.createFrom().<Integer> emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.DROP).subscribe()
            .withSubscriber(MultiAssertSubscriber.create())
            .request(20)
            .request(Long.MAX_VALUE)
            .assertCompletedSuccessfully();
    assertThat(subscriber.items()).isEmpty();

    Multi.createFrom().<Integer> emitter(MultiEmitter::complete, BackPressureStrategy.DROP)
            .subscribe().withSubscriber(MultiAssertSubscriber.create(20))
            .assertCompletedSuccessfully()
            .assertHasNotReceivedAnyItem();

    Multi.createFrom().<Integer> emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.fail(new IOException("boom"));
    }, BackPressureStrategy.DROP).subscribe()
            .withSubscriber(MultiAssertSubscriber.create())
            .request(20)
            .request(Long.MAX_VALUE)
            .assertHasFailedWith(IOException.class, "boom")
            .assertHasNotReceivedAnyItem();
}
 
Example #24
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithLatestBackPressure() {
    MultiAssertSubscriber<Integer> subscriber = Multi.createFrom().<Integer> emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.LATEST).subscribe()
            .withSubscriber(MultiAssertSubscriber.create(20))
            .request(Long.MAX_VALUE)
            .assertCompletedSuccessfully();
    // 21 because the 20 first are consumed, and then only the latest is kept.
    assertThat(subscriber.items()).hasSize(21);

    subscriber = Multi.createFrom().<Integer> emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.complete();
    }, BackPressureStrategy.LATEST).subscribe()
            .withSubscriber(MultiAssertSubscriber.create())
            .request(20)
            .request(Long.MAX_VALUE)
            .assertCompletedSuccessfully();
    assertThat(subscriber.items()).hasSize(1).containsExactly(999);

    Multi.createFrom().<Integer> emitter(MultiEmitter::complete, BackPressureStrategy.LATEST)
            .subscribe().withSubscriber(MultiAssertSubscriber.create(20))
            .assertCompletedSuccessfully()
            .assertHasNotReceivedAnyItem();

    subscriber = Multi.createFrom().<Integer> emitter(emitter -> {
        IntStream.range(0, 1000).forEach(emitter::emit);
        emitter.fail(new IOException("boom"));
    }, BackPressureStrategy.LATEST).subscribe()
            .withSubscriber(MultiAssertSubscriber.create())
            .request(20)
            .request(Long.MAX_VALUE)
            .assertHasFailedWith(IOException.class, "boom");
    assertThat(subscriber.items()).hasSize(1).containsExactly(999);

}
 
Example #25
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@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(MultiAssertSubscriber.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()
            .assertReceived(1, 2)
            .request(10)
            .assertReceived(1, 2, 3)
            .run(() -> {
                assertThat(reference.get().requested()).isEqualTo(10);
            })
            .assertCompletedSuccessfully();

    assertThat(terminated).hasValue(1);
}
 
Example #26
Source File: MultiCreateFromEmitterTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@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(MultiAssertSubscriber.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()
            .assertReceived(1, 2)
            .request(2)
            .assertReceived(1, 2, 3)
            .assertCompletedSuccessfully();

    assertThat(terminated).hasValue(1);
}
 
Example #27
Source File: EmitterImpl.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
static <T> MultiEmitter<? super Message<? extends T>> verify(
        AtomicReference<MultiEmitter<? super Message<? extends T>>> reference,
        String name) {
    MultiEmitter<? super Message<? extends T>> emitter = reference.get();
    if (emitter == null) {
        throw ex.illegalStateForNoSubscriber(name);
    }
    if (emitter.isCancelled()) {
        throw ex.illegalStateForCancelledSubscriber(name);
    }
    return emitter;
}
 
Example #28
Source File: EmitterImpl.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private synchronized void emit(Message<? extends T> message) {
    MultiEmitter<? super Message<? extends T>> emitter = verify(internal, name);
    if (synchronousFailure.get() != null) {
        throw ex.illegalStateForEmitter(synchronousFailure.get());
    }
    if (emitter.isCancelled()) {
        throw ex.illegalStateForDownstreamCancel();
    }
    emitter.emit(message);
    if (synchronousFailure.get() != null) {
        throw ex.illegalStateForEmitterWhileEmitting(synchronousFailure.get());
    }
}
 
Example #29
Source File: EmitterImpl.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
Multi<Message<? extends T>> getPublisherForStrategy(OnOverflow.Strategy overFlowStrategy, long bufferSize,
        long defaultBufferSize,
        Consumer<MultiEmitter<? super Message<? extends T>>> deferred) {
    switch (overFlowStrategy) {
        case BUFFER:
            if (bufferSize > 0) {
                return ThrowingEmitter.create(deferred, bufferSize);
            } else {
                return ThrowingEmitter.create(deferred, defaultBufferSize);
            }

        case UNBOUNDED_BUFFER:
            return Multi.createFrom().emitter(deferred, BackPressureStrategy.BUFFER);

        case THROW_EXCEPTION:
            return ThrowingEmitter.create(deferred, 0);

        case DROP:
            return Multi.createFrom().emitter(deferred, BackPressureStrategy.DROP);

        case FAIL:
            return Multi.createFrom().emitter(deferred, BackPressureStrategy.ERROR);

        case LATEST:
            return Multi.createFrom().emitter(deferred, BackPressureStrategy.LATEST);

        case NONE:
            return Multi.createFrom().emitter(deferred, BackPressureStrategy.IGNORE);

        default:
            throw ex.illegalArgumentForBackPressure(overFlowStrategy);
    }
}
 
Example #30
Source File: MultiOnOverflowTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropPreviousStrategyWithEmitter() {
    MultiAssertSubscriber<Integer> sub = MultiAssertSubscriber.create();
    AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
    Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set)
            .onOverflow().dropPreviousItems();
    multi.subscribe(sub);

    emitter.get().emit(1);
    sub.assertNotTerminated().assertHasNotReceivedAnyItem();

    emitter.get().emit(2);
    sub.assertNotTerminated().assertHasNotReceivedAnyItem();

    sub.request(1);
    sub.assertNotTerminated().assertReceived(2);

    emitter.get().emit(3).emit(4);

    sub.request(2);
    sub.assertNotTerminated().assertReceived(2, 4);

    emitter.get().emit(5);
    sub.assertNotTerminated().assertReceived(2, 4, 5);

    emitter.get().complete();
    sub.assertCompletedSuccessfully();
}