@Test public void monoNotCancelledByMonoProcessor() { AtomicLong cancelCounter = new AtomicLong(); MonoProcessor<String> monoProcessor = Mono.just("foo") .doOnCancel(cancelCounter::incrementAndGet) .toProcessor(); monoProcessor.subscribe(); assertThat(cancelCounter.get()).isEqualTo(0); }
@Test public void MonoProcessorRejectedSubscribeCallback() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<Throwable> ref = new AtomicReference<>(); mp.subscribe(v -> {}, ref::set); mp.onError(new Exception("test")); assertThat(ref.get()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void testMono() throws Exception { MonoProcessor<String> promise = MonoProcessor.create(); promise.onNext("test"); final CountDownLatch successCountDownLatch = new CountDownLatch(1); promise.subscribe(v -> successCountDownLatch.countDown()); assertThat("Failed", successCountDownLatch.await(10, TimeUnit.SECONDS)); }
@Test(expected = NullPointerException.class) public void MonoProcessorRejectedSubscribeCallbackNull() { MonoProcessor<String> mp = MonoProcessor.create(); mp.subscribe((Subscriber<String>)null); }
@Test public void MonoProcessorSuccessSubscribeCallback() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<String> ref = new AtomicReference<>(); mp.subscribe(ref::set); mp.onNext("test"); assertThat(ref.get()).isEqualToIgnoringCase("test"); assertThat(mp.isSuccess()).isTrue(); assertThat(mp.isError()).isFalse(); }
@Test public void MonoProcessorRejectedChainTogether() { MonoProcessor<String> mp = MonoProcessor.create(); MonoProcessor<String> mp2 = MonoProcessor.create(); mp.subscribe(mp2); mp.onError(new Exception("test")); assertThat(mp2.getError()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void MonoProcessorThenFulfill() { MonoProcessor<Integer> mp = MonoProcessor.create(); mp.onNext(1); MonoProcessor<Integer> mp2 = mp.flatMap(s -> Mono.just(s * 2)) .toProcessor(); mp2.subscribe(); assertThat(mp2.isTerminated()).isTrue(); assertThat(mp2.isSuccess()).isTrue(); assertThat(mp2.peek()).isEqualTo(2); }
@Test public void MonoProcessorMapFulfill() { MonoProcessor<Integer> mp = MonoProcessor.create(); mp.onNext(1); MonoProcessor<Integer> mp2 = mp.map(s -> s * 2) .toProcessor(); mp2.subscribe(); assertThat(mp2.isTerminated()).isTrue(); assertThat(mp2.isSuccess()).isTrue(); assertThat(mp2.peek()).isEqualTo(2); }
@Test public void onMonoRejectedDoOnErrorClazzNot() { Mono<String> mp = Mono.error(new TestException()); AtomicReference<Throwable> ref = new AtomicReference<>(); MonoProcessor<String> processor = mp.doOnError(RuntimeException.class, ref::set) .toProcessor(); processor.subscribe(); assertThat(processor.getError()).isInstanceOf(TestException.class); assertThat(ref.get()).isNull(); }
@Test public void monoToProcessorReusesInstance() { MonoProcessor<String> monoProcessor = Mono.just("foo") .toProcessor(); assertThat(monoProcessor) .isSameAs(monoProcessor.toProcessor()) .isSameAs(monoProcessor.subscribe()); }
@Test public void fluxCanBeEnforcedToDispatchValuesDistinctFromPredecessors() { // "A Flux can be enforced to dispatch values distinct from their immediate predecessors" // given:"a composable with values 1 to 3 with duplicates" Flux<Integer> s = Flux.fromIterable(Arrays.asList(1, 1, 2, 2, 3)); // when:"the values are filtered and result is collected" MonoProcessor<List<Integer>> tap = s.distinctUntilChanged() .collectList() .toProcessor(); tap.subscribe(); // then:"collected must remove duplicates" assertThat(tap.block()).containsExactly(1, 2, 3); }
@Test public void cancel() { TestPublisher<String> cancelTester = TestPublisher.create(); MonoProcessor<Integer> processor = cancelTester.mono() .flatMap(s -> Mono.just(s.length())) .toProcessor(); processor.subscribe(); processor.cancel(); cancelTester.assertCancelled(); }
@Test public void MonoProcessorSuccessChainTogether() { MonoProcessor<String> mp = MonoProcessor.create(); MonoProcessor<String> mp2 = MonoProcessor.create(); mp.subscribe(mp2); mp.onNext("test"); assertThat(mp2.peek()).isEqualToIgnoringCase("test"); assertThat(mp.isSuccess()).isTrue(); assertThat(mp.isError()).isFalse(); }
@Test public void fluxCanBeEnforcedToDispatchValuesHavingDistinctKeys() { // "A Flux can be enforced to dispatch values having distinct keys" // given: "a composable with values 1 to 4 with duplicate keys" Flux<Integer> s = Flux.fromIterable(Arrays.asList(1, 2, 3, 1, 2, 3, 4)); // when: "the values are filtered and result is collected" MonoProcessor<List<Integer>> tap = s.distinct(it -> it % 3) .collectList() .toProcessor(); tap.subscribe(); // then: "collected should be without duplicates" assertThat(tap.block()).containsExactly(1, 2, 3); }
@Test public void fluxCanBeEnforcedToDispatchDistinctValues() { // "A Flux can be enforced to dispatch distinct values" // given:"a composable with values 1 to 4 with duplicates" Flux<Integer> s = Flux.fromIterable(Arrays.asList(1, 2, 3, 1, 2, 3, 4)); // when:"the values are filtered and result is collected" MonoProcessor<List<Integer>> tap = s.distinct() .collectList() .toProcessor(); tap.subscribe(); // then:"collected should be without duplicates" assertThat(tap.block()).containsExactly(1, 2, 3, 4); }
@Test public void reduceWillAccumulateListOfAcceptedValues() { // "Reduce will accumulate a list of accepted values" // given: "a composable" FluxProcessor<Integer, Integer> source = EmitterProcessor.create(); Mono<List<Integer>> reduced = source.collectList(); MonoProcessor<List<Integer>> value = reduced.toProcessor(); value.subscribe(); // when: "the first value is accepted" source.onNext(1); source.onComplete(); // then: "the list contains the first element" assertThat(value.block()).containsExactly(1); }
@Test public void cancel() { TestPublisher<String> cancelTester = TestPublisher.create(); MonoProcessor<Boolean> processor = cancelTester.flux() .any(s -> s.length() > 100) .toProcessor(); processor.subscribe(); processor.cancel(); cancelTester.assertCancelled(); }
@Test public void cancel() { TestPublisher<String> cancelTester = TestPublisher.create(); MonoProcessor<String> processor = cancelTester.flux() .elementAt(1000) .toProcessor(); processor.subscribe(); processor.cancel(); cancelTester.assertCancelled(); }
@Test public void cancel() { TestPublisher<String> cancelTester = TestPublisher.create(); MonoProcessor<Void> processor = cancelTester.flux() .then() .toProcessor(); processor.subscribe(); processor.cancel(); cancelTester.assertCancelled(); }
@Test public void cancel() { TestPublisher<String> cancelTester = TestPublisher.create(); MonoProcessor<String> processor = cancelTester.flux() .next() .toProcessor(); processor.subscribe(); processor.cancel(); cancelTester.assertCancelled(); }