Search in sources :

Example 21 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.

the class MultiFromResourceTest method testOnCompletionWithSingleFinalizer.

@Test
public void testOnCompletionWithSingleFinalizer() {
    AtomicBoolean subscribed = new AtomicBoolean();
    Multi<Integer> multi = Multi.createFrom().resource(() -> 1, x -> Multi.createFrom().range(x, 11)).withFinalizer(r -> {
        return Uni.createFrom().item("ok").onSubscription().invoke(s -> subscribed.set(true)).onItem().ignore().andContinueWithNull();
    });
    multi.subscribe().withSubscriber(AssertSubscriber.create(20)).assertCompleted().assertItems(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertThat(subscribed).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 22 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.

the class MultiFromResourceTest method testOnFailureWithSingleFinalizer.

@Test
public void testOnFailureWithSingleFinalizer() {
    AtomicBoolean subscribed = new AtomicBoolean();
    Multi<Integer> multi = Multi.createFrom().resource(() -> 1, x -> Multi.createFrom().range(x, 11).onCompletion().failWith(new IOException("boom"))).withFinalizer(r -> {
        return Uni.createFrom().item("ok").onSubscription().invoke(s -> subscribed.set(true)).onItem().ignore().andContinueWithNull();
    });
    multi.subscribe().withSubscriber(AssertSubscriber.create(20)).assertFailedWith(IOException.class, "boom").assertItems(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertThat(subscribed).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 23 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.

the class MultiOnFailureRetryWhenTest method testWhatTheWhenStreamFailsTheUpstreamIsCancelled.

@Test
public void testWhatTheWhenStreamFailsTheUpstreamIsCancelled() {
    AtomicBoolean subscribed = new AtomicBoolean();
    AtomicBoolean cancelled = new AtomicBoolean();
    Multi<Integer> multi = failingAfter1.onSubscription().invoke(sub -> subscribed.set(true)).onCancellation().invoke(() -> cancelled.set(true));
    AtomicInteger count = new AtomicInteger();
    Multi<Integer> retry = multi.onFailure().retry().when(other -> other.flatMap(l -> count.getAndIncrement() == 0 ? Multi.createFrom().item(l) : Multi.createFrom().failure(new IllegalStateException("boom"))));
    AssertSubscriber<Integer> subscriber = retry.subscribe().withSubscriber(AssertSubscriber.create(10));
    subscriber.assertSubscribed().assertItems(1, 1).assertFailedWith(IllegalStateException.class, "boom");
    assertThat(subscribed.get()).isTrue();
    assertThat(cancelled.get()).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) BeforeEach(org.junit.jupiter.api.BeforeEach) Awaitility.await(org.awaitility.Awaitility.await) Tuple2(io.smallrye.mutiny.tuples.Tuple2) 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) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) MultiRetryWhenOp(io.smallrye.mutiny.operators.multi.MultiRetryWhenOp) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 24 with Multi

use of io.smallrye.mutiny.Multi in project stackgres by ongres.

the class ClusterRestartImpl method restartCluster.

@Override
public Multi<RestartEvent> restartCluster(ClusterRestartState clusterRestartState) {
    return Multi.createFrom().emitter(em -> {
        Uni<?> restartChain = waitForClusterToBeHealthy(clusterRestartState);
        Pod primaryInstance = clusterRestartState.getPrimaryInstance();
        final String primaryInstanceName = primaryInstance.getMetadata().getName();
        final String clusterName = clusterRestartState.getClusterName();
        if (clusterRestartState.getRestartedInstances().isEmpty() && clusterRestartState.hasToBeRestarted(primaryInstance)) {
            restartChain = restartChain.invoke(() -> {
                LOGGER.info("Restarting primary node postgres of cluster {}", clusterName);
                em.emit(ImmutableRestartEvent.builder().pod(primaryInstance).eventType(RestartEventType.RESTARTING_POSTGRES).build());
            }).chain(() -> postgresRestart.restartPostgres(primaryInstanceName, clusterName, clusterRestartState.getNamespace())).onItem().invoke(() -> {
                LOGGER.info("Postgres of instance {} restarted", primaryInstanceName);
                em.emit(ImmutableRestartEvent.builder().pod(primaryInstance).eventType(RestartEventType.POSTGRES_RESTARTED).build());
            });
            restartChain = waitForClusterToBeHealthy(clusterRestartState, restartChain);
        }
        if (isReducedImpact(clusterRestartState) && hasInstancesNotBeenIncreased(clusterRestartState)) {
            restartChain = restartChain.onItem().invoke(() -> {
                LOGGER.info("Increasing instances of cluster {}", clusterName);
                em.emit(ImmutableRestartEvent.builder().eventType(RestartEventType.INCREASING_INSTANCES).build());
            }).chain(() -> clusterInstanceManager.increaseClusterInstances(clusterName, clusterRestartState.getNamespace())).onItem().invoke((createdPod) -> {
                LOGGER.info("Instances of cluster {} increased", clusterName);
                em.emit(ImmutableRestartEvent.builder().pod(createdPod).eventType(RestartEventType.INSTANCES_INCREASED).build());
            });
            restartChain = waitForClusterToBeHealthy(clusterRestartState, restartChain);
        }
        List<Pod> replicas = clusterRestartState.getInitialInstances().stream().filter(pod -> !primaryInstance.equals(pod)).filter(clusterRestartState::hasToBeRestarted).collect(Collectors.toUnmodifiableList());
        for (Pod replica : replicas) {
            restartChain = restartChain.onItem().invoke(() -> logPodRestartReason(replica, clusterRestartState)).invoke(() -> {
                LOGGER.info("Restarting pod {}", replica.getMetadata().getName());
                em.emit(ImmutableRestartEvent.builder().pod(replica).eventType(RestartEventType.RESTARTING_POD).build());
            }).chain(() -> podRestart.restartPod(replica)).onItem().invoke(() -> {
                LOGGER.info("Pod {} restarted", replica.getMetadata().getName());
                em.emit(ImmutableRestartEvent.builder().pod(replica).eventType(RestartEventType.POD_RESTARTED).build());
            });
            restartChain = waitForClusterToBeHealthy(clusterRestartState, restartChain);
        }
        if (!clusterRestartState.isSwitchoverFinalized() && clusterRestartState.hasToBeRestarted(primaryInstance)) {
            restartChain = restartChain.onItem().invoke(() -> em.emit(ImmutableRestartEvent.builder().pod(primaryInstance).eventType(RestartEventType.SWITCHOVER_INITIATED).build())).onItem().invoke(() -> LOGGER.info("Performing Switchover over cluster {}", clusterName)).chain(() -> switchoverHandler.performSwitchover(clusterRestartState.getPrimaryInstance().getMetadata().getName(), clusterName, clusterRestartState.getNamespace())).onItem().invoke(() -> em.emit(ImmutableRestartEvent.builder().pod(primaryInstance).eventType(RestartEventType.SWITCHOVER_FINALIZED).build()));
            restartChain = waitForClusterToBeHealthy(clusterRestartState, restartChain);
        }
        if (clusterRestartState.hasToBeRestarted(primaryInstance)) {
            restartChain = restartChain.onItem().invoke(() -> logPodRestartReason(primaryInstance, clusterRestartState)).invoke(() -> {
                LOGGER.info("Restarting pod {}", primaryInstanceName);
                em.emit(ImmutableRestartEvent.builder().pod(primaryInstance).eventType(RestartEventType.RESTARTING_POD).build());
            }).chain(() -> podRestart.restartPod(primaryInstance)).onItem().invoke(() -> {
                LOGGER.info("Pod {} restarted", primaryInstanceName);
                em.emit(ImmutableRestartEvent.builder().pod(primaryInstance).eventType(RestartEventType.POD_RESTARTED).build());
            });
            restartChain = waitForClusterToBeHealthy(clusterRestartState, restartChain);
        }
        if (isReducedImpact(clusterRestartState) && hasInstancesNotBeenDecreased(clusterRestartState)) {
            restartChain = restartChain.onItem().invoke(() -> {
                LOGGER.info("Decreasing instances of cluster {}", clusterName);
                em.emit(ImmutableRestartEvent.builder().eventType(RestartEventType.DECREASING_INSTANCES).build());
            }).chain(() -> clusterInstanceManager.decreaseClusterInstances(clusterName, clusterRestartState.getNamespace())).onItem().invoke(() -> {
                LOGGER.info("Instances of cluster {} decreased", clusterName);
                em.emit(ImmutableRestartEvent.builder().eventType(RestartEventType.INSTANCES_DECREASED).build());
            });
        }
        restartChain.subscribe().with((x) -> em.complete());
    });
}
Also used : Inject(javax.inject.Inject) List(java.util.List) RestartReasons(io.stackgres.common.ClusterPendingRestartUtil.RestartReasons) Logger(org.slf4j.Logger) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) Pod(io.fabric8.kubernetes.api.model.Pod) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collectors(java.util.stream.Collectors) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) RestartReason(io.stackgres.common.ClusterPendingRestartUtil.RestartReason) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 25 with Multi

