use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-reactive-messaging by smallrye.
the class ThrowingEmitter method create.
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).onRequest().invoke(throwingEmitter::request);
});
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiSubscribeTest method testWith2CallbacksAndCompletion.
@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();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiGroupTest method testGroupByWithUpstreamFailure.
@RepeatedTest(10)
public void testGroupByWithUpstreamFailure() {
AtomicReference<MultiEmitter<? super Long>> emitter = new AtomicReference<>();
Multi<Long> multi = Multi.createBy().merging().streams(Multi.createFrom().ticks().every(Duration.ofMillis(2)), Multi.createFrom().emitter((Consumer<MultiEmitter<? super Long>>) emitter::set));
AssertSubscriber<GroupedMulti<Long, Long>> subscriber = multi.group().by(l -> l % 2).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
subscriber.assertSubscribed();
await().until(() -> subscriber.getItems().size() == 2);
AssertSubscriber<Long> s1 = subscriber.getItems().get(0).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
s1.assertSubscribed();
AssertSubscriber<Long> s2 = subscriber.getItems().get(1).subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
s2.assertSubscribed();
await().until(() -> s1.getItems().size() >= 3);
await().until(() -> s2.getItems().size() >= 3);
emitter.get().fail(new TestException("boom"));
s1.awaitFailure().assertFailedWith(TestException.class, "boom");
s2.awaitFailure().assertFailedWith(TestException.class, "boom");
subscriber.assertFailedWith(TestException.class, "boom");
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCacheTest method testCachingWithDeferredResult.
@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();
AssertSubscriber<Integer> s1 = multi.subscribe().withSubscriber(AssertSubscriber.create(2)).assertItems(1, 2).assertNotTerminated();
AssertSubscriber<Integer> s2 = multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)).assertItems(1, 2).assertNotTerminated();
reference.get().emit(count.incrementAndGet()).complete();
s1.assertItems(1, 2).request(1).assertItems(1, 2, 3).assertCompleted();
s2.assertItems(1, 2, 3).assertCompleted();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCombineTest method testCombiningWithFailuresAtSameTimeAndCollectFailure.
@Test
public void testCombiningWithFailuresAtSameTimeAndCollectFailure() {
AtomicReference<MultiEmitter<? super Integer>> emitter1 = new AtomicReference<>();
AtomicReference<MultiEmitter<? super Integer>> emitter2 = new AtomicReference<>();
Multi<Integer> multi = Multi.createFrom().items(1, 2, 3);
Multi<Integer> multi2 = Multi.createFrom().emitter(e -> {
emitter1.set(e);
e.emit(1);
});
Multi<Integer> multi3 = Multi.createFrom().emitter(e -> {
emitter2.set(e);
e.emit(1);
});
Multi.createBy().combining().streams(multi, multi2, multi, multi3).collectFailures().asTuple().subscribe().withSubscriber(AssertSubscriber.create(1)).assertItems(Tuple4.of(1, 1, 1, 1)).request(2).run(() -> {
emitter1.get().emit(2).fail(new IOException("boomA"));
emitter2.get().emit(2).fail(new IOException("boomB"));
}).assertItems(Tuple4.of(1, 1, 1, 1), Tuple4.of(2, 2, 2, 2)).assertFailedWith(IOException.class, "boomA");
}
Aggregations