Search in sources :

Example 81 with Context

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

the class OperatorsTest method onRejectedExecutionWithDataSignalDelegatesToErrorLocal.

@Test
public void onRejectedExecutionWithDataSignalDelegatesToErrorLocal() {
    BiFunction<Throwable, Object, Throwable> localHook = (e, v) -> new IllegalStateException("boom_" + v, e);
    Context c = Context.of(Hooks.KEY_ON_OPERATOR_ERROR, localHook);
    IllegalArgumentException failure = new IllegalArgumentException("foo");
    final Throwable throwable = Operators.onRejectedExecution(failure, null, null, "bar", c);
    assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("boom_bar").hasNoSuppressedExceptions();
    assertThat(throwable.getCause()).isInstanceOf(RejectedExecutionException.class).hasMessage("Scheduler unavailable").hasCause(failure);
}
Also used : Arrays(java.util.Arrays) Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) RaceTestUtils(reactor.test.util.RaceTestUtils) Nullable(javax.annotation.Nullable) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) Iterator(java.util.Iterator) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Stream(java.util.stream.Stream) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) Context(reactor.util.context.Context) Test(org.junit.jupiter.api.Test)

Example 82 with Context

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

the class OperatorsTest method onOperatorErrorLocal.

@Test
public void onOperatorErrorLocal() {
    BiFunction<Throwable, Object, Throwable> localHook = (e, v) -> new IllegalStateException("boom_" + v, e);
    Context c = Context.of(Hooks.KEY_ON_OPERATOR_ERROR, localHook);
    IllegalArgumentException failure = new IllegalArgumentException("foo");
    final Throwable throwable = Operators.onOperatorError(null, failure, "foo", c);
    assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("boom_foo").hasCause(failure);
}
Also used : Arrays(java.util.Arrays) Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) RaceTestUtils(reactor.test.util.RaceTestUtils) Nullable(javax.annotation.Nullable) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) Iterator(java.util.Iterator) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Stream(java.util.stream.Stream) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) Context(reactor.util.context.Context) Test(org.junit.jupiter.api.Test)

Example 83 with Context

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

the class OperatorsTest method discardCollectionStopsOnIsEmptyError.

@Test
public void discardCollectionStopsOnIsEmptyError() {
    @SuppressWarnings("unchecked") List<Integer> mock = Mockito.mock(List.class);
    Mockito.when(mock.isEmpty()).thenThrow(new IllegalStateException("isEmpty boom"));
    AtomicInteger discardedCount = new AtomicInteger();
    Context hookContext = Operators.discardLocalAdapter(Integer.class, i -> discardedCount.incrementAndGet()).apply(Context.empty());
    Operators.onDiscardMultiple(mock, hookContext);
    assertThat(discardedCount).hasValue(0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(reactor.util.context.Context) Arrays(java.util.Arrays) Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) RaceTestUtils(reactor.test.util.RaceTestUtils) Nullable(javax.annotation.Nullable) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) Iterator(java.util.Iterator) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Stream(java.util.stream.Stream) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 84 with Context

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

the class OperatorsTest method discardIteratorStopsOnIterationError.

@Test
public void discardIteratorStopsOnIterationError() {
    List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
    Iterator<Integer> trueIterator = elements.iterator();
    Iterator<Integer> failingIterator = new Iterator<Integer>() {

        @Override
        public boolean hasNext() {
            return trueIterator.hasNext();
        }

        @Override
        public Integer next() {
            Integer n = trueIterator.next();
            if (n >= 3)
                throw new IllegalStateException("Iterator boom");
            return n;
        }
    };
    AtomicInteger discardedCount = new AtomicInteger();
    Context hookContext = Operators.discardLocalAdapter(Integer.class, i -> {
        if (i == 3)
            throw new IllegalStateException("boom");
        discardedCount.incrementAndGet();
    }).apply(Context.empty());
    Operators.onDiscardMultiple(failingIterator, true, hookContext);
    assertThat(discardedCount).hasValue(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(reactor.util.context.Context) Arrays(java.util.Arrays) Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) RaceTestUtils(reactor.test.util.RaceTestUtils) Nullable(javax.annotation.Nullable) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) Iterator(java.util.Iterator) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Stream(java.util.stream.Stream) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Example 85 with Context

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

the class MonoCollectListTest method discardCancelCompleteRace.

@Test
@Tag("slow")
public void discardCancelCompleteRace() {
    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 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 in discardCancelCompleteRace", 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)

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