use of dev.failsafe.Fallback in project failsafe by jhalterman.
the class DelayableRetryPolicyTest method shouldDelayOnMatchingFailureType.
public void shouldDelayOnMatchingFailureType() {
AtomicInteger delays = new AtomicInteger(0);
RetryPolicy<Integer> retryPolicy = RetryPolicy.<Integer>builder().handle(UncheckedExpectedException.class).withMaxRetries(4).withDelayFnOn(ctx -> {
// side-effect for test purposes
delays.incrementAndGet();
return Duration.ofNanos(1);
}, DelayException.class).build();
AtomicInteger attempts = new AtomicInteger(0);
int result = Failsafe.with(Fallback.of(123), retryPolicy).get(() -> {
int i = attempts.getAndIncrement();
switch(i) {
case 0:
case 2:
throw new DelayException();
default:
throw new UncheckedExpectedException();
}
});
assertEquals(result, 123, "Fallback should be used");
assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
assertEquals(delays.get(), 2, "Expecting two dynamic delays matching DelayException failure");
}
use of dev.failsafe.Fallback in project failsafe by jhalterman.
the class DelayableRetryPolicyTest method shouldDelayOnMatchingResult.
public void shouldDelayOnMatchingResult() {
AtomicInteger delays = new AtomicInteger(0);
RetryPolicy<Object> retryPolicy = RetryPolicy.builder().handleResultIf(result -> true).withMaxRetries(4).withDelayFnWhen(ctx -> {
// side-effect for test purposes
delays.incrementAndGet();
return Duration.ofNanos(1);
}, "expected").build();
Fallback<Object> fallback = Fallback.<Object>builder(123).handleResultIf(result -> true).build();
AtomicInteger attempts = new AtomicInteger(0);
Object result = Failsafe.with(fallback, retryPolicy).get(() -> {
int i = attempts.getAndIncrement();
switch(i) {
case 0:
case 3:
return "expected";
default:
return i;
}
});
assertEquals(result, 123, "Fallback should be used");
assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
assertEquals(delays.get(), 2, "Expecting two dynamic delays matching String result");
}
Aggregations