Search in sources :

Example 16 with AssertSubscriber

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

the class ParallelFluxTest method parallelMode.

@Test
public void parallelMode() {
    Flux<Integer> source = Flux.range(1, 1_000_000).hide();
    int ncpu = Math.max(8, Runtime.getRuntime().availableProcessors());
    for (int i = 1; i < ncpu + 1; i++) {
        Scheduler scheduler = Schedulers.newParallel("test", i);
        try {
            Flux<Integer> result = ParallelFlux.from(source, i).runOn(scheduler).map(v -> v + 1).sequential();
            AssertSubscriber<Integer> ts = AssertSubscriber.create();
            result.subscribe(ts);
            ts.await(Duration.ofSeconds(10));
            ts.assertSubscribed().assertValueCount(1_000_000).assertComplete().assertNoError();
        } finally {
            scheduler.dispose();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Scheduler(reactor.core.scheduler.Scheduler) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) Scheduler(reactor.core.scheduler.Scheduler) Test(org.junit.Test)

Example 17 with AssertSubscriber

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

the class MonoPublishOnTest method rejectedExecutionExceptionOnDataSignalExecutorService.

@Test
public void rejectedExecutionExceptionOnDataSignalExecutorService() throws InterruptedException {
    int data = 1;
    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<>();
        Mono.just(data).publishOn(fromExecutorService(executor)).doOnNext(s -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
            }
        }).publishOn(fromExecutorService(executor)).subscribe(assertSubscriber);
        executor.shutdownNow();
        assertSubscriber.assertNoValues().assertNoError().assertNotComplete();
        hookLatch.await();
        assertThat(throwableInOnOperatorError.get(), instanceOf(RejectedExecutionException.class));
        Assert.assertSame(dataInOnOperatorError.get(), data);
    } 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) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 18 with AssertSubscriber

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

the class MonoPublishOnTest method rejectedExecutionExceptionOnDataSignalExecutor.

@Test
public void rejectedExecutionExceptionOnDataSignalExecutor() throws InterruptedException {
    int data = 1;
    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<>();
        Mono.just(data).publishOn(fromExecutorService(executor)).doOnNext(s -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
            }
        }).publishOn(fromExecutor(executor)).subscribe(assertSubscriber);
        executor.shutdownNow();
        assertSubscriber.assertNoValues().assertNoError().assertNotComplete();
        hookLatch.await();
        assertThat(throwableInOnOperatorError.get(), instanceOf(RejectedExecutionException.class));
        Assert.assertSame(dataInOnOperatorError.get(), data);
    } 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) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 19 with AssertSubscriber

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

the class MonoRepeatWhenEmptyTest method repeatFinite.

@Test
public void repeatFinite() {
    AtomicInteger c = new AtomicInteger();
    Mono<String> source = Mono.defer(() -> c.getAndIncrement() < 3 ? Mono.empty() : Mono.just("test-data"));
    List<Long> iterations = new ArrayList<>();
    AssertSubscriber<String> ts = AssertSubscriber.create();
    source.repeatWhenEmpty(1000, o -> o.doOnNext(iterations::add)).subscribe(ts);
    ts.assertValues("test-data").assertComplete().assertNoError();
    Assert.assertEquals(4, c.get());
    Assert.assertEquals(Arrays.asList(0L, 1L, 2L), iterations);
}
Also used : Arrays(java.util.Arrays) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Test(org.junit.Test) Assert(org.junit.Assert) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 20 with AssertSubscriber

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

the class MonoDoOnEachTest method normal.

@Test
public void normal() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger onNext = new AtomicInteger();
    AtomicReference<Throwable> onError = new AtomicReference<>();
    AtomicBoolean onComplete = new AtomicBoolean();
    Mono.just(1).hide().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()).isEqualTo(1);
    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)

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