0

When combining many Mono<Void>s using .then(Mono<Void>) they don't run in the expected order.

Can somebody explain the difference between the working and nonworking code below?

Working code

StepVerifier.create(
        repository.incrementCounter(bucket, timeStamp)
                .then(repository.incrementCounter(bucket, timeStamp))
                .then(Mono.just(1).flatMap(t -> repository.resetCounter(bucket, timeStamp)))
                .then(Mono.just(1).flatMap(t -> repository.getCounter(bucket, timeStamp))))
        .expectNext(0L)
        .verifyComplete();

non-working

StepVerifier.create(
        repository.incrementCounter(bucket, timeStamp)
                .then(repository.incrementCounter(bucket, timeStamp))
                .then(repository.resetCounter(bucket, timeStamp))
                .then(repository.getCounter(bucket, timeStamp)))
        .expectNext(0L)
        .verifyComplete();
Hemant
  • 33
  • 6

1 Answers1

1

As per Gitter channel, your repository should return cold Monos (aka "lazy"), but you're most probably starting the call even before returning it.

You can use Mono.defer to make it cold.

bsideup
  • 2,845
  • 17
  • 21