Search in sources :

Example 76 with Context

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

the class OperatorsTest method onDiscardCallbackErrorsLog.

@Test
public void onDiscardCallbackErrorsLog() {
    Context context = Operators.enableOnDiscard(Context.empty(), t -> {
        throw new RuntimeException("Boom");
    });
    TestLogger testLogger = new TestLogger();
    LoggerUtils.enableCaptureWith(testLogger);
    try {
        Operators.onDiscard("Foo", context);
        assertThat(testLogger.getErrContent()).contains("Error in discard hook - java.lang.RuntimeException: Boom");
    } finally {
        LoggerUtils.disableCapture();
    }
}
Also used : Context(reactor.util.context.Context) TestLogger(reactor.test.util.TestLogger) Test(org.junit.jupiter.api.Test)

Example 77 with Context

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

the class OperatorsTest method discardStreamContinuesWhenElementFailsToBeDiscarded.

@Test
public void discardStreamContinuesWhenElementFailsToBeDiscarded() {
    Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
    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(stream, hookContext);
    assertThat(discardedCount).hasValue(4);
}
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 78 with Context

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

the class OperatorsTest method discardQueueWithClearContinuesOnExtractionError.

@Test
public void discardQueueWithClearContinuesOnExtractionError() {
    AtomicInteger discardedCount = new AtomicInteger();
    Context hookContext = Operators.discardLocalAdapter(Integer.class, i -> {
        if (i == 3)
            throw new IllegalStateException("boom");
        discardedCount.incrementAndGet();
    }).apply(Context.empty());
    Queue<List<Integer>> q = new ArrayBlockingQueue<>(5);
    q.add(Collections.singletonList(1));
    q.add(Collections.singletonList(2));
    q.add(Arrays.asList(3, 30));
    q.add(Collections.singletonList(4));
    q.add(Collections.singletonList(5));
    Operators.onDiscardQueueWithClear(q, hookContext, o -> {
        List<Integer> l = o;
        if (l.size() == 2)
            throw new IllegalStateException("boom in extraction");
        return l.stream();
    });
    assertThat(discardedCount).hasValue(4);
}
Also used : Context(reactor.util.context.Context) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 79 with Context

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

the class OperatorsTest method onNextFailureWithStrategyNotMatchingDoesCancel.

@Test
public void onNextFailureWithStrategyNotMatchingDoesCancel() {
    Context context = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, new OnNextFailureStrategy() {

        @Override
        public boolean test(Throwable error, @Nullable Object value) {
            return false;
        }

        @Override
        public Throwable process(Throwable error, @Nullable Object value, Context context) {
            return error;
        }
    });
    Operators.DeferredSubscription s = new Operators.DeferredSubscription();
    Throwable t = Operators.onNextError("foo", new NullPointerException("bar"), context, s);
    assertThat(t).as("exception processed").isNotNull().isInstanceOf(NullPointerException.class).hasNoSuppressedExceptions().hasNoCause();
    assertThat(s.isCancelled()).as("subscription cancelled").isTrue();
}
Also used : Context(reactor.util.context.Context) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) Test(org.junit.jupiter.api.Test)

Example 80 with Context

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

the class OperatorsTest method pollErrorModeLocalStrategy.

@Test
public void pollErrorModeLocalStrategy() {
    List<Object> nextDropped = new ArrayList<>();
    List<Object> errorDropped = new ArrayList<>();
    Hooks.onNextDropped(nextDropped::add);
    Hooks.onErrorDropped(errorDropped::add);
    Context c = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, OnNextFailureStrategy.RESUME_DROP);
    Exception error = new IllegalStateException("boom");
    assertThat(Hooks.onNextErrorHook).as("no global hook").isNull();
    RuntimeException e = Operators.onNextPollError("foo", error, c);
    assertThat(e).isNull();
    assertThat(nextDropped).containsExactly("foo");
    assertThat(errorDropped).containsExactly(error);
}
Also used : Context(reactor.util.context.Context) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) 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