У меня есть метод, который вызывает клиент REST и повторяет попытки в случае сбоя.
Код: Выделить всё
class MyService {
private MyRestClient restClient;
public Uni fetchData() {
return restClient.fetchData()
.onFailure().retry().withBackOff(Duration.ofMillis(100), Duration.ofSeconds(5))
.atMost(5);
}
}
Код: Выделить всё
@Test
public void shouldRetry() {
Mockito.when(restClient.fetchData())
.thenReturn(
Uni.createFrom().failure(new RuntimeException("Failed to fetch data")),
Uni.createFrom().item(Response.ok().build())
);
assertDoesNotThrow(() -> myService.fetchData().await().indefinetely());
Mockito.verify(restClient, Mockito.times(2)).fetchData();
}
Он продолжает повторять попытки до пятой попытки, когда выдает IllegalStateException:
Код: Выделить всё
Suppressed: java.lang.IllegalStateException: Retries exhausted: 5/5
Подробнее здесь: https://stackoverflow.com/questions/770 ... -on-an-uni