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());
}
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);
}
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);
}
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));
}
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");
}
Aggregations