Testing Mono.delay with StepVerifier

3.2k views Asked by At

I'm trying to test Mono.delay using StepVerifier. My code is as follow:

Mono[Long] mono = Mono.delay(Duration.ofDays(5));

StepVerifier.withVirtualTime(() -> mono)
    .thenAwait(Duration.ofDays(5))
    .expectNextCount(1)
    .expectComplete()
    .verify();

And I'm getting the following stacktrace

expectation "expectNextCount" failed (expected: count = 1; actual: counted = 0; signal: onError(reactor.core.Exceptions$BubblingException:
java.util.concurrent.RejectedExecutionException: Scheduler unavailable))
java.lang.AssertionError: expectation "expectNextCount" failed (expected: count = 1; actual: counted = 0; signal: onError(reactor.core.Exceptions$BubblingException: java.util.concurrent.RejectedExecutionException: Scheduler unavailable))
at reactor.test.DefaultStepVerifierBuilder.failPrefix(DefaultStepVerifierBuilder.java:1679)
at reactor.test.DefaultStepVerifierBuilder.fail(DefaultStepVerifierBuilder.java:1675)
at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.checkCountMismatch(DefaultStepVerifierBuilder.java:886)
at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.onSignalCount(DefaultStepVerifierBuilder.java:1112)
at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.onExpectation(DefaultStepVerifierBuilder.java:963)
at reactor.test.DefaultStepVerifierBuilder$DefaultVerifySubscriber.onError(DefaultStepVerifierBuilder.java:727)
at reactor.core.publisher.MonoDelay.subscribe(MonoDelay.java:59)
at reactor.test.DefaultStepVerifierBuilder$DefaultStepVerifier.verify(DefaultStepVerifierBuilder.java:532)
at reactor.test.DefaultStepVerifierBuilder$DefaultStepVerifier.verify(DefaultStepVerifierBuilder.java:508)

After tracing the source code, it seems that the executors are shutdown on the method Schedulers.setFactory

I'm wondering if I'm missing something here.

1

There are 1 answers

1
Wins On BEST ANSWER

Ok, I think I'm getting to the bottom of the issue.

The StepVerifier requires Supplier and in my case I provided the Mono[Long] with the existing Mono which has been created. I should instead doing the following

StepVerifier.withVirtualTime(() -> Mono.delay(Duration.ofDays(5)))
    .thenAwait(Duration.ofDays(5))
    .expectNextCount(1)
    .expectComplete()
    .verify();

This way the Mono provided will not be created first and the executor will not be shutdown.