Search in sources :

Example 1 with RetryPolicy

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

Example 2 with RetryPolicy

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

Example 3 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 4 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 5 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)

Aggregations

RetryPolicy (net.jodah.failsafe.RetryPolicy)56 Failsafe (net.jodah.failsafe.Failsafe)16 IOException (java.io.IOException)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 TimeUnit (java.util.concurrent.TimeUnit)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Executors (java.util.concurrent.Executors)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Test (org.testng.annotations.Test)5 ConnectException (java.net.ConnectException)4 Duration (java.time.Duration)4 Map (java.util.Map)4 FailsafeException (net.jodah.failsafe.FailsafeException)4 Assert.assertEquals (org.testng.Assert.assertEquals)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3