Search in sources :

Example 96 with Context

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

the class FluxIterableTest method smokeTestIterableConditionalSubscriptionWithInfiniteIterable.

@Test
@Timeout(5)
void smokeTestIterableConditionalSubscriptionWithInfiniteIterable() {
    // this test is simulating a poll() loop over an infinite iterable with conditional fusion enabled
    AtomicInteger backingAtomic = new AtomicInteger();
    Context discardingContext = Operators.enableOnDiscard(Context.empty(), v -> {
    });
    @SuppressWarnings("unchecked") Fuseable.ConditionalSubscriber<Integer> testSubscriber = Mockito.mock(Fuseable.ConditionalSubscriber.class);
    Iterator<Integer> iterator = new Iterator<Integer>() {

        @Override
        public boolean hasNext() {
            // approximate infinite source with a large upper bound instead
            return backingAtomic.get() < 10_000;
        }

        @Override
        public Integer next() {
            return backingAtomic.incrementAndGet();
        }
    };
    FluxIterable.IterableSubscriptionConditional<Integer> subscription = new FluxIterable.IterableSubscriptionConditional<>(testSubscriber, iterator, false);
    subscription.cancel();
    // protected by @Timeout(5)
    Operators.onDiscardQueueWithClear(subscription, discardingContext, null);
    assertThat(backingAtomic).hasValue(0);
}
Also used : Context(reactor.util.context.Context) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) Fuseable(reactor.core.Fuseable) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 97 with Context

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

the class DefaultContextExpectationsTest method notContextAccessibleDueToPublisher.

@Test
public void notContextAccessibleDueToPublisher() {
    Publisher<Integer> publisher = subscriber -> subscriber.onSubscribe(new Subscription() {

        @Override
        public void request(long l) {
            subscriber.onComplete();
        }

        @Override
        public void cancel() {
        // NO-OP
        }
    });
    Step<Integer> step = StepVerifier.create(publisher);
    DefaultContextExpectations<Integer> expectations = new DefaultContextExpectations<>(step, new MessageFormatter(null, null, null));
    assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> expectations.then().verifyComplete()).withMessage("No propagated Context");
}
Also used : ThrowableAssertAlternative(org.assertj.core.api.ThrowableAssertAlternative) Publisher(org.reactivestreams.Publisher) Step(reactor.test.StepVerifier.Step) Context(reactor.util.context.Context) Hooks(reactor.core.publisher.Hooks) HashMap(java.util.HashMap) DefaultContextExpectations(reactor.test.DefaultStepVerifierBuilder.DefaultContextExpectations) Function(java.util.function.Function) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ContextExpectations(reactor.test.StepVerifier.ContextExpectations) Map(java.util.Map) Subscription(org.reactivestreams.Subscription) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) Collections(java.util.Collections) Subscription(org.reactivestreams.Subscription) DefaultContextExpectations(reactor.test.DefaultStepVerifierBuilder.DefaultContextExpectations) Test(org.junit.jupiter.api.Test)

Example 98 with Context

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

the class DefaultContextExpectationsTest method notContainsOnlyOfContextSize.

@Test
public void notContainsOnlyOfContextSize() throws Exception {
    Context expected = Context.of("foo", "bar", "other", "stuff");
    assertContextExpectationFails(s -> s.contextWrite(Context.of("foo", "bar")), e -> e.containsOnly(expected)).withMessageStartingWith("" + "Expected Context to contain same values as Context2{foo=bar, other=stuff}, but they differ in size\n" + "Context: Context1{foo=bar}");
}
Also used : Context(reactor.util.context.Context) ThrowableAssertAlternative(org.assertj.core.api.ThrowableAssertAlternative) Publisher(org.reactivestreams.Publisher) Step(reactor.test.StepVerifier.Step) Context(reactor.util.context.Context) Hooks(reactor.core.publisher.Hooks) HashMap(java.util.HashMap) DefaultContextExpectations(reactor.test.DefaultStepVerifierBuilder.DefaultContextExpectations) Function(java.util.function.Function) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) AfterEach(org.junit.jupiter.api.AfterEach) ContextExpectations(reactor.test.StepVerifier.ContextExpectations) Map(java.util.Map) Subscription(org.reactivestreams.Subscription) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) Collections(java.util.Collections) Test(org.junit.jupiter.api.Test)

