Search in sources :

Example 6 with Context

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

the class SignalTest method nextStateWithContext.

@Test
public void nextStateWithContext() {
    Context context = Context.of("foo", "bar");
    Signal<Integer> s = Signal.next(1, context);
    assertThat(s.getContext().isEmpty()).as("has context").isFalse();
    assertThat(s.isOnComplete()).isFalse();
    assertThat(s.isOnSubscribe()).isFalse();
    assertThat(s.hasError()).isFalse();
    assertThat(s.hasValue()).isTrue();
    assertThat(s).isEqualTo(Signal.next(1));
    assertThat(s).isNotEqualTo(Signal.next(2));
    assertThat(s).isNotEqualTo(Signal.error(e));
    assertThat(s).isNotEqualTo(Signal.complete());
    assertThat(s).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()));
    assertThat(s.hashCode()).isEqualTo(Signal.next(1).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.next(2).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.error(e).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.complete().hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
    assertThat(Signal.isComplete(s)).isFalse();
    assertThat(Signal.isError(s)).isFalse();
    assertThat(s.get()).isEqualTo(1);
    assertThat(s.getType()).isEqualTo(SignalType.ON_NEXT);
    assertThat(s.toString()).contains("onNext(1)");
    StepVerifier.create(Flux.<Integer>from(sub -> {
        sub.onSubscribe(Operators.emptySubscription());
        s.accept(sub);
    })).expectNext(1).thenCancel().verify();
}
Also used : Context(reactor.util.context.Context) Test(org.junit.Test)

Example 7 with Context

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

the class SignalTest method subscribeStateWithContext.

@Test
public void subscribeStateWithContext() {
    Context context = Context.of("foo", "bar");
    Signal<Integer> s = Signal.subscribe(Operators.emptySubscription(), context);
    assertThat(s.getContext().isEmpty()).as("has context").isFalse();
    assertThat(s.isOnComplete()).isFalse();
    assertThat(s.isOnSubscribe()).isTrue();
    assertThat(s.hasError()).isFalse();
    assertThat(s.hasValue()).isFalse();
    assertThat(s).isEqualTo(Signal.subscribe(Operators.emptySubscription()));
    assertThat(s).isNotEqualTo(Signal.subscribe(Operators.cancelledSubscription()));
    assertThat(s).isNotEqualTo(Signal.next(1));
    assertThat(s).isNotEqualTo(Signal.error(e));
    assertThat(s).isNotEqualTo(Signal.complete());
    assertThat(s.hashCode()).isEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.cancelledSubscription()).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.next(1).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.error(e).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.complete().hashCode());
    assertThat(Signal.isComplete(s)).isFalse();
    assertThat(Signal.isError(s)).isFalse();
    assertThat(s.getSubscription()).isEqualTo(Operators.emptySubscription());
    assertThat(s.getType()).isEqualTo(SignalType.ON_SUBSCRIBE);
    assertThat(s.toString()).contains("onSubscribe");
    StepVerifier.create(Flux.<Integer>from(s::accept)).expectSubscription().thenCancel().verify();
}
Also used : Context(reactor.util.context.Context) Test(org.junit.Test)

Example 8 with Context

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

the class SignalTest method completeStateWithContext.

@Test
public void completeStateWithContext() {
    Context context = Context.of("foo", "bar");
    Signal<Integer> s = Signal.complete(context);
    assertThat(s.getContext().isEmpty()).as("has context").isFalse();
    assertThat(s.isOnComplete()).isTrue();
    assertThat(s.isOnSubscribe()).isFalse();
    assertThat(s.hasError()).isFalse();
    assertThat(s.hasValue()).isFalse();
    assertThat(s).isEqualTo(Signal.complete());
    assertThat(s).isNotEqualTo(Signal.error(e));
    assertThat(s).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()));
    assertThat(s).isNotEqualTo(Signal.next(1));
    assertThat(s.hashCode()).isEqualTo(Signal.complete().hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.error(e).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.next(1).hashCode());
    assertThat(s.hashCode()).isNotEqualTo(Signal.subscribe(Operators.emptySubscription()).hashCode());
    assertThat(Signal.isComplete(s)).isTrue();
    assertThat(Signal.isError(s)).isFalse();
    assertThat(s.getType()).isEqualTo(SignalType.ON_COMPLETE);
    assertThat(s.toString()).contains("onComplete");
    StepVerifier.create(Flux.<Integer>from(sub -> {
        sub.onSubscribe(Operators.emptySubscription());
        s.accept(sub);
    })).verifyComplete();
}
Also used : Context(reactor.util.context.Context) Test(org.junit.Test)

Example 9 with Context

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

the class OperatorsTest method onNextErrorModeLocalStrategy.

@Test
public void onNextErrorModeLocalStrategy() {
    List<Object> nextDropped = new ArrayList<>();
    List<Object> errorDropped = new ArrayList<>();
    Hooks.onNextDropped(nextDropped::add);
    Hooks.onErrorDropped(errorDropped::add);
    Hooks.onNextError(OnNextFailureStrategy.STOP);
    Context c = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, OnNextFailureStrategy.RESUME_DROP);
    Exception error = new IllegalStateException("boom");
    DeferredSubscription s = new Operators.DeferredSubscription();
    try {
        assertThat(s.isCancelled()).as("s initially cancelled").isFalse();
        Throwable e = Operators.onNextError("foo", error, c, s);
        assertThat(e).isNull();
        assertThat(nextDropped).containsExactly("foo");
        assertThat(errorDropped).containsExactly(error);
        assertThat(s.isCancelled()).as("s cancelled").isFalse();
    } finally {
        Hooks.resetOnNextDropped();
        Hooks.resetOnErrorDropped();
        Hooks.resetOnNextError();
    }
}
Also used : Context(reactor.util.context.Context) ArrayList(java.util.ArrayList) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Example 10 with Context

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

the class OperatorsTest method onNextFailureWithStrategyMatchingButNotNullDoesCancel.

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

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

        @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) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) Test(org.junit.Test)

Aggregations

Context (reactor.util.context.Context)35 Test (org.junit.Test)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 Consumer (java.util.function.Consumer)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 ArrayList (java.util.ArrayList)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Nullable (javax.annotation.Nullable)8 Scannable (reactor.core.Scannable)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 Function (java.util.function.Function)7 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)7 Flux (reactor.core.publisher.Flux)7 Mono (reactor.core.publisher.Mono)7 StepVerifier (reactor.test.StepVerifier)7 List (java.util.List)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Subscription (org.reactivestreams.Subscription)6 CoreSubscriber (reactor.core.CoreSubscriber)6 Exceptions (reactor.core.Exceptions)6