|
22 | 22 | import org.mockito.ArgumentCaptor;
|
23 | 23 | import reactor.core.publisher.Flux;
|
24 | 24 | import reactor.test.StepVerifier;
|
25 |
| -import reactor.util.function.Tuple2; |
26 | 25 |
|
27 |
| -import java.time.Duration; |
28 | 26 | import java.util.ArrayList;
|
29 | 27 | import java.util.List;
|
30 | 28 | import java.util.concurrent.CompletionStage;
|
31 | 29 | import java.util.function.Supplier;
|
32 | 30 |
|
33 |
| -import org.neo4j.driver.internal.util.Clock; |
34 |
| -import org.neo4j.driver.internal.util.ImmediateSchedulingEventExecutor; |
35 | 31 | import org.neo4j.driver.Logger;
|
36 | 32 | import org.neo4j.driver.Logging;
|
37 | 33 | import org.neo4j.driver.exceptions.ServiceUnavailableException;
|
38 | 34 | import org.neo4j.driver.exceptions.SessionExpiredException;
|
39 | 35 | import org.neo4j.driver.exceptions.TransientException;
|
| 36 | +import org.neo4j.driver.internal.util.Clock; |
| 37 | +import org.neo4j.driver.internal.util.ImmediateSchedulingEventExecutor; |
40 | 38 |
|
41 | 39 | import static java.lang.Long.MAX_VALUE;
|
42 | 40 | import static java.util.concurrent.CompletableFuture.completedFuture;
|
@@ -751,53 +749,44 @@ public CompletionStage<Void> get()
|
751 | 749 | }
|
752 | 750 |
|
753 | 751 | @Test
|
754 |
| - void shouldRetryWithBackOff() { |
| 752 | + void shouldRetryWithBackOff() |
| 753 | + { |
755 | 754 | Exception exception = new TransientException( "Unknown", "Retry this error." );
|
756 |
| - List<Long> elapsedList = new ArrayList<>(); |
757 |
| - |
758 | 755 | ExponentialBackoffRetryLogic retryLogic = new ExponentialBackoffRetryLogic( 1000, 100, 2, 0,
|
759 |
| - null, Clock.SYSTEM, DEV_NULL_LOGGING ); |
| 756 | + eventExecutor, Clock.SYSTEM, DEV_NULL_LOGGING ); |
760 | 757 |
|
761 | 758 | Flux<Integer> source = Flux.concat( Flux.range( 0, 2 ), Flux.error( exception ) );
|
762 |
| - StepVerifier.withVirtualTime( () -> // This test uses a virtual time. So do not panic if you saw this test runs faster than it should be. |
763 |
| - Flux.from( retryLogic.retryRx( source ) ) |
764 |
| - .elapsed() |
765 |
| - .doOnNext( elapsed -> { if ( elapsed.getT2() == 0 ) elapsedList.add( elapsed.getT1() ); } ) |
766 |
| - .map( Tuple2::getT2 ) ) |
767 |
| - .thenAwait( Duration.ofSeconds( 2 ) ) |
| 759 | + Flux<Integer> retriedSource = Flux.from( retryLogic.retryRx( source ) ); |
| 760 | + StepVerifier.create( retriedSource ) |
768 | 761 | .expectNext( 0, 1 ) // first run
|
769 | 762 | .expectNext( 0, 1, 0, 1, 0, 1, 0, 1 ) //4 retry attempts
|
770 | 763 | .verifyErrorSatisfies( e -> assertThat( e, equalTo( exception ) ) );
|
771 | 764 |
|
772 |
| - assertThat( elapsedList.size(), equalTo( 5 ) ); |
773 |
| - assertThat( elapsedList, contains( 0L, 100L, 200L, 400L, 800L ) ); |
| 765 | + List<Long> delays = eventExecutor.scheduleDelays(); |
| 766 | + assertThat( delays.size(), equalTo( 4 ) ); |
| 767 | + assertThat( delays, contains( 100L, 200L, 400L, 800L ) ); |
774 | 768 | }
|
775 | 769 |
|
776 | 770 | @Test
|
777 |
| - void shouldRetryWithRandomBackOff() { |
778 |
| - List<Long> elapsedList = new ArrayList<>(); |
| 771 | + void shouldRetryWithRandomBackOff() |
| 772 | + { |
779 | 773 | Exception exception = new TransientException( "Unknown", "Retry this error." );
|
780 |
| - |
781 | 774 | ExponentialBackoffRetryLogic retryLogic = new ExponentialBackoffRetryLogic( 1000, 100, 2, 0.1,
|
782 |
| - null, Clock.SYSTEM, DEV_NULL_LOGGING ); |
| 775 | + eventExecutor, Clock.SYSTEM, DEV_NULL_LOGGING ); |
783 | 776 |
|
784 | 777 | Flux<Integer> source = Flux.concat( Flux.range( 0, 2 ), Flux.error( exception ) );
|
785 |
| - StepVerifier.withVirtualTime( () -> // This test uses a virtual time. So do not panic if you saw this test runs faster than it should be. |
786 |
| - Flux.from( retryLogic.retryRx( source ) ) |
787 |
| - .elapsed() |
788 |
| - .doOnNext( elapsed -> { if ( elapsed.getT2() == 0 ) elapsedList.add( elapsed.getT1() ); } ) |
789 |
| - .map( Tuple2::getT2 ) ) |
790 |
| - .thenAwait( Duration.ofSeconds( 2 ) ) |
| 778 | + Flux<Integer> retriedSource = Flux.from( retryLogic.retryRx( source ) ); |
| 779 | + StepVerifier.create( retriedSource ) |
791 | 780 | .expectNext( 0, 1 ) // first run
|
792 | 781 | .expectNext( 0, 1, 0, 1, 0, 1, 0, 1 ) // 4 retry attempts
|
793 | 782 | .verifyErrorSatisfies( e -> assertThat( e, equalTo( exception ) ) );
|
794 | 783 |
|
795 |
| - assertThat( elapsedList.size(), equalTo( 5 ) ); |
796 |
| - assertThat( elapsedList.get( 0 ), equalTo( 0L ) ); |
797 |
| - assertThat( elapsedList.get( 1 ), allOf( greaterThanOrEqualTo( 90L ), lessThanOrEqualTo( 110L ) ) ); |
798 |
| - assertThat( elapsedList.get( 2 ), allOf( greaterThanOrEqualTo( 180L ), lessThanOrEqualTo( 220L ) ) ); |
799 |
| - assertThat( elapsedList.get( 3 ), allOf( greaterThanOrEqualTo( 260L ), lessThanOrEqualTo( 440L ) ) ); |
800 |
| - assertThat( elapsedList.get( 4 ), allOf( greaterThanOrEqualTo( 720L ), lessThanOrEqualTo( 880L ) ) ); |
| 784 | + List<Long> delays = eventExecutor.scheduleDelays(); |
| 785 | + assertThat( delays.size(), equalTo( 4 ) ); |
| 786 | + assertThat( delays.get( 0 ), allOf( greaterThanOrEqualTo( 90L ), lessThanOrEqualTo( 110L ) ) ); |
| 787 | + assertThat( delays.get( 1 ), allOf( greaterThanOrEqualTo( 180L ), lessThanOrEqualTo( 220L ) ) ); |
| 788 | + assertThat( delays.get( 2 ), allOf( greaterThanOrEqualTo( 260L ), lessThanOrEqualTo( 440L ) ) ); |
| 789 | + assertThat( delays.get( 3 ), allOf( greaterThanOrEqualTo( 720L ), lessThanOrEqualTo( 880L ) ) ); |
801 | 790 | }
|
802 | 791 |
|
803 | 792 | private static void retry( ExponentialBackoffRetryLogic retryLogic, final int times )
|
|
0 commit comments