@Test public void monoToProcessorChainColdToHot() { AtomicInteger subscriptionCount = new AtomicInteger(); Mono<String> coldToHot = Mono.just("foo") .doOnSubscribe(sub -> subscriptionCount.incrementAndGet()) .cache() .toProcessor() //this actually subscribes .filter(s -> s.length() < 4); assertThat(subscriptionCount.get()).isEqualTo(1); coldToHot.block(); coldToHot.block(); coldToHot.block(); assertThat(subscriptionCount.get()).isEqualTo(1); }
@Test public void filterMonoProcessorError() { MonoProcessor<Integer> mp = MonoProcessor.create(); MonoProcessor<Integer> mp2 = MonoProcessor.create(); StepVerifier.create(mp.filter(s -> {throw new RuntimeException("test"); }) .subscribeWith (mp2)) .then(() -> mp.onNext(2)) .then(() -> assertThat(mp2.isError()).isTrue()) .then(() -> assertThat(mp2.isSuccess()).isFalse()) .then(() -> assertThat(mp2.getError()).hasMessage("test")) .then(() -> assertThat(mp2.isTerminated()).isTrue()) .verifyErrorMessage("test"); }
@Test public void filterMonoProcessor() { MonoProcessor<Integer> mp = MonoProcessor.create(); MonoProcessor<Integer> mp2 = MonoProcessor.create(); StepVerifier.create(mp.filter(s -> s % 2 == 0).subscribeWith(mp2)) .then(() -> mp.onNext(2)) .then(() -> assertThat(mp2.isError()).isFalse()) .then(() -> assertThat(mp2.isSuccess()).isTrue()) .then(() -> assertThat(mp2.peek()).isEqualTo(2)) .then(() -> assertThat(mp2.isTerminated()).isTrue()) .expectNext(2) .verifyComplete(); }
@Test public void filterMonoProcessorNot() { MonoProcessor<Integer> mp = MonoProcessor.create(); MonoProcessor<Integer> mp2 = MonoProcessor.create(); StepVerifier.create(mp.filter(s -> s % 2 == 0).subscribeWith(mp2)) .then(() -> mp.onNext(1)) .then(() -> assertThat(mp2.isError()).isFalse()) .then(() -> assertThat(mp2.isSuccess()).isTrue()) .then(() -> assertThat(mp2.peek()).isNull()) .then(() -> assertThat(mp2.isTerminated()).isTrue()) .verifyComplete(); }
@Test public void asyncFusionBackpressured() { AssertSubscriber<Object> ts = AssertSubscriber.create(1); MonoProcessor<Integer> up = MonoProcessor.create(); Mono.just(1) .hide() .flatMap(w -> up.filter(v -> (v & 1) == 0)) .subscribe(ts); up.onNext(2); ts.assertValues(2) .assertNoError() .assertComplete(); try{ up.onNext(3); } catch(Exception e){ Assert.assertTrue(Exceptions.isCancel(e)); } ts.assertValues(2) .assertNoError() .assertComplete(); }
@Test public void asyncFusion() { AssertSubscriber<Object> ts = AssertSubscriber.create(); MonoProcessor<Integer> up = MonoProcessor.create(); up.filter(v -> (v & 1) == 0) .subscribe(ts); up.onNext(2); up.onComplete(); ts.assertValues(2) .assertNoError() .assertComplete(); }