private void emitValues() { emitter1.onNext(1L); emitter2.onNext(2L); emitter1.onNext(3L); emitter2.onNext(4L); emitter1.onComplete(); emitter2.onComplete(); }
source.onNext(3); source.onNext(4); source.onComplete();
@Test public void timedFused() throws Exception { VirtualTimeScheduler.getOrSet(); ReplayProcessor<Integer> rp = ReplayProcessor.createTimeout(Duration.ofSeconds(1)); for (int i = 0; i < 5; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 5; i < 10; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp) .expectFusion(Fuseable.NONE) .expectNext(5,6,7,8,9) .verifyComplete(); }
@Test public void boundedFused() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, false); for (int i = 0; i < 256; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp) .expectFusion(Fuseable.ASYNC) .expectNextCount(256) .verifyComplete(); }
@Test public void boundedLong() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, false); for (int i = 0; i < 256; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp.hide()) .expectNextCount(16) .verifyComplete(); }
@Test public void unboundedFused() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, true); for (int i = 0; i < 256; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp) .expectFusion(Fuseable.ASYNC) .expectNextCount(256) .verifyComplete(); }
@Test public void timedFusedAfter() throws Exception { ReplayProcessor<Integer> rp = ReplayProcessor.createTimeout(Duration.ofSeconds(1)); StepVerifier.create(rp) .expectFusion(Fuseable.NONE) .then(() -> { for (int i = 0; i < 5; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 5; i < 10; i++) { rp.onNext(i); } rp.onComplete(); }) .expectNext(0,1,2,3,4,5,6,7,8,9) .verifyComplete(); }
@Test public void timed() throws Exception { VirtualTimeScheduler.getOrSet(); ReplayProcessor<Integer> rp = ReplayProcessor.createTimeout(Duration.ofSeconds(1)); for (int i = 0; i < 5; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 5; i < 10; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp.hide()) .expectFusion(Fuseable.NONE) .expectNext(5,6,7,8,9) .verifyComplete(); }
@Test public void timedAndBoundFused() throws Exception { ReplayProcessor<Integer> rp = ReplayProcessor.createSizeAndTimeout(5, Duration.ofSeconds(1)); for (int i = 0; i < 10; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 10; i < 20; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp) .expectFusion(Fuseable.ASYNC) .expectNext(15,16,17,18,19) .verifyComplete(); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); }
@Test public void timedAfter() throws Exception { ReplayProcessor<Integer> rp = ReplayProcessor.createTimeout(Duration.ofSeconds(1)); StepVerifier.create(rp.hide()) .expectFusion(Fuseable.NONE) .then(() -> { for (int i = 0; i < 5; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 5; i < 10; i++) { rp.onNext(i); } rp.onComplete(); }) .expectNext(0,1,2,3,4,5,6,7,8,9) .verifyComplete(); }
@Test public void boundedFusedAfter() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, false); StepVerifier.create(rp) .expectFusion(Fuseable.ASYNC) .then(() -> { for (int i = 0; i < 256; i++) { rp.onNext(i); } rp.onComplete(); }) .expectNextCount(256) .verifyComplete(); }
@Test public void timedAndBound() throws Exception { ReplayProcessor<Integer> rp = ReplayProcessor.createSizeAndTimeout(5, Duration.ofSeconds(1)); for (int i = 0; i < 10; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 10; i < 20; i++) { rp.onNext(i); } rp.onComplete(); StepVerifier.create(rp.hide()) .expectFusion(Fuseable.NONE) .expectNext(15,16,17,18,19) .verifyComplete(); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); }
@Test public void timedAndBoundAfter() throws Exception { ReplayProcessor<Integer> rp = ReplayProcessor.createSizeAndTimeout(5, Duration.ofSeconds(1)); StepVerifier.create(rp.hide()) .expectFusion(Fuseable.NONE) .then(() -> { for (int i = 0; i < 10; i++) { rp.onNext(i); } VirtualTimeScheduler.get().advanceTimeBy(Duration.ofSeconds(2)); for (int i = 10; i < 20; i++) { rp.onNext(i); } rp.onComplete(); }) .expectNextCount(20) .verifyComplete(); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); }
@Test public void analyticsTest() throws Exception { ReplayProcessor<Integer> source = ReplayProcessor.create(); long avgTime = 50l; Mono<Long> result = source .log("delay") .publishOn(asyncGroup) .delayElements(Duration.ofMillis(avgTime)) .elapsed() .skip(1) .groupBy(w -> w.getT1()) .flatMap(w -> w.count().map(c -> Tuples.of(w.key(), c))) .log("elapsed") .collectSortedList(Comparator.comparing(Tuple2::getT1)) .flatMapMany(Flux::fromIterable) .reduce(-1L, (acc, next) -> acc > 0l ? ((next.getT1() + acc) / 2) : next.getT1()) .log("reduced-elapsed") .cache(); source.subscribe(); for (int j = 0; j < 10; j++) { source.onNext(1); } source.onComplete(); Assert.assertTrue(result.block(Duration.ofSeconds(5)) >= avgTime * 0.6); }
@Test public void unboundedLong() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, true); AssertSubscriber<Integer> ts = AssertSubscriber.create(0L); for (int i = 0; i < 256; i++) { rp.onNext(i); } rp.onComplete(); rp.subscribe(ts); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); ts.assertNoValues(); ts.request(Long.MAX_VALUE); ts.assertValueCount(256) .assertNoError() .assertComplete(); }
@Test public void unbounded() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, true); AssertSubscriber<Integer> ts = AssertSubscriber.create(0L); rp.subscribe(ts); rp.onNext(1); rp.onNext(2); rp.onNext(3); rp.onComplete(); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); ts.assertNoValues(); ts.request(1); ts.assertValues(1); ts.request(2); ts.assertValues(1, 2, 3) .assertNoError() .assertComplete(); }
@Test public void bounded() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, false); AssertSubscriber<Integer> ts = AssertSubscriber.create(0L); rp.subscribe(ts); rp.onNext(1); rp.onNext(2); rp.onNext(3); rp.onComplete(); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); ts.assertNoValues(); ts.request(1); ts.assertValues(1); ts.request(2); ts.assertValues(1, 2, 3) .assertNoError() .assertComplete(); }
@Test public void unboundedAfter() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, true); AssertSubscriber<Integer> ts = AssertSubscriber.create(0L); rp.onNext(1); rp.onNext(2); rp.onNext(3); rp.onComplete(); rp.subscribe(ts); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); ts.assertNoValues(); ts.request(1); ts.assertValues(1); ts.request(2); ts.assertValues(1, 2, 3) .assertNoError() .assertComplete(); }
@Test public void boundedAfter() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, false); AssertSubscriber<Integer> ts = AssertSubscriber.create(0L); rp.onNext(1); rp.onNext(2); rp.onNext(3); rp.onComplete(); rp.subscribe(ts); Assert.assertFalse("Has subscribers?", rp.hasDownstreams()); ts.assertNoValues(); ts.request(1); ts.assertValues(1); ts.request(2); ts.assertValues(1, 2, 3) .assertNoError() .assertComplete(); }
@Test public void checkHotSource() { ReplayProcessor<Long> processor = ReplayProcessor.create(1); processor.onNext(1L); processor.onNext(2L); processor.onNext(3L); StepVerifier.create(processor.switchOnFirst((s, f) -> f.filter(v -> v % s.get() == 0))) .expectNext(3L) .then(() -> { processor.onNext(4L); processor.onNext(5L); processor.onNext(6L); processor.onNext(7L); processor.onNext(8L); processor.onNext(9L); processor.onComplete(); }) .expectNext(6L, 9L) .verifyComplete(); } }