use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class UniSubscriberTest method testSingleCallbackVariant.
@Test
public void testSingleCallbackVariant() {
AtomicInteger result = new AtomicInteger();
Uni.createFrom().item(1).subscribe().with(result::set);
assertThat(result).hasValue(1);
AtomicReference<String> value = new AtomicReference<>("sentinel");
Uni.createFrom().<String>nullItem().subscribe().with(value::set);
assertThat(value).hasValue(null);
result.set(-1);
Uni.createFrom().<Integer>failure(new IOException("boom")).subscribe().with(result::set);
assertThat(result).hasValue(-1);
// Assert cancellation before the emission
AtomicBoolean called = new AtomicBoolean();
Cancellable cancellable = Uni.createFrom().nothing().subscribe().with(x -> called.set(true));
assertThat(called).isFalse();
cancellable.cancel();
assertThat(called).isFalse();
// Assert cancellation after the emission
cancellable = Uni.createFrom().item(1).subscribe().with(x -> called.set(true));
assertThat(called).isTrue();
cancellable.cancel();
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class UniSubscriberTest method testTwoCallbacksVariant.
@Test
public void testTwoCallbacksVariant() {
AtomicInteger result = new AtomicInteger();
AtomicReference<Throwable> failure = new AtomicReference<>();
Uni.createFrom().item(1).subscribe().with(result::set, failure::set);
assertThat(result).hasValue(1);
assertThat(failure).hasValue(null);
AtomicReference<String> value = new AtomicReference<>("sentinel");
Uni.createFrom().<String>nullItem().subscribe().with(value::set, failure::set);
assertThat(value).hasValue(null);
assertThat(failure).hasValue(null);
result.set(-1);
Uni.createFrom().<Integer>failure(new IOException("boom")).subscribe().with(result::set, failure::set);
assertThat(result).hasValue(-1);
assertThat(failure.get()).isInstanceOf(IOException.class).hasMessage("boom");
// Assert cancellation before the emission
AtomicBoolean called = new AtomicBoolean();
Cancellable cancellable = Uni.createFrom().nothing().subscribe().with(x -> called.set(true), f -> called.set(true));
assertThat(called).isFalse();
cancellable.cancel();
assertThat(called).isFalse();
// Assert cancellation after the emission
cancellable = Uni.createFrom().item(1).subscribe().with(x -> called.set(true), f -> called.set(true));
assertThat(called).isTrue();
cancellable.cancel();
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class MultiOnEventTest method testCancellationBeforeActionCompletes.
@Test
public void testCancellationBeforeActionCompletes() {
AtomicBoolean terminated = new AtomicBoolean();
Uni<Object> uni = Uni.createFrom().emitter(e -> e.onTermination(() -> terminated.set(true)));
AtomicInteger result = new AtomicInteger();
Cancellable cancellable = numbers.onItem().call(i -> uni).subscribe().with(result::set);
cancellable.cancel();
assertThat(result).hasValue(0);
// noinspection ConstantConditions
assertThat(terminated).isTrue();
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class UniOnItemInvokeTest method testCancellationBeforeActionCompletes.
@Test
public void testCancellationBeforeActionCompletes() {
AtomicBoolean terminated = new AtomicBoolean();
Uni<Object> uni = Uni.createFrom().emitter(e -> e.onTermination(() -> terminated.set(true)));
AtomicInteger result = new AtomicInteger();
Cancellable cancellable = one.onItem().call(i -> uni).subscribe().with(result::set);
cancellable.cancel();
assertThat(result).hasValue(0);
// noinspection ConstantConditions
assertThat(terminated).isTrue();
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-reactive-messaging by smallrye.
the class ClientTestBase method subscribe.
void subscribe(Multi<IncomingKafkaRecord<Integer, String>> stream, CountDownLatch... latches) throws Exception {
Cancellable cancellable = stream.onItem().invoke(record -> {
onReceive(record);
for (CountDownLatch latch : latches) {
latch.countDown();
}
}).subscribe().with(ignored -> {
// Ignored.
});
subscriptions.add(cancellable);
waitForPartitionAssignment();
}
Aggregations