Java Reactor StepVerifier.withVirtualTime循环:使用“重复检查”;expectNoEvent()”&引用;expectNext();及;然后等待();
我正在学习Java11反应堆。我见过这个例子:Java Reactor StepVerifier.withVirtualTime循环:使用“重复检查”;expectNoEvent()”&引用;expectNext();及;然后等待();,java,reactor,Java,Reactor,我正在学习Java11反应堆。我见过这个例子: StepVerifier.withVirtualTime(() -> Flux.interval(Duration.ofSeconds(1)).take(3600)) .expectSubscription() .expectNextCount(3600); 本例仅检查通量每秒递增1,直到1小时,最终计数为3600 但是,有没有办法在每秒钟后重复检查计数器 我知道这一点: .expec
StepVerifier.withVirtualTime(() -> Flux.interval(Duration.ofSeconds(1)).take(3600))
.expectSubscription()
.expectNextCount(3600);
本例仅检查通量每秒递增1,直到1小时,最终计数为3600
但是,有没有办法在每秒钟后重复检查计数器
我知道这一点:
.expectNoEvent(Duration.ofSeconds(1))
.expectNext(0L)
.thenAwait(Duration.ofSeconds(1))
但我没有办法在每秒钟后反复检查,比如:
.expectNoEvent(Duration.ofSeconds(1))
.expectNext(i)
.thenAwait(Duration.ofSeconds(1))
当i
增加到3600时。有吗
附言:
在一个长时间运行的测试中,我终于尝试添加了verifyComplete()
,它永远不会结束。我必须这样做吗?或者干脆忽略它?您可以通过使用expectNextSequence
实现您想要的。您必须传递一个Iterable
,并包含预期到达的每个元素。请参见下面的示例:
var longRange = LongStream.range(0, 3600)
.boxed()
.collect(Collectors.toList());
StepVerifier
.withVirtualTime(() -> Flux.interval(Duration.ofSeconds(1)).take(3600))
.expectSubscription()
.thenAwait(Duration.ofHours(1))
.expectNextSequence(longRange)
.expectComplete().verify();
如果您没有添加verifyComplete()
或expectComplete().verify()
,那么JUnit测试将不会等到元素从通量中到达并终止
有关更多参考信息,请参阅verify()
的JavaDoc:
此方法将阻止,直到流终止