代码之家  ›  专栏  ›  技术社区  ›  Frischling

步进验证程序预期0毫秒内没有事件不工作(要检查特殊情况下是否没有延迟)

  •  0
  • Frischling  · 技术社区  · 8 年前

    我必须为每个客户创建一个backpressure,并通过返回 Mono.justOrEmpty(authentication).delayElement(Duration.ofMillis(calculatedDelay));

    在我的单元测试中,这似乎工作得很好,但是如果我的计算时间是0,我就不能测试这个。此代码段返回 java.lang.AssertionError: unexpected end during a no-event expectation :

    @Test
    public void testSomeDelay_8CPUs_Load7() throws IOException {
      when(cut.getLoad()).thenReturn("7");
      when(cut.getCpuCount()).thenReturn(8L);
      when(authentication.getName()).thenReturn("delayeduser");
    
      Duration duration = StepVerifier
      .withVirtualTime(() -> cut.get(Optional.of(authentication))) 
      .expectSubscription()  // swallow subscribe event
      .expectNoEvent(Duration.ofMillis(0)) // here is the culprit
      .expectNextCount(1) 
      .verifyComplete(); 
    }
    

    我不知道如何检查这些案子,我被认为根本不会耽搁。顺便说一句,当我返回Mono.justOrEmpty(身份验证)(没有任何延迟订阅)时,也是如此。我似乎无法检查,我是否创建了正确的非延迟流。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Simon Baslé    8 年前

    是的,这是一个很难掩盖的角落案件。尤其是当你知道 foo.delayElement(0) 实际上只是回来 foo 未经修改。

    你可以做的是用不同的方法测试延迟,在 elapsed() 操作员:

    Duration duration = StepVerifier
      .withVirtualTime(() -> cut.get(Optional.of(authentication))
         .elapsed() //this will get correct timing of 0 with virtual time
         .map(Tuple2::getT1) //we only care about the timing, T2 being the value
      ) 
      .expectNext(calculateDelay)
      .verifyComplete();