【问题标题】:Reactor StepVerifier.withVirtualTime blocks indefinitelyReactor StepVerifier.withVirtualTime 无限期阻塞
【发布时间】:2017-08-24 16:05:57
【问题描述】:

我正在尝试使用 Reactor 的虚拟时间功能​​,但测试会无限期阻塞(无超时)或抛出 AssertionError(有超时):

@Test
public void test() {
    StepVerifier.withVirtualTime(() -> 
            Flux.just(1, 2, 3, 4).delayElements(Duration.ofSeconds(1)))
            .expectSubscription()
            .expectNextCount(4)
            .expectComplete()
            .verify(Duration.ofSeconds(10));
}

例外是:

java.lang.AssertionError: VerifySubscribertimed out on reactor.core.publisher.FluxConcatMap$ConcatMapImmediate@66d1af89

与实时相同的示例按预期工作:

@Test
public void test2() {
    StepVerifier.create(Flux.just(1, 2, 3, 4).delayElements(Duration.ofSeconds(1)))
            .expectSubscription()
            .expectNextCount(4)
            .expectComplete()
            .verify(Duration.ofSeconds(10));
}

我在参考Manipulating Time 之后的第一个示例中看不到错误。

怎么了?

【问题讨论】:

    标签: java project-reactor


    【解决方案1】:

    您需要使用.thenAwait(Duration),否则(虚拟)时钟不会移动,延迟永远不会发生。你也可以在expectSubscription()之后使用.expectNoEvent(Duration)

    例如:

    @Test
    public void test() {
      StepVerifier.withVirtualTime(() -> 
            Flux.just(1, 2, 3, 4).delayElements(Duration.ofSeconds(1)))
            .expectSubscription() //t == 0
    //move the clock forward by 1s, and check nothing is emitted in the meantime
            .expectNoEvent(Duration.ofSeconds(1))
    //so this effectively verifies the first value is delayed by 1s:
            .expectNext(1)
    //and so on...
            .expectNoEvent(Duration.ofSeconds(1))
            .expectNext(2)
    //or move the clock forward by 2s, allowing events to take place,
    //and check last 2 values where delayed
            .thenAwait(Duration.ofSeconds(2))
            .expectNext(3, 4)
            .expectComplete()
    //trigger the verification and check that in realtime it ran in under 200ms
            .verify(Duration.ofMillis(200));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 2020-10-28
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2018-03-27
      相关资源
      最近更新 更多