use of reactor.util.context.Context in project reactor-core by reactor.
the class OperatorsTest method onRejectedExecutionWithDataSignalDelegatesToErrorLocal.
@Test
public void onRejectedExecutionWithDataSignalDelegatesToErrorLocal() {
BiFunction<Throwable, Object, Throwable> localHook = (e, v) -> new IllegalStateException("boom_" + v, e);
Context c = Context.of(Hooks.KEY_ON_OPERATOR_ERROR, localHook);
IllegalArgumentException failure = new IllegalArgumentException("foo");
final Throwable throwable = Operators.onRejectedExecution(failure, null, null, "bar", c);
assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("boom_bar").hasNoSuppressedExceptions();
assertThat(throwable.getCause()).isInstanceOf(RejectedExecutionException.class).hasMessage("Scheduler unavailable").hasCause(failure);
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalTest method completeWithContextCreatesNewInstances.
@Test
public void completeWithContextCreatesNewInstances() {
Context context = Context.of("foo", "bar");
assertThat(Signal.complete(context)).isNotSameAs(Signal.complete(context)).isNotSameAs(Signal.complete()).isEqualTo(Signal.complete()).isEqualTo(Signal.complete(context));
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class SignalTest method errorStateWithContext.
@Test
public void errorStateWithContext() {
Context context = Context.of("foo", "bar");
Signal<Integer> s = Signal.error(e, context);
assertThat(s.getContext().isEmpty()).as("has context").isFalse();
assertThat(s.isOnComplete()).isFalse();
assertThat(s.isOnSubscribe()).isFalse();
assertThat(s.hasError()).isTrue();
assertThat(s.hasValue()).isFalse();
assertThat(s).isEqualTo(Signal.error(e));
assertThat(s).isNotEqualTo(Signal.error(new Exception("test2")));
assertThat(s).isNotEqualTo(Signal.complete());
assertThat(s).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()));
assertThat(s).isNotEqualTo(Signal.next(1));
assertThat(s.hashCode()).isEqualTo(Signal.error(e).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.error(new Exception("test2")).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.complete().hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.next(1).hashCode());
assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
assertThat(Signal.isComplete(s)).isFalse();
assertThat(Signal.isError(s)).isTrue();
assertThat(s.getThrowable()).isEqualTo(e);
assertThat(s.getType()).isEqualTo(SignalType.ON_ERROR);
assertThat(s.toString()).contains("onError");
StepVerifier.create(Flux.<Integer>from(sub -> {
sub.onSubscribe(Operators.emptySubscription());
s.accept(sub);
})).verifyErrorMessage("test");
}
use of reactor.util.context.Context in project reactor-core by reactor.
the class DefaultContextExpectationsTest method notContainsOnlyOfContextSize.
@Test
public void notContainsOnlyOfContextSize() throws Exception {
Context expected = Context.of("foo", "bar", "other", "stuff");
assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar")), e -> e.containsOnly(expected)).withMessage("Expected Context Context1{foo=bar} to contain same values as " + "Context2{foo=bar, other=stuff}, but they differ in size");
}
use of reactor.util.context.Context in project JavaForFun by gumartinm.
the class UsernameFilter method filter.
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
if (!request.getHeaders().containsKey(UsernameThreadContext.USERNAME_HEADER)) {
return chain.filter(exchange);
}
String username = request.getHeaders().get(UsernameThreadContext.USERNAME_HEADER).get(0);
return chain.filter(exchange).compose(function -> function.then(Mono.subscriberContext()).doOnSubscribe(onSubscribe -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
}).doOnError(throwable -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
}).onErrorMap(throwable -> {
MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
return throwable;
}).doFinally(onFinally -> {
MDC.remove(UsernameThreadContext.USERNAME_HEADER);
}).flatMap(context -> {
Mono<Void> continuation = Mono.empty();
return continuation;
}).subscriberContext(context -> {
Context updatedContext = context;
if (!context.hasKey(UsernameContext.class)) {
updatedContext = context.put(UsernameContext.class, new UsernameContext(username));
}
return updatedContext;
}));
}
Aggregations