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