Search in sources :

Example 31 with Context

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);
}
Also used : Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) Test(org.junit.Test) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) Exceptions(reactor.core.Exceptions) Nullable(javax.annotation.Nullable) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) RaceTestUtils(reactor.test.RaceTestUtils) Context(reactor.util.context.Context) Test(org.junit.Test)

Example 32 with Context

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));
}
Also used : Context(reactor.util.context.Context) Test(org.junit.Test)

Example 33 with 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");
}
Also used : Context(reactor.util.context.Context) Test(org.junit.Test)

Example 34 with Context

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");
}
Also used : Context(reactor.util.context.Context) ThrowableAssertAlternative(org.assertj.core.api.ThrowableAssertAlternative) Step(reactor.test.StepVerifier.Step) Context(reactor.util.context.Context) HashMap(java.util.HashMap) Test(org.junit.Test) DefaultContextExpectations(reactor.test.DefaultStepVerifierBuilder.DefaultContextExpectations) Function(java.util.function.Function) Objects(java.util.Objects) Flux(reactor.core.publisher.Flux) ContextExpectations(reactor.test.StepVerifier.ContextExpectations) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) Collections(java.util.Collections) Test(org.junit.Test)

Example 35 with Context

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;
    }));
}
Also used : UsernameThreadContext(de.spring.example.context.UsernameThreadContext) WebFilter(org.springframework.web.server.WebFilter) MDC(org.slf4j.MDC) Context(reactor.util.context.Context) Mono(reactor.core.publisher.Mono) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) UsernameThreadContext(de.spring.example.context.UsernameThreadContext) Context(reactor.util.context.Context) UsernameContext(de.spring.example.context.UsernameContext) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Aggregations

Context (reactor.util.context.Context)35 Test (org.junit.Test)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 Consumer (java.util.function.Consumer)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 ArrayList (java.util.ArrayList)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Nullable (javax.annotation.Nullable)8 Scannable (reactor.core.Scannable)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 Function (java.util.function.Function)7 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)7 Flux (reactor.core.publisher.Flux)7 Mono (reactor.core.publisher.Mono)7 StepVerifier (reactor.test.StepVerifier)7 List (java.util.List)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Subscription (org.reactivestreams.Subscription)6 CoreSubscriber (reactor.core.CoreSubscriber)6 Exceptions (reactor.core.Exceptions)6