use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanAbortForFailure.
@SuppressWarnings("unchecked")
public void testCanAbortForFailure() {
RetryPolicy policy = new RetryPolicy().abortOn(Exception.class);
assertTrue(policy.canAbortFor(null, new Exception()));
assertTrue(policy.canAbortFor(null, new IllegalArgumentException()));
policy = new RetryPolicy().abortOn(IllegalArgumentException.class, IOException.class);
assertTrue(policy.canAbortFor(null, new IllegalArgumentException()));
assertTrue(policy.canAbortFor(null, new IOException()));
assertFalse(policy.canAbortFor(null, new RuntimeException()));
assertFalse(policy.canAbortFor(null, new IllegalStateException()));
policy = new RetryPolicy().abortOn(Arrays.asList(IllegalArgumentException.class));
assertTrue(policy.canAbortFor(null, new IllegalArgumentException()));
assertFalse(policy.canAbortFor(null, new RuntimeException()));
assertFalse(policy.canAbortFor(null, new IllegalStateException()));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCopy.
public void testCopy() {
RetryPolicy rp = new RetryPolicy();
rp.withBackoff(2, 20, TimeUnit.SECONDS, 2.5);
rp.withMaxDuration(60, TimeUnit.SECONDS);
rp.withMaxRetries(3);
RetryPolicy rp2 = rp.copy();
assertEquals(rp.getDelay().toNanos(), rp2.getDelay().toNanos());
assertEquals(rp.getDelayFactor(), rp2.getDelayFactor());
assertEquals(rp.getMaxDelay().toNanos(), rp2.getMaxDelay().toNanos());
assertEquals(rp.getMaxDuration().toNanos(), rp2.getMaxDuration().toNanos());
assertEquals(rp.getMaxRetries(), rp2.getMaxRetries());
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanAbortForFailurePredicate.
public void testCanAbortForFailurePredicate() {
RetryPolicy policy = new RetryPolicy().abortOn(failure -> failure instanceof ConnectException);
assertTrue(policy.canAbortFor(null, new ConnectException()));
assertFalse(policy.canAbortFor(null, new IllegalArgumentException()));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanRetryForFailure.
@SuppressWarnings("unchecked")
public void testCanRetryForFailure() {
RetryPolicy policy = new RetryPolicy();
assertTrue(policy.canRetryFor(null, new Exception()));
assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
policy = new RetryPolicy().retryOn(Exception.class);
assertTrue(policy.canRetryFor(null, new Exception()));
assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
policy = new RetryPolicy().retryOn(RuntimeException.class);
assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
assertFalse(policy.canRetryFor(null, new Exception()));
policy = new RetryPolicy().retryOn(IllegalArgumentException.class, IOException.class);
assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
assertTrue(policy.canRetryFor(null, new IOException()));
assertFalse(policy.canRetryFor(null, new RuntimeException()));
assertFalse(policy.canRetryFor(null, new IllegalStateException()));
policy = new RetryPolicy().retryOn(Arrays.asList(IllegalArgumentException.class));
assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
assertFalse(policy.canRetryFor(null, new RuntimeException()));
assertFalse(policy.canRetryFor(null, new IllegalStateException()));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Java8Example method main.
@SuppressWarnings("unused")
public static void main(String... args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
RetryPolicy retryPolicy = new RetryPolicy();
// Create a retryable functional interface
Function<String, String> bar = value -> Failsafe.with(retryPolicy).get(() -> value + "bar");
// Create a retryable runnable Stream
Failsafe.with(retryPolicy).run(() -> Stream.of("foo").map(value -> value + "bar").forEach(System.out::println));
// Create a retryable callable Stream
Failsafe.with(retryPolicy).get(() -> Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).collect(Collectors.toList()));
// Create a individual retryable Stream operation
Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);
// Create a retryable CompletableFuture
Failsafe.with(retryPolicy).with(executor).future(() -> CompletableFuture.supplyAsync(() -> "foo").thenApplyAsync(value -> value + "bar").thenAccept(System.out::println));
// Create an individual retryable CompletableFuture stages
CompletableFuture.supplyAsync(() -> Failsafe.with(retryPolicy).get(() -> "foo")).thenApplyAsync(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).thenAccept(System.out::println);
}
Aggregations