Search in sources :

Example 21 with Cancellable

use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.

the class CreatingMultiTest method subscription.

@Test
void subscription(SystemOut out) {
    Multi<Integer> multi = Multi.createFrom().item(1);
    // tag::subscription[]
    Cancellable cancellable = multi.subscribe().with(item -> System.out.println(item), failure -> System.out.println("Failed with " + failure), () -> System.out.println("Completed"));
    // end::subscription[]
    assertThat(cancellable).isNotNull();
    assertThat(out.get()).contains("1", "Completed").doesNotContain("Failed");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) Test(org.junit.jupiter.api.Test)

Example 22 with Cancellable

use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.

the class MergeConcatTest method testMergeTicks.

@Test
public void testMergeTicks(SystemOut out) {
    // tag::merge-ticks[]
    Multi<String> first = Multi.createFrom().ticks().every(Duration.ofMillis(10)).onItem().transform(l -> "Stream 1 - " + l);
    Multi<String> second = Multi.createFrom().ticks().every(Duration.ofMillis(15)).onItem().transform(l -> "Stream 2 - " + l);
    Multi<String> third = Multi.createFrom().ticks().every(Duration.ofMillis(5)).onItem().transform(l -> "Stream 3 - " + l);
    Cancellable cancellable = Multi.createBy().merging().streams(first, second, third).subscribe().with(s -> System.out.println("Got item: " + s));
    // end::merge-ticks[]
    await().until(() -> out.get().contains("Got item: Stream 3 - 10"));
    cancellable.cancel();
}
Also used : Cancellable(io.smallrye.mutiny.subscription.Cancellable) Test(org.junit.jupiter.api.Test)

Example 23 with Cancellable

use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.

the class PollableSourceTest method test.

@Test
public void test(SystemOut out) {
    // NOSONAR
    // tag::code[]
    PollableDataSource source = new PollableDataSource();
    // First creates a uni that emit the polled item.
    // Because `poll` blocks, let's use a specific executor
    Uni<String> pollItemFromSource = Uni.createFrom().item(source::poll).runSubscriptionOn(executor);
    // To get the stream of items, just repeat the uni indefinitely
    Multi<String> stream = pollItemFromSource.repeat().indefinitely();
    Cancellable cancellable = stream.subscribe().with(item -> System.out.println("Polled item: " + item));
    // end::code[]
    await().until(() -> source.counter.get() >= 4);
    // tag::code[]
    // ... later ..
    // when you don't want the items anymore, cancel the subscription and close the source if needed.
    cancellable.cancel();
    source.close();
// end::code[]
}
Also used : Cancellable(io.smallrye.mutiny.subscription.Cancellable) Test(org.junit.jupiter.api.Test)

Example 24 with Cancellable

use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.

the class MultiSubscribeTest method testWith4CallbacksAndCancellation.

@Test
public void testWith4CallbacksAndCancellation() {
    List<Integer> items = new CopyOnWriteArrayList<>();
    AtomicBoolean completion = new AtomicBoolean();
    AtomicReference<Throwable> failure = new AtomicReference<>();
    Cancellable cancellable = Multi.createFrom().range(1, 100).subscribe().with(s -> s.request(3), items::add, failure::set, () -> completion.set(true));
    cancellable.cancel();
    // Called twice
    cancellable.cancel();
    assertThat(items).contains(1, 2, 3);
    assertThat(failure.get()).isNull();
    assertThat(completion).isFalse();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 25 with Cancellable

use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-reactive-messaging by smallrye.

the class ReactiveKafkaBatchConsumerTest method subscribeBatch.

private void subscribeBatch(Multi<IncomingKafkaRecordBatch<Integer, String>> stream, CountDownLatch... latches) throws Exception {
    Cancellable cancellable = stream.onItem().invoke(record -> {
        onReceive(record);
        for (CountDownLatch latch : latches) {
            for (KafkaRecord<Integer, String> ignored : record.getRecords()) {
                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