use of reactor.util.context.Context in project reactor-core by reactor.
the class FluxDoOnEachTest method nextCompleteAndErrorHaveContext.
@Test
public void nextCompleteAndErrorHaveContext() {
Context context = Context.of("foo", "bar");
List<Signal> signals = new ArrayList<>();
StepVerifier.create(Flux.just("hello").doOnEach(signals::add), StepVerifierOptions.create().withInitialContext(context)).expectNext("hello").verifyComplete();
assertThat(signals).allSatisfy(signal -> assertThat(signal.getContextView().hasKey("foo")).as("has Context value").isTrue());
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class EmitterProcessorTest method currentContextDelegatesToFirstSubscriber.
@Test
public void currentContextDelegatesToFirstSubscriber() {
AssertSubscriber<Object> testSubscriber1 = new AssertSubscriber<>(Context.of("key", "value1"));
AssertSubscriber<Object> testSubscriber2 = new AssertSubscriber<>(Context.of("key", "value2"));
EmitterProcessor<Object> emitterProcessor = new EmitterProcessor<>(false, 1);
emitterProcessor.subscribe(testSubscriber1);
emitterProcessor.subscribe(testSubscriber2);
Context processorContext = emitterProcessor.currentContext();
assertThat(processorContext.getOrDefault("key", "EMPTY")).isEqualTo("value1");
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class StepVerifierAssertionsTests method contextDiscardCaptureWithInitialContext.
@Test
public void contextDiscardCaptureWithInitialContext() {
Context initial = Context.of("foo", "bar");
StepVerifier.create(Mono.deferContextual(Mono::just).flatMapIterable(ctx -> ctx.stream().map(Map.Entry::getKey).map(String::valueOf).collect(Collectors.toList())).concatWithValues("A", "B").filter(s -> s.length() > 1), StepVerifierOptions.create().withInitialContext(initial)).expectNext("foo").expectNext("reactor.onDiscard.local").expectComplete().verifyThenAssertThat().hasDiscardedExactly("A", "B");
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class MonoCompletionStage method subscribe.
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
Operators.MonoSubscriber<T, T> sds = new Operators.MonoSubscriber<>(actual);
actual.onSubscribe(sds);
if (sds.isCancelled()) {
return;
}
future.whenComplete((v, e) -> {
if (sds.isCancelled()) {
// nobody is interested in the Mono anymore, don't risk dropping errors
Context ctx = sds.currentContext();
if (e == null || e instanceof CancellationException) {
// we discard any potential value and ignore Future cancellations
Operators.onDiscard(v, ctx);
} else {
// we make sure we keep _some_ track of a Future failure AFTER the Mono cancellation
Operators.onErrorDropped(e, ctx);
// and we discard any potential value just in case both e and v are not null
Operators.onDiscard(v, ctx);
}
return;
}
try {
if (e instanceof CompletionException) {
actual.onError(e.getCause());
} else if (e != null) {
actual.onError(e);
} else if (v != null) {
sds.complete(v);
} else {
actual.onComplete();
}
} catch (Throwable e1) {
Operators.onErrorDropped(e1, actual.currentContext());
throw Exceptions.bubble(e1);
}
});
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class FluxExpandTest method currentContextForExpandDepthSubscriber.
@Test
public void currentContextForExpandDepthSubscriber() {
final Context context = Context.of("foo", "bar");
CoreSubscriber<Integer> parentActual = new BaseSubscriber<Integer>() {
@Override
public Context currentContext() {
return context;
}
};
ExpandDepthSubscription<Integer> expandDepthSubscription = new ExpandDepthSubscription<>(parentActual, i -> i > 5 ? Mono.empty() : Mono.just(i + 1), 123);
ExpandDepthSubscriber<Integer> test = new ExpandDepthSubscriber<>(expandDepthSubscription);
assertThat(test.currentContext()).isSameAs(context);
}
Aggregations