Search in sources :

Example 1 with CleanAutoCloseable

use of io.smallrye.context.CleanAutoCloseable in project smallrye-mutiny by smallrye.

the class UniContextPropagationTest method testContextOverride.

@Test
public void testContextOverride() {
    MyContext ctx = MyContext.get();
    assertThat(ctx).isNotNull();
    SmallRyeThreadContext emptyContext = SmallRyeThreadContext.builder().cleared(ThreadContext.ALL_REMAINING).propagated(ThreadContext.NONE).build();
    Uni<Integer> uni;
    // remove context propagation in this scope
    try (CleanAutoCloseable ac = SmallRyeThreadContext.withThreadContext(emptyContext)) {
        uni = Uni.createFrom().item(() -> {
            assertThat(MyContext.get()).isNull();
            return 2;
        }).runSubscriptionOn(executor).map(r -> {
            assertThat(MyContext.get()).isNull();
            return r;
        });
    }
    Uni<Integer> latch = Uni.createFrom().emitter(emitter -> new Thread(() -> {
        try {
            int result = uni.await().indefinitely();
            emitter.complete(result);
        } catch (Throwable t) {
            emitter.fail(t);
        }
    }).start());
    int result = latch.await().indefinitely();
    assertThat(result).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) ThreadContext(org.eclipse.microprofile.context.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.junit.jupiter.api(org.junit.jupiter.api) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext) Duration(java.time.Duration) Cancellable(io.smallrye.mutiny.subscription.Cancellable) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext)

Example 2 with CleanAutoCloseable

use of io.smallrye.context.CleanAutoCloseable in project smallrye-mutiny by smallrye.

the class MultiContextPropagationTest method testContextOverride.

@Test
public void testContextOverride() {
    MyContext ctx = MyContext.get();
    assertThat(ctx).isNotNull();
    SmallRyeThreadContext emptyContext = SmallRyeThreadContext.builder().cleared(ThreadContext.ALL_REMAINING).propagated(ThreadContext.NONE).build();
    Multi<Integer> multi;
    // remove context propagation in this scope
    try (CleanAutoCloseable ac = SmallRyeThreadContext.withThreadContext(emptyContext)) {
        multi = Multi.createFrom().item(() -> {
            assertThat(MyContext.get()).isNull();
            return 2;
        }).runSubscriptionOn(executor).map(r -> {
            assertThat(MyContext.get()).isNull();
            return r;
        });
    }
    Uni<Integer> latch = Multi.createFrom().<Integer>emitter(emitter -> new Thread(() -> {
        try {
            int result = multi.toUni().await().indefinitely();
            emitter.emit(result);
            emitter.complete();
        } catch (Throwable t) {
            emitter.fail(t);
        }
    }).start()).toUni();
    int result = latch.await().indefinitely();
    assertThat(result).isEqualTo(2);
}
Also used : AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ThreadContext(org.eclipse.microprofile.context.ThreadContext) Multi(io.smallrye.mutiny.Multi) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) Uni(io.smallrye.mutiny.Uni) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) org.junit.jupiter.api(org.junit.jupiter.api) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext) Duration(java.time.Duration) CleanAutoCloseable(io.smallrye.context.CleanAutoCloseable) SmallRyeThreadContext(io.smallrye.context.SmallRyeThreadContext)

Aggregations

CleanAutoCloseable (io.smallrye.context.CleanAutoCloseable)2 SmallRyeThreadContext (io.smallrye.context.SmallRyeThreadContext)2 Uni (io.smallrye.mutiny.Uni)2 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)2 Duration (java.time.Duration)2 java.util.concurrent (java.util.concurrent)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ThreadContext (org.eclipse.microprofile.context.ThreadContext)2 org.junit.jupiter.api (org.junit.jupiter.api)2 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)2 Multi (io.smallrye.mutiny.Multi)1 AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)1 Cancellable (io.smallrye.mutiny.subscription.Cancellable)1 UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)1 ChronoUnit (java.time.temporal.ChronoUnit)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 Awaitility.await (org.awaitility.Awaitility.await)1