2017-08-24 8 views
0

私はReactorの仮想時間機能を使用しようとしているが、テストブロック無期限(タイムアウトなし)または(タイムアウト付き)AssertionErrorをスローしています:原子炉StepVerifier.withVirtualTimeブロック無期限

@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 [email protected] 

リアルタイムでの同じ例は期待通りに動作します:

@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の後にエラーを見ることができません。

どうしたのですか?

答えて

1

.thenAwait(Duration)を使用する必要があります。そうしないと、(仮想)時計は移動せず、遅延は発生しません。 .expectNoEvent(Duration)の後にの後にexpectSubscription()を使用することもできます。例えば

@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.ofMilliseconds(200)); 
} 
関連する問題