Search in sources :

Example 26 with AssertSubscriber

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

the class FluxPublishOnTest method rejectedExecutionExceptionOnDataSignalExecutor.

@Test(timeout = 5000)
public void rejectedExecutionExceptionOnDataSignalExecutor() throws InterruptedException {
    final AtomicReference<Throwable> throwableInOnOperatorError = new AtomicReference<>();
    final AtomicReference<Object> dataInOnOperatorError = new AtomicReference<>();
    try {
        CountDownLatch hookLatch = new CountDownLatch(1);
        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) {
            }
        }).publishOn(fromExecutor(executor)).subscribe(assertSubscriber);
        executor.shutdownNow();
        assertSubscriber.assertNoValues().assertNoError().assertNotComplete();
        hookLatch.await();
        Assert.assertThat(throwableInOnOperatorError.get(), CoreMatchers.instanceOf(RejectedExecutionException.class));
        Assert.assertSame(dataInOnOperatorError.get(), 0);
    } finally {
        Hooks.resetOnOperatorError();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) ExecutorService(java.util.concurrent.ExecutorService) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.Test)

Example 27 with AssertSubscriber

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

the class FluxFlatMapTest method singleSubscriberOnly.

@Test
public void singleSubscriberOnly() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger emission = new AtomicInteger();
    Flux<Integer> source = Flux.range(1, 2).doOnNext(v -> emission.getAndIncrement());
    EmitterProcessor<Integer> source1 = EmitterProcessor.create();
    EmitterProcessor<Integer> source2 = EmitterProcessor.create();
    source.flatMap(v -> v == 1 ? source1 : source2, 1, 32).subscribe(ts);
    Assert.assertEquals(1, emission.get());
    ts.assertNoValues().assertNoError().assertNotComplete();
    Assert.assertTrue("source1 no subscribers?", source1.downstreamCount() != 0);
    Assert.assertFalse("source2 has subscribers?", source2.downstreamCount() != 0);
    source1.onNext(1);
    source2.onNext(10);
    source1.onComplete();
    source2.onNext(2);
    source2.onComplete();
    ts.assertValues(1, 10, 2).assertNoError().assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Assert(org.junit.Assert) Exceptions(reactor.core.Exceptions) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 28 with AssertSubscriber

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

the class FluxFlatMapTest method flatMapUnbounded.

@Test
public void flatMapUnbounded() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger emission = new AtomicInteger();
    Flux<Integer> source = Flux.range(1, 1000).doOnNext(v -> emission.getAndIncrement());
    EmitterProcessor<Integer> source1 = EmitterProcessor.create();
    EmitterProcessor<Integer> source2 = EmitterProcessor.create();
    source.flatMap(v -> v == 1 ? source1 : source2, Integer.MAX_VALUE, 32).subscribe(ts);
    Assert.assertEquals(1000, emission.get());
    ts.assertNoValues().assertNoError().assertNotComplete();
    Assert.assertTrue("source1 no subscribers?", source1.downstreamCount() != 0);
    Assert.assertTrue("source2 no  subscribers?", source2.downstreamCount() != 0);
    source1.onNext(1);
    source1.onComplete();
    source2.onNext(2);
    source2.onComplete();
    ts.assertValueCount(1000).assertNoError().assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Assert(org.junit.Assert) Exceptions(reactor.core.Exceptions) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 29 with AssertSubscriber

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

the class FluxGenerateTest method stateConsumerCalled.

@Test
public void stateConsumerCalled() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger stateConsumer = new AtomicInteger();
    Flux.<Integer, Integer>generate(() -> 1, (s, o) -> {
        o.complete();
        return s;
    }, stateConsumer::set).subscribe(ts);
    ts.assertNoValues().assertComplete().assertNoError();
    Assert.assertEquals(1, stateConsumer.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) StepVerifier(reactor.test.StepVerifier) Iterator(java.util.Iterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scannable(reactor.core.Scannable) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Assert(org.junit.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 30 with AssertSubscriber

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

the class FluxGroupByTest method twoGroupsConsumeWithSubscribePrefetchSmaller.

@Test
public void twoGroupsConsumeWithSubscribePrefetchSmaller() {
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts3 = AssertSubscriber.create();
    ts3.onSubscribe(Operators.emptySubscription());
    Flux.range(0, 1_000_000).groupBy(v -> v & 1).subscribe(new CoreSubscriber<GroupedFlux<Integer, Integer>>() {

        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(GroupedFlux<Integer, Integer> t) {
            if (t.key() == 0) {
                t.publishOn(Schedulers.fromExecutorService(forkJoinPool), 32).subscribe(ts1);
            } else {
                t.publishOn(Schedulers.fromExecutorService(forkJoinPool), 32).subscribe(ts2);
            }
        }

        @Override
        public void onError(Throwable t) {
            ts3.onError(t);
        }

        @Override
        public void onComplete() {
            ts3.onComplete();
        }
    });
    if (!ts1.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("main subscriber timed out");
    }
    if (!ts2.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("group 0 subscriber timed out");
    }
    if (!ts3.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("group 1 subscriber timed out");
    }
    ts1.assertValueCount(500_000).assertNoError().assertComplete();
    ts2.assertValueCount(500_000).assertNoError().assertComplete();
    ts3.assertNoValues().assertNoError().assertComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Queues(reactor.util.concurrent.Queues) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) Ignore(org.junit.Ignore) ForkJoinPool(java.util.concurrent.ForkJoinPool) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Assert(org.junit.Assert) Subscription(org.reactivestreams.Subscription) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest)

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