Search in sources :

Example 11 with Cancellable

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Awaitility.await(org.awaitility.Awaitility.await) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) InfrastructureResource(junit5.support.InfrastructureResource) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 12 with Cancellable

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();
}
Also used : Awaitility.await(org.awaitility.Awaitility.await) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) InfrastructureResource(junit5.support.InfrastructureResource) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Cancellable(io.smallrye.mutiny.subscription.Cancellable) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 13 with Cancellable

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) Test(org.junit.jupiter.api.Test)

Example 14 with Cancellable

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) Test(org.junit.jupiter.api.Test)

Example 15 with Cancellable

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();
}
Also used : Cancellable(io.smallrye.mutiny.subscription.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Cancellable (io.smallrye.mutiny.subscription.Cancellable)26 Test (org.junit.jupiter.api.Test)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Uni (io.smallrye.mutiny.Uni)5 IOException (java.io.IOException)5 Duration (java.time.Duration)4 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 GraphQLClientException (io.smallrye.graphql.client.GraphQLClientException)2 GraphQLError (io.smallrye.graphql.client.GraphQLError)2 InvalidResponseException (io.smallrye.graphql.client.InvalidResponseException)2 ResponseReader (io.smallrye.graphql.client.impl.ResponseReader)2 WebSocketSubprotocolHandler (io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler)2 IncrementingNumberOperationIDGenerator (io.smallrye.graphql.client.vertx.websocket.opid.IncrementingNumberOperationIDGenerator)2 OperationIDGenerator (io.smallrye.graphql.client.vertx.websocket.opid.OperationIDGenerator)2 MultiEmitter (io.smallrye.mutiny.subscription.MultiEmitter)2 WebSocket (io.vertx.core.http.WebSocket)2 StringReader (java.io.StringReader)2