@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 sampleTest() throws Exception { CountDownLatch latch = new CountDownLatch(1); Disposable top10every1second = Flux.fromIterable(PULP_SAMPLE) .publishOn(asyncGroup) .flatMap(samuelJackson -> Flux .fromArray(samuelJackson.split(" ")) .publishOn(asyncGroup) .filter(w -> !w.trim().isEmpty()) .doOnNext(i -> simulateLatency()) ) .window(Duration.ofSeconds(2)) .flatMap(s -> s.groupBy(w -> w) .flatMap(w -> w.count().map(c -> Tuples.of(w.key(), c))) .collectSortedList((a, b) -> -a.getT2().compareTo(b.getT2())) .flatMapMany(Flux::fromIterable) .take(10) .doAfterTerminate(() -> LOG.info("------------------------ window terminated" + "----------------------")) ) .subscribe( entry -> LOG.info(entry.getT1() + ": " + entry.getT2()), error -> LOG.error("", error), latch::countDown ); awaitLatch(top10every1second, latch); }
@Test public void data6() { // tag::6[] Flux.just("alpha", "bravo", "charlie") .map(String::toUpperCase) .flatMap(s -> Flux.fromArray(s.split(""))) .groupBy(String::toString) .sort((o1, o2) -> o1.key().compareTo(o2.key())) .flatMap(group -> Mono.just(group.key()).zipWith(group.count())) .map(keyAndCount -> keyAndCount.getT1() + " => " + keyAndCount.getT2()) .subscribe(System.out::println); // end::6[] }