Search in sources :

Example 86 with Context

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

the class MonoCollectTest method discardCancelCompleteRace.

@Test
public void discardCancelCompleteRace() {
    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 resource = new AtomicBoolean(false);
        subscriber.onNext(resource);
        RaceTestUtils.race(subscriber::cancel, subscriber::onComplete);
        if (testSubscriber.values().isEmpty()) {
            assertThat(resource).as("not completed and 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 87 with Context

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

the class LambdaSubscriberTest 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");
    LambdaSubscriber<Object> sub = new LambdaSubscriber<>(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)

Example 88 with Context

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

the class FluxDeferContextual method subscribe.

@Override
public void subscribe(CoreSubscriber<? super T> actual) {
    Publisher<? extends T> p;
    Context ctx = actual.currentContext();
    try {
        p = Objects.requireNonNull(contextualPublisherFactory.apply(ctx.readOnly()), "The Publisher returned by the contextualPublisherFactory is null");
    } catch (Throwable e) {
        Operators.error(actual, Operators.onOperatorError(e, ctx));
        return;
    }
    from(p).subscribe(actual);
}
Also used : Context(reactor.util.context.Context)

Example 89 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.jupiter.api.Test)

Example 90 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.getContextView().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.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