4

I have a simple implementation of WebClient request:

public Mono<Todo> getTodoWithSimpleRetry(String id) {
    RetryBackoffSpec retrySpec = Retry.fixedDelay(3, Duration.ofSeconds(2))
            .doBeforeRetry(beforeRetry -> {
                log.info("Attempt to retry: " + (beforeRetry.totalRetries() + 1));
            }).doAfterRetry(afterRetry -> {
                log.error("After retry {}", afterRetry.failure().getMessage());
            })
            .onRetryExhaustedThrow(((retrySpec1, retrySignal) -> {
                throw new TodoServiceException("Max retry exceeded. Todo Service is not available", HttpStatus.SERVICE_UNAVAILABLE.value());
            }));

    return webClient.get()
            .uri("/todo/{id}", id)
            .retrieve()
            .bodyToMono(Todo.class)
            .retryWhen(retrySpec)
            ;
}

and I'm trying to test that exception TodoServiceException will be thrown after all retry attempts but I don't want to wait time between all retries. I found that it could be done through StepVerifier.withVirtualTime. And my example below:

@Test
void testRetry() {
    mockBackEnd.enqueue(new MockResponse().setResponseCode(500));
    mockBackEnd.enqueue(new MockResponse().setResponseCode(500));
    mockBackEnd.enqueue(new MockResponse().setResponseCode(500));
    mockBackEnd.enqueue(new MockResponse().setResponseCode(500));
    
    StepVerifier.withVirtualTime(() -> todoWebClient.getTodoWithSimpleRetry("1"))
            .expectSubscription()
            .thenAwait(Duration.ofSeconds(2))
            .thenAwait(Duration.ofSeconds(2))
            .thenAwait(Duration.ofSeconds(2))
            .expectError(TodoServiceException.class)
            .verify();
}

But the problem is that test always stuck on the second retry and nothing happens.

ExcepOra
  • 117
  • 9
  • Does it work if you add one more `thenAwait`? It is possible there is some additional delay somewhere in the flow which needs to be waited for. – Martin Tarjányi Jul 13 '21 at 20:35
  • No, tried `thenAwait(), thenAwait(Duration.ofSeconds(2)) and thenAwait(Duration.ofSeconds(30))` nothing happens – ExcepOra Jul 14 '21 at 06:02

0 Answers0