/** * Test a {@link Decoder#decodeToMono decode} scenario where the input stream is canceled. * This test method will immediately cancel the output stream. * * @param input the input to be provided to the decoder * @param outputType the desired output type * @param mimeType the mime type to use for decoding. May be {@code null}. * @param hints the hints used for decoding. May be {@code null}. */ protected void testDecodeToMonoCancel(Publisher<DataBuffer> input, ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Mono<?> result = this.decoder.decodeToMono(input, outputType, mimeType, hints); StepVerifier.create(result) .thenCancel() .verify(); }
@Test public void cancelsOther() { AtomicBoolean cancelled = new AtomicBoolean(); Flux<Integer> when = Flux.range(1, 10) .doOnCancel(() -> cancelled.set(true)); StepVerifier.create(justError.retryWhen(other -> when)) .thenCancel() .verify(); assertThat(cancelled.get()).isTrue(); }
@Test public void cancelsOther() { AtomicBoolean cancelled = new AtomicBoolean(); Flux<Integer> when = Flux.range(1, 10) .doOnCancel(() -> cancelled.set(true)); StepVerifier.create(Flux.just(1).repeatWhen(other -> when)) .thenCancel() .verify(); assertThat(cancelled.get()).isTrue(); }
.andThen(this::conditional) .apply(withFluxSource(up)), 0) .thenCancel() .verify(); .andThen(this::conditional) .apply(withFluxSource(up2)), 0) .thenCancel() .verify();
@Test public void shouldBeAbleToCatchDiscardedElement() { TestPublisher<Integer> publisher = TestPublisher.createCold(); Integer[] discarded = new Integer[1]; Flux<String> switchTransformed = publisher.flux() .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf)) .doOnDiscard(Integer.class, e -> discarded[0] = e); publisher.next(1); StepVerifier.create(switchTransformed, 0) .thenCancel() .verify(Duration.ofSeconds(10)); publisher.assertCancelled(); publisher.assertWasRequested(); Assertions.assertThat(discarded).containsExactly(1); }
@Test //FIXME use Violation.NO_CLEANUP_ON_TERMINATE public void failDoubleNext() { Hooks.onNextDropped(c -> { }); StepVerifier.create(Flux.zip(obj -> 0, Flux.just(1), Flux.just(2), s -> { s.onSubscribe(Operators.emptySubscription()); s.onNext(2); s.onNext(3); })) .thenCancel() .verify(); Hooks.resetOnNextDropped(); }
@Test public void shouldBeAbleToCatchDiscardedElementInCaseOfConditional() { TestPublisher<Integer> publisher = TestPublisher.createCold(); Integer[] discarded = new Integer[1]; Flux<String> switchTransformed = publisher.flux() .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf)) .filter(t -> true) .doOnDiscard(Integer.class, e -> discarded[0] = e); publisher.next(1); StepVerifier.create(switchTransformed, 0) .thenCancel() .verify(Duration.ofSeconds(10)); publisher.assertCancelled(); publisher.assertWasRequested(); Assertions.assertThat(discarded).contains(1); }
final void inputHiddenOutputConditionalCancel(OperatorScenario<I, PI, O, PO> scenario) { StepVerifier.create(scenario.body() .andThen(this::conditional) .apply(anySourceHidden(scenario))) .thenCancel() //hit double cancel .verify(); }
@Test public void cancel() { final EmitterProcessor<Boolean> pp = EmitterProcessor.create(); StepVerifier.create(Mono.just(1) .filterWhen(v -> pp)) .thenCancel(); assertThat(pp.hasDownstreams()).isFalse(); }
@Test public void discardOnCancel() { StepVerifier.create(Flux.just(1, 2, 3) .concatMap(i -> Mono.just("value" + i), 1), 0) .thenCancel() .verifyThenAssertThat() .hasDiscardedExactly(1, 2, 3); }
@Test public void ignoreConcurrentHiddenComplete() { StepVerifier.create(Flux.from(s -> { s.onSubscribe(Operators.emptySubscription()); s.onNext(1); }).flatMap(f -> Flux.just(f).hide())) .thenCancel() .verify(); }
@Test public void completePublisherDelayCancel() { StepVerifier.create(Flux.just(1, 2, 3) .flatMapDelayError(d -> Flux.<Integer>empty().hide(), 1, 1)) .thenCancel() .verify(); }
@Test public void never() { new FluxMergeOrdered<Integer>(2, Queues.small(), Comparator.naturalOrder(), Flux.never(), Flux.never()) .as(StepVerifier::create) .thenCancel() .verify(); }
@Test public void takeConditionalCancel() { StepVerifier.create(Flux.just("test", "test2", "test3") .hide() .take(3) .filter("test2"::equals), 2) .thenCancel() .verify(); }
@Test public void discardDelayedOnCancel() { StepVerifier.create(Flux.just(1, 2, 3) .concatMapDelayError(i -> Mono.just("value" + i), 1), 0) .thenCancel() .verifyThenAssertThat() .hasDiscardedExactly(1, 2, 3); }
@Test public void shouldBeAbleToBeCancelledProperly() { TestPublisher<Integer> publisher = TestPublisher.createCold(); Flux<String> switchTransformed = publisher.flux() .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf)); publisher.next(1); StepVerifier.create(switchTransformed, 0) .thenCancel() .verify(Duration.ofSeconds(10)); publisher.assertCancelled(); publisher.assertWasRequested(); }
@Test public void supplierCancel(){ StepVerifier.create(Mono.fromSupplier(() -> "test")) .thenCancel() .verify(); }
@Test public void cancel() { final EmitterProcessor<Boolean> pp = EmitterProcessor.create(); StepVerifier.create(Flux.range(1, 5) .filterWhen(v -> pp, 16)) .thenCancel(); assertThat(pp.hasDownstreams()).isFalse(); }
@Test public void verifyVirtualTimeNoEventNeverError() { assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> StepVerifier.withVirtualTime(() -> Mono.never() .log()) .expectNoEvent(Duration.ofDays(10000)) .thenCancel() .verify()) .withMessageStartingWith("expectation failed (expected no event: onSubscribe("); }
@Test public void joinCanceled() { Flux<DataBuffer> source = Flux.concat( deferStringBuffer("foo"), deferStringBuffer("bar"), deferStringBuffer("baz") ); Mono<DataBuffer> result = DataBufferUtils.join(source); StepVerifier.create(result) .thenCancel() .verify(); }