@SuppressWarnings("unchecked") void testReplayProcessorState(ReplayProcessor<String> rp){ Disposable d1 = rp.subscribe(); rp.subscribe(); ReplayProcessor.ReplayInner<String> s = ((ReplayProcessor.ReplayInner<String>) rp.inners().findFirst().get()); assertThat(d1).isEqualTo(s.actual()); assertThat(s.isEmpty()).isTrue(); assertThat(s.isCancelled()).isFalse(); assertThat(s.isCancelled()).isFalse(); assertThat(rp.getPrefetch()).isEqualTo(Integer.MAX_VALUE); if(rp.getBufferSize() != Integer.MAX_VALUE) { assertThat(rp.getBufferSize()).isEqualTo(1); } FluxSink<String> sink = rp.sink(); sink.next("test"); rp.onComplete(); rp.onComplete(); Exception e = new RuntimeException("test"); try{ rp.onError(e); Assert.fail(); } catch (Exception t){ assertThat(Exceptions.unwrap(t)).isEqualTo(e); } }
@Test public void cancel() { ReplayProcessor<Integer> rp = ReplayProcessor.create(16, false); AssertSubscriber<Integer> ts = AssertSubscriber.create(); rp.subscribe(ts); ts.cancel(); 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(); }