use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCreateFromEmitterTest method testThatWeCanHaveMultipleSubscribersWhenUsingBackPressure.
@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);
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();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class EmitterBasedMultiTest method testSerializedWithConcurrentEmissionsAndFailure.
@Test
public void testSerializedWithConcurrentEmissionsAndFailure() {
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);
}
reference.get().fail(new Exception("boom"));
};
ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(r1);
service.submit(r2);
subscriber.awaitFailure().assertFailedWith(Exception.class, "boom");
service.shutdown();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class EmitterBasedMultiTest method testLateRegistrationOfOnTerminationAfterCancellation.
@Test
public void testLateRegistrationOfOnTerminationAfterCancellation() {
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();
subscriber.cancel();
reference.get().onTermination(() -> called.set(true));
assertThat(called).isTrue();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class EmitterBasedMultiTest method testLateRegistrationOfOnTerminationAfterFailure.
@Test
public void testLateRegistrationOfOnTerminationAfterFailure() {
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().fail(new Exception("boom"));
subscriber.assertFailedWith(Exception.class, "boom");
reference.get().onTermination(() -> called.set(true));
assertThat(called).isTrue();
}
Aggregations