/** * Skip elements from this {@link Flux} emitted within the specified initial duration. * * <p> * <img class="marble" src="doc-files/marbles/skipWithTimespan.svg" alt=""> * * @reactor.discard This operator discards elements that are skipped. * * @param timespan the initial time window during which to drop elements * * @return a {@link Flux} dropping at the beginning until the end of the given duration */ public final Flux<T> skip(Duration timespan) { return skip(timespan, Schedulers.parallel()); }
Flux<Integer> scenario_aFluxCanBeSkippedByTime2(){ return Flux.range(0, 1000) .skip(Duration.ofMillis(2000)); }
Flux<Integer> scenario_aFluxCanBeSkippedByTime(){ return Flux.range(0, 1000) .skip(Duration.ofSeconds(2)); }
Flux<Integer> scenario_aFluxCanBeSkippedByTimeZero(){ return Flux.range(0, 1000) .skip(Duration.ofMillis(0)); }
@Override protected List<Scenario<String, String>> scenarios_errorFromUpstreamFailure() { return Arrays.asList(scenario(f -> f.skip(1))); }
@Test(expected = IllegalArgumentException.class) public void skipInvalid() { Flux.never().skip(-1); }
@Override protected List<Scenario<String, String>> scenarios_errorFromUpstreamFailure() { return Arrays.asList( // We have to skip the first element because the generic tests do not // handle the seed being sent in response to demand prior to the tests // throwing an error. scenario(f -> f.scan(item(0), (a, b) -> a).skip(1)) ); }
@Override protected List<Scenario<String, String>> scenarios_operatorSuccess() { return Arrays.asList( scenario(f -> f.skip(1)).receiveValues(item(1), item(2)) ); }
@Test public void aFluxCanBeSkipped(){ StepVerifier.create(Flux.just("test", "test2", "test3") .skip(1) ) .expectNext("test2", "test3") .verifyComplete(); }
@Test public void aFluxCanBeSkippedZero(){ StepVerifier.create(Flux.just("test", "test2", "test3") .skip(0) ) .expectNext("test", "test2", "test3") .verifyComplete(); }
@Test public void normal() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .skip(5) .subscribe(ts); ts.assertValues(6, 7, 8, 9, 10) .assertComplete() .assertNoError(); }
@Test public void skipAll() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .skip(Long.MAX_VALUE) .subscribe(ts); ts.assertNoValues() .assertComplete() .assertNoError(); }
@Test public void subsequentSum() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); range(1, 5).publish(o -> zip((Object[] a) -> (Integer) a[0] + (Integer) a[1], o, o.skip(1))) .subscribe(ts); ts.assertValues(1 + 2, 2 + 3, 3 + 4, 4 + 5) .assertNoError() .assertComplete(); }
@Test public void subsequentSumHidden() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); range(1, 5).hide() .publish(o -> zip((Object[] a) -> (Integer) a[0] + (Integer) a[1], o, o .skip(1))) .subscribe(ts); ts.assertValues(1 + 2, 2 + 3, 3 + 4, 4 + 5) .assertNoError() .assertComplete(); }
@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 subsequentSumAsync() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); UnicastProcessor<Integer> up = UnicastProcessor.create(Queues.<Integer>get(16).get()); up.publish(o -> zip((Object[] a) -> (Integer) a[0] + (Integer) a[1], o, o.skip(1))) .subscribe(ts); up.onNext(1); up.onNext(2); up.onNext(3); up.onNext(4); up.onNext(5); up.onComplete(); ts.assertValues(1 + 2, 2 + 3, 3 + 4, 4 + 5) .assertNoError() .assertComplete(); }
@Test public void pairWise() { AssertSubscriber<Tuple2<Integer, Integer>> ts = AssertSubscriber.create(); range(1, 9).transform(o -> zip(o, o.skip(1))) .subscribe(ts); ts.assertValues(Tuples.of(1, 2), Tuples.of(2, 3), Tuples.of(3, 4), Tuples.of(4, 5), Tuples.of(5, 6), Tuples.of(6, 7), Tuples.of(7, 8), Tuples.of(8, 9)) .assertComplete(); }
@Test public void normalBackpressured() { AssertSubscriber<Integer> ts = AssertSubscriber.create(0); Flux.range(1, 10) .skip(5) .subscribe(ts); ts.assertNoValues() .assertNoError() .assertNotComplete(); ts.request(2); ts.assertValues(6, 7) .assertNotComplete() .assertNoError(); ts.request(10); ts.assertValues(6, 7, 8, 9, 10) .assertComplete() .assertNoError(); }
@Test public void contextGetHide() throws InterruptedException { StepVerifier.create(Flux.range(1, 1000) .hide() .log() .map(d -> d) .handle((d, c) -> c.next(c.currentContext().get("test") + "" + d)) .skip(3) .take(3) .handle((d, c) -> c.next(c.currentContext().get("test2") + "" + d)) .subscriberContext(ctx -> ctx.put("test", "foo")) .subscriberContext(ctx -> ctx.put("test2", "bar")) .log()) .expectNext("barfoo4") .expectNext("barfoo5") .expectNext("barfoo6") .verifyComplete(); }
@Test public void contextGet() throws InterruptedException { StepVerifier.create(Flux.range(1, 1000) .log() .handle((d, c) -> c.next(c.currentContext().get("test") + "" + d)) .skip(3) .take(3) .handle((d, c) -> c.next(c.currentContext().get("test2") + "" + d)) .subscriberContext(ctx -> ctx.put("test", "foo")) .subscriberContext(ctx -> ctx.put("test2", "bar")) .log()) .expectNext("barfoo4") .expectNext("barfoo5") .expectNext("barfoo6") .verifyComplete(); }