use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.
the class UniContextPropagationTest method testOnTermination.
@Test
public void testOnTermination() throws InterruptedException {
MyContext ctx = MyContext.get();
AtomicInteger count = new AtomicInteger();
AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
Uni<Integer> uni = Uni.createFrom().emitter((Consumer<UniEmitter<? super Integer>>) reference::set).onTermination().invoke((value, failure, cancellation) -> {
count.incrementAndGet();
assertThat(ctx).isSameAs(MyContext.get());
});
// Test completion
CountDownLatch latch1 = new CountDownLatch(1);
uni.subscribe().with(x -> {
latch1.countDown();
});
new Thread(() -> {
reference.get().complete(23);
}).start();
latch1.await(10, TimeUnit.MILLISECONDS);
assertThat(count).hasValue(1);
// Test failure
CountDownLatch latch2 = new CountDownLatch(1);
uni.subscribe().with(x -> {
}, fail -> {
latch2.countDown();
});
new Thread(() -> {
reference.get().fail(new Exception("boom"));
}).start();
latch2.await(10, TimeUnit.MILLISECONDS);
assertThat(count).hasValue(2);
// Test cancellation
Cancellable cancellable = uni.subscribe().with(x -> {
}, fail -> {
});
new Thread(cancellable::cancel).start();
await().until(() -> count.get() == 3);
}
use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.
the class UniSerializedSubscriberTest method testRaceBetweenItemAndCancellationWithUniserializedSubscriber.
@RepeatedTest(100)
public void testRaceBetweenItemAndCancellationWithUniserializedSubscriber() {
AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
AtomicBoolean cancelled = new AtomicBoolean();
Uni<Integer> uni = Uni.createFrom().<Integer>emitter(reference::set).onCancellation().invoke(() -> cancelled.set(true));
UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
uni.subscribe().withSerializedSubscriber(subscriber);
subscriber.assertSubscribed();
CountDownLatch start = new CountDownLatch(2);
CountDownLatch done = new CountDownLatch(2);
Runnable runnable1 = () -> {
try {
start.countDown();
await(start);
reference.get().complete(1);
done.countDown();
} catch (Throwable e) {
e.printStackTrace();
}
};
Runnable runnable2 = () -> {
try {
start.countDown();
await(start);
subscriber.cancel();
done.countDown();
} catch (Throwable e) {
e.printStackTrace();
}
};
List<Runnable> runnables = Arrays.asList(runnable1, runnable2);
Collections.shuffle(runnables);
runnables.forEach(r -> new Thread(r).start());
await(done);
if (subscriber.getItem() != null) {
assertThat(cancelled).isFalse();
subscriber.assertCompleted().assertItem(1);
} else {
subscriber.assertNotTerminated();
}
subscriber.assertSignalsReceivedInOrder();
}
use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.
the class UniSerializedSubscriberTest method testNormal.
@Test
public void testNormal() {
AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
AbstractUni<? super Integer> uni = (AbstractUni<? super Integer>) Uni.createFrom().<Integer>emitter(reference::set);
UniAssertSubscriber<Object> subscriber = UniAssertSubscriber.create();
UniSerializedSubscriber.subscribe(uni, subscriber);
subscriber.assertSubscribed();
reference.get().complete(1);
reference.get().complete(2);
reference.get().fail(new IOException("boom"));
subscriber.assertCompleted().assertItem(1).assertSignalsReceivedInOrder();
}
use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.
the class UniSerializedSubscriberTest method testNormalWithNullItem.
@Test
public void testNormalWithNullItem() {
AtomicReference<UniEmitter<? super String>> reference = new AtomicReference<>();
AbstractUni<? super String> uni = (AbstractUni<? super String>) Uni.createFrom().<String>emitter(reference::set);
UniAssertSubscriber<Object> subscriber = UniAssertSubscriber.create();
UniSerializedSubscriber.subscribe(uni, subscriber);
subscriber.assertSubscribed();
reference.get().complete(null);
reference.get().complete("hello");
reference.get().fail(new IOException("boom"));
subscriber.assertCompleted().assertItem(null).assertSignalsReceivedInOrder();
}
use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.
the class UniSerializedSubscriberTest method testRaceBetweenItemAndFailureWithoutUniserializedSubscriber.
@RepeatedTest(100)
public void testRaceBetweenItemAndFailureWithoutUniserializedSubscriber() {
AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
Uni<Integer> uni = Uni.createFrom().<Integer>emitter(reference::set);
UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
uni.subscribe().withSubscriber(subscriber);
subscriber.assertSubscribed();
CountDownLatch start = new CountDownLatch(2);
Runnable runnable1 = () -> {
start.countDown();
await(start);
reference.get().complete(1);
};
Runnable runnable2 = () -> {
start.countDown();
await(start);
reference.get().fail(new IOException("boom"));
};
List<Runnable> runnables = Arrays.asList(runnable1, runnable2);
Collections.shuffle(runnables);
runnables.forEach(r -> new Thread(r).start());
Awaitility.await().untilAsserted(subscriber::assertTerminated);
if (subscriber.getFailure() != null) {
subscriber.assertFailed().assertFailedWith(IOException.class, "boom");
} else {
subscriber.assertCompleted().assertItem(1);
}
subscriber.assertSignalsReceivedInOrder();
}
Aggregations