use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.

the class MultiToHotStreamTest method testResubscriptionAfterFailure.

@Test
public void testResubscriptionAfterFailure() {
    BroadcastProcessor<String> processor = BroadcastProcessor.create();
    Multi<String> multi = processor.map(s -> s).toHotStream();
    AssertSubscriber<String> subscriber1 = multi.subscribe().withSubscriber(AssertSubscriber.create(10));
    processor.onNext("one");
    subscriber1.assertItems("one");
    subscriber1.cancel();
    processor.onNext("two");
    processor.onError(new IOException("boom"));
    processor.subscribe(subscriber1);
    subscriber1.assertItems("one").assertFailedWith(IOException.class, "boom");
}
Also used : AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) BroadcastProcessor(io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) Test(org.junit.jupiter.api.Test) List(java.util.List) BackPressureFailure(io.smallrye.mutiny.subscription.BackPressureFailure) UnicastProcessor(io.smallrye.mutiny.operators.multi.processors.UnicastProcessor) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

Multi (io.smallrye.mutiny.Multi)60 Test (org.junit.jupiter.api.Test)43 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)42 Uni (io.smallrye.mutiny.Uni)41 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)41 List (java.util.List)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)34 IOException (java.io.IOException)33 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)31 Duration (java.time.Duration)29 Function (java.util.function.Function)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)25 Consumer (java.util.function.Consumer)25 InfrastructureResource (junit5.support.InfrastructureResource)22 ResourceAccessMode (org.junit.jupiter.api.parallel.ResourceAccessMode)22 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)22 Supplier (java.util.function.Supplier)20 CompositeException (io.smallrye.mutiny.CompositeException)19 ArrayList (java.util.ArrayList)18