use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue190Test method test.
public void test() throws Throwable {
RetryPolicy<Object> policy = RetryPolicy.builder().withMaxRetries(5).build();
AtomicInteger failureEvents = new AtomicInteger();
AtomicInteger successEvents = new AtomicInteger();
Waiter waiter = new Waiter();
Failsafe.with(policy).onFailure(e -> {
failureEvents.incrementAndGet();
waiter.resume();
}).onSuccess(e -> {
successEvents.incrementAndGet();
waiter.resume();
}).getAsyncExecution(execution -> Testing.futureResult(executor, true).whenComplete((result, failure) -> {
execution.recordResult(result);
})).get();
waiter.await(1000);
Assert.assertEquals(failureEvents.get(), 0);
Assert.assertEquals(successEvents.get(), 1);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36Test method retryListener_WithExceptions_ShouldBeCalled.
@Test
public void retryListener_WithExceptions_ShouldBeCalled() {
AtomicInteger listenerCallbacks = new AtomicInteger();
RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onRetry(e -> listenerCallbacks.incrementAndGet()).build();
Testing.ignoreExceptions(() -> Failsafe.with(policy).get(() -> {
throw new RuntimeException();
}));
assertEquals(listenerCallbacks.get(), 3);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36Test method retryListener_WithFailedResponses_ShouldBeCalled.
@Test
public void retryListener_WithFailedResponses_ShouldBeCalled() {
AtomicInteger listenerCallbacks = new AtomicInteger();
RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onRetry(e -> listenerCallbacks.incrementAndGet()).build();
Failsafe.with(policy).get(() -> false);
assertEquals(listenerCallbacks.get(), 3);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue146Test method shouldRespectFailureCondition.
public void shouldRespectFailureCondition() {
AtomicInteger successCounter = new AtomicInteger();
AtomicInteger failureCounter = new AtomicInteger();
AtomicInteger failedAttemptCounter = new AtomicInteger();
RetryPolicy<Object> retryPolicy = RetryPolicy.builder().handleResultIf(Objects::isNull).withMaxRetries(2).onSuccess(e -> successCounter.incrementAndGet()).onFailure(e -> failureCounter.incrementAndGet()).onFailedAttempt(e -> failedAttemptCounter.incrementAndGet()).build();
Failsafe.with(retryPolicy).get(() -> null);
assertEquals(3, failedAttemptCounter.get());
assertEquals(0, successCounter.get());
assertEquals(1, failureCounter.get());
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method assertScheduledRetryDelay.
/**
* Asserts that the ExecutionScheduledEvent.getDelay is as expected.
*/
public void assertScheduledRetryDelay() throws Throwable {
// Given
Waiter waiter = new Waiter();
RetryPolicy<Object> rp = RetryPolicy.builder().withDelay(Duration.ofMillis(10)).onRetryScheduled(e -> {
waiter.assertEquals(e.getDelay().toMillis(), 10L);
waiter.resume();
}).build();
// Sync when / then
ignoreExceptions(() -> Failsafe.with(rp).run(() -> {
throw new IllegalStateException();
}));
waiter.await(1000);
// Async when / then
Failsafe.with(rp).runAsync(() -> {
throw new IllegalStateException();
});
waiter.await(1000);
}
Aggregations