2017-08-24 3 views
0

Ich versuche Reactor virtuellen Zeit Funktion, aber die Testblöcke auf unbestimmte Zeit (ohne Timeout) oder wirft ein AssertionError (mit Timeout) zu verwenden:Reactor StepVerifier.withVirtualTime Blöcke unbegrenzt

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

Die Ausnahme ist:

java.lang.AssertionError: VerifySubscribertimed out on [email protected] 

Das gleiche Beispiel mit Echtzeit funktioniert wie erwartet:

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

Ich kann keinen Fehler in meinem ersten Beispiel nach Manipulating Time von der Referenz sehen.

Was ist los?

Antwort

1

Sie müssen .thenAwait(Duration) verwenden, sonst bewegt sich die (virtuelle) Uhr nicht, und die Verzögerung tritt nie auf. Sie können auch .expectNoEvent(Duration)nach die expectSubscription() verwenden.

Zum Beispiel:

@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)); 
}