Search code examples
javaproject-reactorflux

Unit test indefinite repeat Flux


Given a Flux which repeats itself forever, how can we perform Unit test on this? Say I want to assert the first set of elements [1,2,3,4,5]

 StepVerifier.
                withVirtualTime(() -> Flux.just(1,2,3,4,5).repeatWhen(c -> c.delayElements(Duration.ofSeconds(1)).log()))
                .thenAwait(Duration.ofSeconds(1))
                .expectNext(1)
                .thenAwait(Duration.ofSeconds(4))
                .expectNext(2,3,4,5)

                .thenConsumeWhile(x -> true)   // what is the right behaviour to be added here ? 
    ```

Solution

  • for an "infinite" flux, your only choice is probably thenCancel().verify()