use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalTest method subscribeStateWithContext.
@Test
public void subscribeStateWithContext() {
Context context = Context.of("foo", "bar");
Signal<Integer> s = Signal.subscribe(Operators.emptySubscription(), context);
assertThat(s.getContextView().isEmpty()).as("has context").isFalse();
assertThat(s.isOnComplete()).isFalse();
assertThat(s.isOnSubscribe()).isTrue();
assertThat(s.hasError()).isFalse();
assertThat(s.hasValue()).isFalse();
assertThat(s).isEqualTo(Signal.subscribe(Operators.emptySubscription()));
assertThat(s).isNotEqualTo(Signal.subscribe(Operators.cancelledSubscription()));
assertThat(s).isNotEqualTo(Signal.next(1));
assertThat(s).isNotEqualTo(Signal.error(e));
assertThat(s).isNotEqualTo(Signal.complete());
assertThat(s.hashCode()).isEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.cancelledSubscription()).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.next(1).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.error(e).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.complete().hashCode());
assertThat(Signal.isComplete(s)).isFalse();
assertThat(Signal.isError(s)).isFalse();
assertThat(s.getSubscription()).isEqualTo(Operators.emptySubscription());
assertThat(s.getType()).isEqualTo(SignalType.ON_SUBSCRIBE);
assertThat(s.toString()).contains("onSubscribe");
StepVerifier.create(Flux.<Integer>from(s::accept)).expectSubscription().thenCancel().verify();
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalTest method nextStateWithContext.
@Test
public void nextStateWithContext() {
Context context = Context.of("foo", "bar");
Signal<Integer> s = Signal.next(1, context);
assertThat(s.getContextView().isEmpty()).as("has context").isFalse();
assertThat(s.isOnComplete()).isFalse();
assertThat(s.isOnSubscribe()).isFalse();
assertThat(s.hasError()).isFalse();
assertThat(s.hasValue()).isTrue();
assertThat(s).isEqualTo(Signal.next(1));
assertThat(s).isNotEqualTo(Signal.next(2));
assertThat(s).isNotEqualTo(Signal.error(e));
assertThat(s).isNotEqualTo(Signal.complete());
assertThat(s).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()));
assertThat(s.hashCode()).isEqualTo(Signal.next(1).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.next(2).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.error(e).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.complete().hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
assertThat(Signal.isComplete(s)).isFalse();
assertThat(Signal.isError(s)).isFalse();
assertThat(s.get()).isEqualTo(1);
assertThat(s.getType()).isEqualTo(SignalType.ON_NEXT);
assertThat(s.toString()).contains("onNext(1)");
StepVerifier.create(Flux.<Integer>from(sub -> {
sub.onSubscribe(Operators.emptySubscription());
s.accept(sub);
})).expectNext(1).thenCancel().verify();
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalTest method completeStateWithContext.
@Test
public void completeStateWithContext() {
Context context = Context.of("foo", "bar");
Signal<Integer> s = Signal.complete(context);
assertThat(s.getContextView().isEmpty()).as("has context").isFalse();
assertThat(s.isOnComplete()).isTrue();
assertThat(s.isOnSubscribe()).isFalse();
assertThat(s.hasError()).isFalse();
assertThat(s.hasValue()).isFalse();
assertThat(s).isEqualTo(Signal.complete());
assertThat(s).isNotEqualTo(Signal.error(e));
assertThat(s).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()));
assertThat(s).isNotEqualTo(Signal.next(1));
assertThat(s.hashCode()).isEqualTo(Signal.complete().hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.error(e).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.next(1).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
assertThat(Signal.isComplete(s)).isTrue();
assertThat(Signal.isError(s)).isFalse();
assertThat(s.getType()).isEqualTo(SignalType.ON_COMPLETE);
assertThat(s.toString()).contains("onComplete");
StepVerifier.create(Flux.<Integer>from(sub -> {
sub.onSubscribe(Operators.emptySubscription());
s.accept(sub);
})).verifyComplete();
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class FluxRetryWhenTest method retryWhenContextTrigger_MergesOriginalContext.
@Test
public void retryWhenContextTrigger_MergesOriginalContext() {
final int RETRY_COUNT = 3;
List<Integer> retriesLeft = Collections.synchronizedList(new ArrayList<>(4));
List<ContextView> contextPerRetry = Collections.synchronizedList(new ArrayList<>(4));
Flux<Object> retryWithContext = Flux.error(new IllegalStateException("boom")).doOnEach(sig -> {
retriesLeft.add(sig.getContextView().get("retriesLeft"));
if (!sig.isOnNext()) {
contextPerRetry.add(sig.getContextView());
}
}).retryWhen(Retry.from(retrySignalFlux -> retrySignalFlux.handle((rs, sink) -> {
Context ctx = sink.currentContext();
int rl = ctx.getOrDefault("retriesLeft", 0);
if (rl > 0) {
sink.next(Context.of("retriesLeft", rl - 1));
} else {
sink.error(Exceptions.retryExhausted("retries exhausted", rs.failure()));
}
}))).contextWrite(Context.of("retriesLeft", RETRY_COUNT)).contextWrite(Context.of("thirdPartyContext", "present"));
StepVerifier.create(retryWithContext).expectErrorSatisfies(e -> assertThat(e).matches(Exceptions::isRetryExhausted, "isRetryExhausted").hasMessage("retries exhausted").hasCause(new IllegalStateException("boom"))).verify(Duration.ofSeconds(1));
assertThat(retriesLeft).containsExactly(3, 2, 1, 0);
assertThat(contextPerRetry).allMatch(ctx -> ctx.hasKey("thirdPartyContext"));
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalLoggerTests method currentContextLogWhenTrace.
@Test
public void currentContextLogWhenTrace() {
TestLogger logger = new TestLogger();
SignalLogger<?> signalLogger = new SignalLogger<>(Mono.empty(), null, Level.FINEST, false, name -> logger);
assertThat(logger.getOutContent()).as("before currentContext()").isEmpty();
Context context = Context.of("foo", "bar");
signalLogger.onCurrentContextCall().accept(context);
assertThat(logger.getOutContent()).startsWith("[TRACE] (").endsWith(") currentContext(Context1{foo=bar})\n");
}
Aggregations