Search in sources :

Example 91 with Context

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

Example 92 with Context

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

Example 93 with Context

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

Example 94 with Context

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);
        }
    });
}
Also used : Context(reactor.util.context.Context) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException)

Example 95 with Context

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);
}
Also used : Context(reactor.util.context.Context) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpandDepthSubscriber(reactor.core.publisher.FluxExpand.ExpandDepthSubscriber) ExpandDepthSubscription(reactor.core.publisher.FluxExpand.ExpandDepthSubscription) 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