Search in sources :

Example 36 with Context

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

Example 37 with Context

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

Example 38 with Context

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

Example 39 with Context

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"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StepVerifier(reactor.test.StepVerifier) Retry(reactor.util.retry.Retry) Scannable(reactor.core.Scannable) ContextView(reactor.util.context.ContextView) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Percentage(org.assertj.core.data.Percentage) RetryBackoffSpec(reactor.util.retry.RetryBackoffSpec) Scheduler(reactor.core.scheduler.Scheduler) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) LongAssert(org.assertj.core.api.LongAssert) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Context(reactor.util.context.Context) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) Context(reactor.util.context.Context) Exceptions(reactor.core.Exceptions) ContextView(reactor.util.context.ContextView) Test(org.junit.jupiter.api.Test)

Example 40 with Context

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

Aggregations

Context (reactor.util.context.Context)101 Test (org.junit.jupiter.api.Test)83 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)28 ArrayList (java.util.ArrayList)27 List (java.util.List)27 Subscription (org.reactivestreams.Subscription)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 Collections (java.util.Collections)25 Mono (reactor.core.publisher.Mono)25 Consumer (java.util.function.Consumer)24 Function (java.util.function.Function)24 CoreSubscriber (reactor.core.CoreSubscriber)24 Authentication (org.springframework.security.core.Authentication)23 SecurityContext (org.springframework.security.core.context.SecurityContext)23 Scannable (reactor.core.Scannable)23 Exceptions (reactor.core.Exceptions)22 TestLogger (reactor.test.util.TestLogger)21