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();
}
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();
}
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();
}
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());
});
}
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");
}
Aggregations