Search in sources :

Example 41 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class SignalLoggerTests method currentContextLogWhenDebug.

@Test
public void currentContextLogWhenDebug() {
    TestLogger logger = new TestLogger();
    SignalLogger<?> signalLogger = new SignalLogger<>(Mono.empty(), null, Level.FINE, false, name -> logger);
    assertThat(logger.getOutContent()).as("before currentContext()").isEmpty();
    Context context = Context.of("foo", "bar");
    signalLogger.onCurrentContextCall().accept(context);
    assertThat(logger.getOutContent()).startsWith("[DEBUG] (").endsWith(") currentContext(Context1{foo=bar})\n");
}
Also used : Context(reactor.util.context.Context) TestLogger(reactor.test.util.TestLogger) Test(org.junit.jupiter.api.Test)

Example 42 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class DirectProcessorTest method currentContextDelegatesToFirstSubscriber.

@Test
public void currentContextDelegatesToFirstSubscriber() {
    AssertSubscriber<Object> testSubscriber1 = new AssertSubscriber<>(Context.of("key", "value1"));
    AssertSubscriber<Object> testSubscriber2 = new AssertSubscriber<>(Context.of("key", "value2"));
    DirectProcessor<Object> directProcessor = new DirectProcessor<>();
    directProcessor.subscribe(testSubscriber1);
    directProcessor.subscribe(testSubscriber2);
    Context processorContext = directProcessor.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 43 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class MonoCollectListTest method discardCancelNextRace.

@Test
@Tag("slow")
public void discardCancelNextRace() {
    AtomicInteger doubleDiscardCounter = new AtomicInteger();
    Context discardingContext = Operators.enableOnDiscard(null, o -> {
        AtomicBoolean ab = (AtomicBoolean) o;
        if (ab.getAndSet(true)) {
            doubleDiscardCounter.incrementAndGet();
        }
    });
    for (int i = 0; i < 100_000; i++) {
        AssertSubscriber<List<AtomicBoolean>> testSubscriber = new AssertSubscriber<>(discardingContext);
        MonoCollectListSubscriber<AtomicBoolean> subscriber = new MonoCollectListSubscriber<>(testSubscriber);
        subscriber.onSubscribe(Operators.emptySubscription());
        AtomicBoolean extraneous = new AtomicBoolean(false);
        RaceTestUtils.race(subscriber::cancel, () -> subscriber.onNext(extraneous));
        testSubscriber.assertNoValues();
        if (!extraneous.get()) {
            LOGGER.info("" + subscriber.list);
        }
        assertThat(extraneous).as("released %d", i).isTrue();
    }
    LOGGER.info("{} discarded twice or more in discardCancelNextRace", doubleDiscardCounter.get());
}
Also used : Context(reactor.util.context.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MonoCollectListSubscriber(reactor.core.publisher.MonoCollectList.MonoCollectListSubscriber) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) List(java.util.List) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 44 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class MonoCollectTest method discardCancelNextRace.

@Test
public void discardCancelNextRace() {
    lessVerboseLogs(Operators.class);
    AtomicInteger doubleDiscardCounter = new AtomicInteger();
    Context discardingContext = Operators.enableOnDiscard(null, o -> {
        AtomicBoolean ab = (AtomicBoolean) o;
        if (ab.getAndSet(true)) {
            doubleDiscardCounter.incrementAndGet();
        }
    });
    for (int i = 0; i < 100_000; i++) {
        AssertSubscriber<List<AtomicBoolean>> testSubscriber = new AssertSubscriber<>(discardingContext);
        CollectSubscriber<AtomicBoolean, List<AtomicBoolean>> subscriber = new CollectSubscriber<>(testSubscriber, List::add, new ArrayList<>());
        subscriber.onSubscribe(Operators.emptySubscription());
        AtomicBoolean extraneous = new AtomicBoolean(false);
        RaceTestUtils.race(subscriber::cancel, () -> subscriber.onNext(extraneous));
        testSubscriber.assertNoValues();
        if (!extraneous.get()) {
            LOGGER.info("" + subscriber.container);
        }
        assertThat(extraneous).as("released %d", i).isTrue();
    }
    LOGGER.info("discarded twice or more: {}", doubleDiscardCounter.get());
}
Also used : Context(reactor.util.context.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) ArrayList(java.util.ArrayList) List(java.util.List) CollectSubscriber(reactor.core.publisher.MonoCollect.CollectSubscriber) Test(org.junit.jupiter.api.Test)

Example 45 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class LambdaMonoSubscriberTest method initialContextIsUsedForOnErrorDropped.

@Test
public void initialContextIsUsedForOnErrorDropped() {
    AtomicReference<Throwable> droppedRef = new AtomicReference<>();
    Context ctx = Context.of(Hooks.KEY_ON_ERROR_DROPPED, (Consumer<Throwable>) droppedRef::set);
    IllegalStateException expectDropped = new IllegalStateException("boom2");
    LambdaMonoSubscriber<Object> sub = new LambdaMonoSubscriber<>(null, e -> {
    }, null, null, ctx);
    sub.onError(new IllegalStateException("boom1"));
    // now trigger drop
    sub.onError(expectDropped);
    assertThat(droppedRef).hasValue(expectDropped);
}
Also used : Context(reactor.util.context.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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