use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class EmitterBasedMultiTest method testRegistrationOfOnTerminationAndCompletionRace.
@RepeatedTest(100)
public void testRegistrationOfOnTerminationAndCompletionRace() {
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();
List<Runnable> runnables = new ArrayList<>();
runnables.add(() -> reference.get().emit(1).complete());
runnables.add(() -> reference.get().onTermination(() -> called.set(true)));
Collections.shuffle(runnables);
runnables.forEach(r -> new Thread(r).start());
await().untilAsserted(() -> {
subscriber.assertItems(1).assertCompleted();
assertThat(called).isTrue();
});
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class EmitterBasedMultiTest method testLateReplacementOfOnTerminationAfterCompletion.
@Test
public void testLateReplacementOfOnTerminationAfterCompletion() {
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().complete();
subscriber.assertCompleted();
assertThat(called1).isTrue();
assertThat(called2).isFalse();
reference.get().onTermination(() -> called2.set(true));
assertThat(called2).isTrue();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class EmitterBasedMultiTest method testSerializedWithConcurrentEmissions.
@Test
public void testSerializedWithConcurrentEmissions() {
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);
}
};
ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(r1);
service.submit(r2);
await().until(() -> subscriber.getItems().size() == 1200);
service.shutdown();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiConvertToTest method testCreatingAMaybeWithSubscriberAlreadyCancelled.
@Test
public void testCreatingAMaybeWithSubscriberAlreadyCancelled() {
AtomicBoolean cancelled = new AtomicBoolean();
AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>();
Maybe<Integer> maybe = Multi.createFrom().<Integer>emitter(emitter::set).onCancellation().invoke(() -> cancelled.set(true)).convert().with(MultiRx3Converters.toMaybe());
assertThat(maybe).isNotNull();
TestObserver<Integer> observer = maybe.test();
observer.dispose();
emitter.get().emit(1).complete();
assertThat(observer.isDisposed()).isTrue();
observer.assertEmpty();
assertThat(cancelled).isTrue();
}
use of io.smallrye.mutiny.subscription.MultiEmitter in project smallrye-mutiny by smallrye.
the class MultiCreateFromEmitterTest method testThatWeCanHaveMultipleSubscribers.
@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);
});
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();
}
Aggregations