使用 StepVerifier 测试 Mono.delay

Testing Mono.delay with StepVerifier

我正在尝试使用 StepVerifier 测试 Mono.delay。 我的代码如下:

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

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

我得到以下堆栈跟踪

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)

跟踪源代码后,执行程序似乎在方法 Schedulers.setFactory

上关闭

我想知道我是否遗漏了什么。

好的,我想我已经找到问题的根源了。

StepVerifier 需要 Supplier,在我的例子中,我为 Mono[Long] 提供了已创建的现有 Mono。 我应该改为执行以下操作

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

这样提供的Mono不会先被创建,执行器也不会被关闭。