Search in sources :

Example 6 with Uni

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

the class MultiFromResourceTest method cancellationShouldBePossible.

@Test
public void cancellationShouldBePossible() {
    AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
    Supplier<Integer> supplier = () -> 1;
    AtomicInteger onFailure = new AtomicInteger();
    AtomicInteger onComplete = new AtomicInteger();
    AtomicInteger onCancellation = new AtomicInteger();
    BiFunction<Integer, Throwable, Uni<Void>> onFailureCallback = (s, f) -> {
        onFailure.set(s);
        return Uni.createFrom().voidItem();
    };
    Function<Integer, Uni<Void>> onCompletionCallback = s -> {
        onComplete.set(s);
        return Uni.createFrom().voidItem();
    };
    Function<Integer, Uni<Void>> onCancellationCallback = s -> {
        onCancellation.set(s);
        return Uni.createFrom().voidItem();
    };
    Multi.createFrom().<Integer, Integer>resource(supplier, r -> Multi.createFrom().nothing()).withFinalizer(onCompletionCallback, onFailureCallback, onCancellationCallback).subscribe(subscriber);
    subscriber.cancel();
    assertThat(onFailure).hasValue(0);
    assertThat(onCancellation).hasValue(1);
    assertThat(onComplete).hasValue(0);
}
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) Uni(io.smallrye.mutiny.Uni) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 7 with Uni

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

the class UniOnItemTransformToUniTest method testWithAnUniResolvedAsynchronouslyWithAFailure.

@Test
public void testWithAnUniResolvedAsynchronouslyWithAFailure() {
    UniAssertSubscriber<Integer> test = UniAssertSubscriber.create();
    Uni<Integer> uni = Uni.createFrom().item(1).onItem().transformToUni(v -> Uni.createFrom().emitter(emitter -> new Thread(() -> emitter.fail(new IOException("boom"))).start()));
    uni.subscribe().withSubscriber(test);
    test.awaitFailure().assertFailedWith(IOException.class, "boom");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Test(org.junit.jupiter.api.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UniAssertSubscriber(io.smallrye.mutiny.helpers.test.UniAssertSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Uni(io.smallrye.mutiny.Uni) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 8 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-health by smallrye.

the class SmallRyeHealthReporter method getHealthAsync.

private Uni<SmallRyeHealth> getHealthAsync(Collection<Uni<HealthCheckResponse>> checks) {
    List<Uni<HealthCheckResponse>> healthCheckUnis = new ArrayList<>();
    if (checks != null) {
        healthCheckUnis.addAll(checks);
    }
    if (!additionalChecks.isEmpty()) {
        healthCheckUnis.addAll(additionalChecks.values());
    }
    if (healthCheckUnis.isEmpty()) {
        return Uni.createFrom().item(createEmptySmallRyeHealth(emptyChecksOutcome));
    }
    return Uni.combine().all().unis(healthCheckUnis).combinedWith(responses -> {
        JsonArrayBuilder results = jsonProvider.createArrayBuilder();
        HealthCheckResponse.Status status = HealthCheckResponse.Status.UP;
        for (Object o : responses) {
            HealthCheckResponse response = (HealthCheckResponse) o;
            status = handleResponse(response, results, status);
        }
        return createSmallRyeHealth(results, status, emptyChecksOutcome);
    });
}
Also used : Uni(io.smallrye.mutiny.Uni) HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 9 with Uni

use of io.smallrye.mutiny.Uni in project smallrye-health by smallrye.

the class SmallRyeHealthReporter method computeHealth.

private Uni<SmallRyeHealth> computeHealth(HealthType[] types) {
    List<Uni<HealthCheckResponse>> checks = new ArrayList<>();
    for (HealthType type : types) {
        switch(type) {
            case LIVENESS:
                checks.addAll(livenessUnis);
                checks.addAll(livenessHealthRegistry.getChecks(healthChecksConfigs));
                break;
            case READINESS:
                checks.addAll(readinessUnis);
                checks.addAll(readinessHealthRegistry.getChecks(healthChecksConfigs));
                break;
            case WELLNESS:
                checks.addAll(wellnessUnis);
                checks.addAll(wellnessHealthRegistry.getChecks(healthChecksConfigs));
                break;
            case STARTUP:
                checks.addAll(startupUnis);
                checks.addAll(startupHealthRegistry.getChecks(healthChecksConfigs));
                break;
        }
    }
    return getHealthAsync(checks);
}
Also used : Uni(io.smallrye.mutiny.Uni) ArrayList(java.util.ArrayList) HealthType(io.smallrye.health.api.HealthType)

Example 10 with Uni

use of io.smallrye.mutiny.Uni in project hibernate-reactive by hibernate.

the class MultipleContextTest method testOnPersistWithMutiny.

@Test
public void testOnPersistWithMutiny(TestContext testContext) {
    final Async async = testContext.async();
    Uni<Mutiny.Session> sessionUni = getMutinySessionFactory().openSession();
    currentSession = sessionUni;
    Context testVertxContext = Vertx.currentContext();
    // Create a different new context
    Context newContext = Vertx.vertx().getOrCreateContext();
    Assertions.assertThat(testVertxContext).isNotEqualTo(newContext);
    test(testContext, sessionUni.invoke(session -> newContext.runOnContext(event -> test(async, testContext, session.persist(new Competition("Cheese Rolling")).onItemOrFailure().transformToUni((unused, e) -> Uni.createFrom().completionStage(assertExceptionThrown(e)))))));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Context(io.vertx.core.Context) Mutiny(org.hibernate.reactive.mutiny.Mutiny) TestContext(io.vertx.ext.unit.TestContext) DatabaseSelectionRule.runOnlyFor(org.hibernate.reactive.testing.DatabaseSelectionRule.runOnlyFor) Entity(javax.persistence.Entity) Async(io.vertx.ext.unit.Async) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) DatabaseSelectionRule(org.hibernate.reactive.testing.DatabaseSelectionRule) Context(io.vertx.core.Context) Uni(io.smallrye.mutiny.Uni) Stage(org.hibernate.reactive.stage.Stage) CompletionStage(java.util.concurrent.CompletionStage) Rule(org.junit.Rule) Ignore(org.junit.Ignore) POSTGRESQL(org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL) Locale(java.util.Locale) After(org.junit.After) Assertions(org.assertj.core.api.Assertions) Configuration(org.hibernate.cfg.Configuration) Id(javax.persistence.Id) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

Uni (io.smallrye.mutiny.Uni)44 Multi (io.smallrye.mutiny.Multi)21 Test (org.junit.jupiter.api.Test)18 List (java.util.List)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Duration (java.time.Duration)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 IOException (java.io.IOException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)13 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)12 Function (java.util.function.Function)12 Supplier (java.util.function.Supplier)12 Map (java.util.Map)11 Consumer (java.util.function.Consumer)10 CompositeException (io.smallrye.mutiny.CompositeException)9 ArrayList (java.util.ArrayList)9 ApplicationScoped (javax.enterprise.context.ApplicationScoped)8 CompletableFuture (java.util.concurrent.CompletableFuture)7