use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue76 method shouldAbortOnSyncError.
public void shouldAbortOnSyncError() throws Exception {
AssertionError error = new AssertionError();
try {
Failsafe.with(new RetryPolicy().abortOn(AssertionError.class)).run(() -> {
throw error;
});
fail();
} catch (FailsafeException e) {
assertEquals(e.getCause(), error);
}
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue76 method shouldAbortOnAsyncError.
public void shouldAbortOnAsyncError() throws Exception {
final AssertionError error = new AssertionError();
Waiter waiter = new Waiter();
Future<?> future = Failsafe.with(new RetryPolicy().abortOn(AssertionError.class)).with(Executors.newSingleThreadScheduledExecutor()).onAbort(e -> {
waiter.assertEquals(e, error);
waiter.resume();
}).run(() -> {
throw error;
});
waiter.await(1000);
try {
future.get();
fail();
} catch (ExecutionException e) {
assertEquals(e.getCause(), error);
}
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue9 method test.
public void test() throws Throwable {
// Given - Fail twice then succeed
AtomicInteger retryCounter = new AtomicInteger();
Service service = mock(Service.class);
when(service.connect()).thenThrow(failures(2, new IllegalStateException())).thenReturn(true);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Listeners<Boolean> listeners = new Listeners<Boolean>() {
public void onRetry(Boolean result, Throwable failure) {
retryCounter.incrementAndGet();
}
};
Waiter waiter = new Waiter();
// When
AtomicInteger successCounter = new AtomicInteger();
FailsafeFuture<Boolean> future = Failsafe.with(new RetryPolicy().withMaxRetries(2)).with(executor).with(listeners).onSuccess(p -> {
successCounter.incrementAndGet();
waiter.resume();
}).get(() -> service.connect());
// Then
waiter.await(1000);
verify(service, times(3)).connect();
assertEquals(future.get().booleanValue(), true);
assertEquals(retryCounter.get(), 2);
assertEquals(successCounter.get(), 1);
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method shouldRequireValidBackoff.
public void shouldRequireValidBackoff() {
shouldFail(() -> new RetryPolicy().withBackoff(0, 0, null), NullPointerException.class);
shouldFail(() -> new RetryPolicy().withMaxDuration(1, TimeUnit.MILLISECONDS).withBackoff(100, 120, TimeUnit.MILLISECONDS), IllegalStateException.class);
shouldFail(() -> new RetryPolicy().withBackoff(-3, 10, TimeUnit.MILLISECONDS), IllegalArgumentException.class);
shouldFail(() -> new RetryPolicy().withBackoff(100, 10, TimeUnit.MILLISECONDS), IllegalArgumentException.class);
shouldFail(() -> new RetryPolicy().withBackoff(5, 10, TimeUnit.MILLISECONDS, .5), IllegalArgumentException.class);
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanAbortForResultPredicate.
public void testCanAbortForResultPredicate() {
RetryPolicy policy = new RetryPolicy().abortIf((Integer result) -> result > 100);
assertTrue(policy.canAbortFor(110, null));
assertFalse(policy.canAbortFor(50, null));
assertFalse(policy.canAbortFor(50, new IllegalArgumentException()));
}
Aggregations