Example 99 with Context

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

the class FluxPeekFuseableTest method resumeFuseableConditional.

@Test
public void resumeFuseableConditional() {
    RuntimeException nextError = new IllegalStateException("next");
    List<Throwable> resumedErrors = new ArrayList<>();
    List<Object> resumedValues = new ArrayList<>();
    Context context = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, OnNextFailureStrategy.resume((t, s) -> {
        resumedErrors.add(t);
        resumedValues.add(s);
    }));
    ConditionalAssertSubscriber<Integer> actual = new ConditionalAssertSubscriber<>(context);
    SignalPeekThrowNext<Integer> peekParent = new SignalPeekThrowNext<>(nextError);
    AssertQueueSubscription<Integer> qs = new AssertQueueSubscription<>();
    PeekFuseableConditionalSubscriber<Integer> test = new PeekFuseableConditionalSubscriber<>(actual, peekParent);
    test.onSubscribe(qs);
    test.onNext(1);
    assertThat(actual.next).as("onNext skips").isEmpty();
    assertThat(qs.requested).as("onNext requested more").isEqualTo(1);
    boolean tryOnNext = test.tryOnNext(2);
    assertThat(tryOnNext).as("tryOnNext skips").isFalse();
    qs.offer(3);
    Integer polled = test.poll();
    assertThat(polled).as("poll skips").isNull();
    test.onComplete();
    assertThat(actual.error).isNull();
    assertThat(actual.completed).isTrue();
    assertThat(resumedErrors).containsExactly(nextError, nextError, nextError);
    assertThat(resumedValues).containsExactly(1, 2, 3);
}
Also used : Context(reactor.util.context.Context) PeekConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekConditionalSubscriber) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) 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) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueueSubscription(reactor.core.Fuseable.QueueSubscription) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Thread.sleep(java.lang.Thread.sleep) MockUtils(reactor.test.MockUtils) PeekFuseableSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableSubscriber) Collection(java.util.Collection) Context(reactor.util.context.Context) Schedulers.parallel(reactor.core.scheduler.Schedulers.parallel) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) Assertions.fail(org.assertj.core.api.Assertions.fail) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) PeekFuseableConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableConditionalSubscriber) PeekFuseableConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableConditionalSubscriber) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 100 with Context

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

the class FluxPeekFuseableTest method resumeConditional.

@Test
public void resumeConditional() {
    RuntimeException nextError = new IllegalStateException("next");
    List<Throwable> resumedErrors = new ArrayList<>();
    List<Object> resumedValues = new ArrayList<>();
    Context context = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, OnNextFailureStrategy.resume((t, s) -> {
        resumedErrors.add(t);
        resumedValues.add(s);
    }));
    ConditionalAssertSubscriber<Integer> actual = new ConditionalAssertSubscriber<>(context);
    SignalPeekThrowNext<Integer> peekParent = new SignalPeekThrowNext<>(nextError);
    AssertQueueSubscription<Integer> qs = new AssertQueueSubscription<>();
    PeekConditionalSubscriber<Integer> test = new PeekConditionalSubscriber<>(actual, peekParent);
    test.onSubscribe(qs);
    test.onNext(1);
    assertThat(actual.next).as("onNext skips").isEmpty();
    assertThat(qs.requested).as("onNext requested more").isEqualTo(1);
    boolean tryOnNext = test.tryOnNext(2);
    assertThat(tryOnNext).as("tryOnNext skips").isFalse();
    test.onComplete();
    assertThat(actual.error).isNull();
    assertThat(actual.completed).isTrue();
    assertThat(resumedErrors).containsExactly(nextError, nextError);
    assertThat(resumedValues).containsExactly(1, 2);
}
Also used : Context(reactor.util.context.Context) PeekConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekConditionalSubscriber) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) 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) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueueSubscription(reactor.core.Fuseable.QueueSubscription) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Thread.sleep(java.lang.Thread.sleep) MockUtils(reactor.test.MockUtils) PeekFuseableSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableSubscriber) Collection(java.util.Collection) Context(reactor.util.context.Context) Schedulers.parallel(reactor.core.scheduler.Schedulers.parallel) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) Assertions.fail(org.assertj.core.api.Assertions.fail) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) PeekFuseableConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableConditionalSubscriber) PeekConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekConditionalSubscriber) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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