Search in sources :

Example 61 with AssertSubscriber

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

the class MonoRepeatWhenEmptyTest method repeatInfinite.

@Test
public void repeatInfinite() {
    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(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 62 with AssertSubscriber

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

the class MonoRepeatWhenEmptyTest method repeatFiniteExceeded.

@Test
public void repeatFiniteExceeded() {
    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(2, o -> o.doOnNext(iterations::add)).subscribe(ts);
    ts.assertError(IllegalStateException.class);
    Assert.assertEquals(3, c.get());
    Assert.assertEquals(Arrays.asList(0L, 1L), 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 63 with AssertSubscriber

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

the class MonoDoOnEachTest method error.

@Test
public void error() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger onNext = new AtomicInteger();
    AtomicReference<Throwable> onError = new AtomicReference<>();
    AtomicBoolean onComplete = new AtomicBoolean();
    Mono.<Integer>error(new RuntimeException("forced failure")).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()).isInstanceOf(RuntimeException.class).hasMessage("forced failure");
    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 64 with AssertSubscriber

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

the class ParallelFluxTest method parallelModeFused.

@Test
public void parallelModeFused() {
    Flux<Integer> source = Flux.range(1, 1_000_000);
    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 65 with AssertSubscriber

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

the class ParallelMergeReduceTest method parallelReduceFull.

@Test
public void parallelReduceFull() {
    int m = 100_000;
    for (int n = 1; n <= m; n *= 10) {
        // System.out.println(n);
        for (int i = 1; i <= Runtime.getRuntime().availableProcessors(); i++) {
            // System.out.println("  " + i);
            Scheduler scheduler = Schedulers.newParallel("test", i);
            try {
                AssertSubscriber<Long> ts = AssertSubscriber.create();
                Flux.range(1, n).map(v -> (long) v).parallel(i).runOn(scheduler).reduce((a, b) -> a + b).subscribe(ts);
                ts.await(Duration.ofSeconds(500));
                long e = ((long) n) * (1 + n) / 2;
                ts.assertValues(e);
            } finally {
                scheduler.dispose();
            }
        }
    }
}
Also used : CoreSubscriber(reactor.core.CoreSubscriber) Scannable(reactor.core.Scannable) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Schedulers(reactor.core.scheduler.Schedulers) Test(org.junit.Test) MergeReduceInner(reactor.core.publisher.ParallelMergeReduce.MergeReduceInner) Scheduler(reactor.core.scheduler.Scheduler) MergeReduceMain(reactor.core.publisher.ParallelMergeReduce.MergeReduceMain) Scheduler(reactor.core.scheduler.Scheduler) 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