Search in sources :

Example 11 with RetryPolicy

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()));
}
Also used : IOException(java.io.IOException) RetryPolicy(net.jodah.failsafe.RetryPolicy) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Example 12 with RetryPolicy

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());
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 13 with RetryPolicy

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()));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy) ConnectException(java.net.ConnectException)

Example 14 with RetryPolicy

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()));
}
Also used : IOException(java.io.IOException) RetryPolicy(net.jodah.failsafe.RetryPolicy) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Example 15 with RetryPolicy

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);
}
Also used : Stream(java.util.stream.Stream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) RetryPolicy(net.jodah.failsafe.RetryPolicy) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Failsafe(net.jodah.failsafe.Failsafe) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Aggregations

RetryPolicy (net.jodah.failsafe.RetryPolicy)34 Failsafe (net.jodah.failsafe.Failsafe)12 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7 Test (org.testng.annotations.Test)7 Executors (java.util.concurrent.Executors)6 Assert.assertEquals (org.testng.Assert.assertEquals)6 ConnectException (java.net.ConnectException)5 Assert.fail (org.testng.Assert.fail)5 Future (java.util.concurrent.Future)4 TimeUnit (java.util.concurrent.TimeUnit)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Waiter (net.jodah.concurrentunit.Waiter)3 Paths (java.nio.file.Paths)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2