0ms 的 StepVerifier expectNoEvent 不起作用(想检查特殊情况下是否没有延迟)

StepVerifier expectNoEvent for 0ms does not work (want to check for no delay for special cases)

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

来实现

这在我的单元测试中似乎工作正常,但如果我的 calculatedDelay 为 0,我将无法测试它。此代码段 return 是 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(); 
}

我不知道如何检查我应该不会有任何延迟的情况。顺便说一句,当我 return a Mono.justOrEmpty(authentication) (没有任何延迟订阅)时,这是相同的。我似乎无法检查我是否创建了正确的非延迟流程。

是的,这是一个难以涵盖的极端情况。尤其是当您知道 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();