/** * Propagate a {@link TimeoutException} as soon as no item is emitted within the * given {@link Duration} from the previous emission (or the subscription for the first * item), as measured by the specified {@link Scheduler}. * * <p> * <img class="marble" src="doc-files/marbles/timeoutForFlux.svg" alt=""> * * @param timeout the timeout {@link Duration} between two signals from this {@link Flux} * @param timer a time-capable {@link Scheduler} instance to run on * * @return a {@link Flux} that can time out on a per-item basis */ public final Flux<T> timeout(Duration timeout, Scheduler timer) { return timeout(timeout, null, timer); }
/** * Propagate a {@link TimeoutException} as soon as no item is emitted within the * given {@link Duration} from the previous emission (or the subscription for the first item). * * <p> * <img class="marble" src="doc-files/marbles/timeoutForFlux.svg" alt=""> * * @param timeout the timeout between two signals from this {@link Flux} * * @return a {@link Flux} that can time out on a per-item basis */ public final Flux<T> timeout(Duration timeout) { return timeout(timeout, null, Schedulers.parallel()); }
Flux<?> scenario_timeoutThrown() { return Flux.never() .timeout(Duration.ofMillis(500)); }
Flux<?> scenario_timeoutThrown2() { return Flux.never() .timeout(Duration.ofMillis(500)); }
Flux<?> scenario_timeoutThrown3() { return Flux.never() .timeout(Duration.ofMillis(500), Schedulers.parallel()); }
Flux<Integer> scenario_timeoutCanBeBoundWithCallback() { return Flux.<Integer>never().timeout(Duration.ofMillis(500), Flux.just(-5)); }
Flux<Integer> scenario_timeoutCanBeBoundWithCallback2() { return Flux.<Integer>never().timeout(Duration.ofMillis(500), Flux.just(-5)); }
@Test public void timeoutNotDurationMessageFirstTimeout() { StepVerifier.create(Flux.never() .timeout(Mono.just("immediate"))) .expectErrorMessage("Did not observe any item or terminal signal within " + "first signal from a Publisher in 'source(FluxNever)' (and no fallback has been configured)") .verify(); }
@Test public void fluxTimeoutOther() { StepVerifier.create(Flux.never().timeout(Flux.just(1))) .thenAwait(Duration.ofMillis(500)) .verifyError(TimeoutException.class); }
@Test public void itemTimeoutThrows() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.never(), v -> { throw new RuntimeException("forced failure"); }) .subscribe(ts); ts.assertValues(1) .assertNotComplete() .assertError(RuntimeException.class) .assertErrorMessage("forced failure"); }
@Test public void firstTimeoutError() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.error(new RuntimeException("forced " + "failure")), v -> Flux.never()) .subscribe(ts); ts.assertNoValues() .assertNotComplete() .assertError(RuntimeException.class) .assertErrorMessage("forced failure"); }
@Test public void itemTimeoutError() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.never(), v -> Flux.error(new RuntimeException("forced failure"))) .subscribe(ts); ts.assertValues(1) .assertNotComplete() .assertError(RuntimeException.class) .assertErrorMessage("forced failure"); }
@Test public void timeoutDurationMessageDefault() { StepVerifier.withVirtualTime(() -> Flux.never() .timeout(Duration.ofHours(1))) .thenAwait(Duration.ofHours(2)) .expectErrorMessage("Did not observe any item or terminal signal within " + "3600000ms in 'source(FluxNever)' (and no fallback has been configured)") .verify(); }
@Test public void itemTimeoutReturnsNull() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.never(), v -> null) .subscribe(ts); ts.assertValues(1) .assertNotComplete() .assertError(NullPointerException.class); }
@Test public void noTimeout() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.never(), v -> Flux.never()) .subscribe(ts); ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .assertComplete() .assertNoError(); }
@Test public void immediateTimeout() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.empty(), v -> Flux.never()) .subscribe(ts); ts.assertNoValues() .assertNotComplete() .assertError(TimeoutException.class); }
public void immediateTimeoutResume() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.empty(), v -> Flux.never(), Flux.range(1, 10)) .subscribe(ts); ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .assertComplete() .assertNoError(); }
@Test public void timeoutDurationMessageWithName() { StepVerifier.withVirtualTime(() -> Flux.never() .name("Name") .timeout(Duration.ofHours(1))) .thenAwait(Duration.ofHours(2)) .expectErrorMessage("Did not observe any item or terminal signal within " + "3600000ms in 'Name' (and no fallback has been configured)") .verify(); }
@Test public void firstElemenetImmediateTimeout() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.never(), v -> Flux.empty()) .subscribe(ts); ts.assertValues(1) .assertNotComplete() .assertError(TimeoutException.class); }
@Test public void firstElemenetImmediateResume() { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .timeout(Flux.never(), v -> Flux.empty(), Flux.range(1, 10)) .subscribe(ts); ts.assertValues(1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .assertComplete() .assertNoError(); }