Search in sources :

Example 21 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class MonoDoOnEachTest method empty.

@Test
public void empty() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger onNext = new AtomicInteger();
    AtomicReference<Throwable> onError = new AtomicReference<>();
    AtomicBoolean onComplete = new AtomicBoolean();
    Mono.<Integer>empty().doOnEach(s -> {
        if (s.isOnNext()) {
            onNext.incrementAndGet();
        } else if (s.isOnError()) {
            onError.set(s.getThrowable());
        } else if (s.isOnComplete()) {
            onComplete.set(true);
        }
    }).subscribe(ts);
    assertThat(onNext.get()).isZero();
    assertThat(onError.get()).isNull();
    assertThat(onComplete.get()).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) StepVerifier(reactor.test.StepVerifier) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assertions(org.assertj.core.api.Assertions) Exceptions(reactor.core.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 22 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class MonoDoOnEachTest method never.

@Test
public void never() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger onNext = new AtomicInteger();
    AtomicReference<Throwable> onError = new AtomicReference<>();
    AtomicBoolean onComplete = new AtomicBoolean();
    Mono.<Integer>never().doOnEach(s -> {
        if (s.isOnNext()) {
            onNext.incrementAndGet();
        } else if (s.isOnError()) {
            onError.set(s.getThrowable());
        } else if (s.isOnComplete()) {
            onComplete.set(true);
        }
    }).subscribe(ts);
    assertThat(onNext.get()).isZero();
    assertThat(onError.get()).isNull();
    assertThat(onComplete.get()).isFalse();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) StepVerifier(reactor.test.StepVerifier) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assertions(org.assertj.core.api.Assertions) Exceptions(reactor.core.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 23 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class MonoExpandTest method depthCancelRace2.

@Test
public void depthCancelRace2() throws Exception {
    for (int i = 0; i < 1000; i++) {
        final TestPublisher<Integer> pp = TestPublisher.create();
        Flux<Integer> source = Mono.just(0).expandDeep(it -> pp);
        final CountDownLatch cdl = new CountDownLatch(1);
        AssertSubscriber<Integer> ts = new AssertSubscriber<Integer>() {

            final AtomicInteger sync = new AtomicInteger(2);

            @Override
            public void onNext(Integer t) {
                super.onNext(t);
                Schedulers.single().schedule(() -> {
                    if (sync.decrementAndGet() != 0) {
                        while (sync.get() != 0) {
                        }
                    }
                    cancel();
                    cdl.countDown();
                });
                if (sync.decrementAndGet() != 0) {
                    while (sync.get() != 0) {
                    }
                }
            }
        };
        source.subscribe(ts);
        assertThat(cdl.await(5, TimeUnit.SECONDS)).as("runs under 5s").isTrue();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 24 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class FluxPeekFuseableTest method syncFusionAvailable.

@Test
public void syncFusionAvailable() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    Flux.range(1, 2).doOnNext(v -> {
    }).subscribe(ts);
    Subscription s = ts.upstream();
    Assert.assertTrue("Non-fuseable upstream: " + s, s instanceof Fuseable.QueueSubscription);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeekConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekConditionalSubscriber) CoreMatchers(org.hamcrest.CoreMatchers) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(reactor.core.scheduler.Schedulers) Thread.sleep(java.lang.Thread.sleep) MockUtils(reactor.test.MockUtils) Nullable(javax.annotation.Nullable) PeekFuseableSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableSubscriber) Context(reactor.util.context.Context) Test(org.junit.Test) Schedulers.parallel(reactor.core.scheduler.Schedulers.parallel) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Assert(org.junit.Assert) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) PeekFuseableConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableConditionalSubscriber) Subscription(org.reactivestreams.Subscription) Fuseable(reactor.core.Fuseable) Test(org.junit.Test)

Example 25 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class FluxPublishOnTest method rejectedExecutionExceptionOnErrorSignalExecutorService.

@Test(timeout = 5000)
public void rejectedExecutionExceptionOnErrorSignalExecutorService() throws InterruptedException {
    Exception exception = new IllegalStateException();
    final AtomicReference<Throwable> throwableInOnOperatorError = new AtomicReference<>();
    final AtomicReference<Object> dataInOnOperatorError = new AtomicReference<>();
    try {
        CountDownLatch hookLatch = new CountDownLatch(2);
        Hooks.onOperatorError((t, d) -> {
            throwableInOnOperatorError.set(t);
            dataInOnOperatorError.set(d);
            hookLatch.countDown();
            return t;
        });
        ExecutorService executor = newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(1);
        AssertSubscriber<Integer> assertSubscriber = new AssertSubscriber<>();
        Flux.range(0, 5).publishOn(fromExecutorService(executor)).doOnNext(s -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw Exceptions.propagate(exception);
            }
        }).publishOn(fromExecutorService(executor)).subscribe(assertSubscriber);
        executor.shutdownNow();
        assertSubscriber.assertNoValues().assertNoError().assertNotComplete();
        hookLatch.await();
        Assert.assertThat(throwableInOnOperatorError.get(), CoreMatchers.instanceOf(RejectedExecutionException.class));
        Assert.assertSame(throwableInOnOperatorError.get().getSuppressed()[0], exception);
    } finally {
        Hooks.resetOnOperatorError();
    }
}
Also used : AssertSubscriber(reactor.test.subscriber.AssertSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) ExecutorService(java.util.concurrent.ExecutorService) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)65 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)65 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)52 StepVerifier (reactor.test.StepVerifier)51 List (java.util.List)49 CoreSubscriber (reactor.core.CoreSubscriber)46 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)44 Assert (org.junit.Assert)43 Scannable (reactor.core.Scannable)41 Arrays (java.util.Arrays)40 Subscription (org.reactivestreams.Subscription)37 ArrayList (java.util.ArrayList)33 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)33 Schedulers (reactor.core.scheduler.Schedulers)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 Duration (java.time.Duration)21 Fuseable (reactor.core.Fuseable)21 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)20 Exceptions (reactor.core.Exceptions)19 Queues (reactor.util.concurrent.Queues)19