Search in sources :

Example 1 with Fallback

use of dev.failsafe.Fallback in project failsafe by jhalterman.

the class DelayableRetryPolicyTest method shouldDelayOnMatchingFailureType.

public void shouldDelayOnMatchingFailureType() {
    AtomicInteger delays = new AtomicInteger(0);
    RetryPolicy<Integer> retryPolicy = RetryPolicy.<Integer>builder().handle(UncheckedExpectedException.class).withMaxRetries(4).withDelayFnOn(ctx -> {
        // side-effect for test purposes
        delays.incrementAndGet();
        return Duration.ofNanos(1);
    }, DelayException.class).build();
    AtomicInteger attempts = new AtomicInteger(0);
    int result = Failsafe.with(Fallback.of(123), retryPolicy).get(() -> {
        int i = attempts.getAndIncrement();
        switch(i) {
            case 0:
            case 2:
                throw new DelayException();
            default:
                throw new UncheckedExpectedException();
        }
    });
    assertEquals(result, 123, "Fallback should be used");
    assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
    assertEquals(delays.get(), 2, "Expecting two dynamic delays matching DelayException failure");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RetryPolicy(dev.failsafe.RetryPolicy) ExecutionContext(dev.failsafe.ExecutionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Fallback(dev.failsafe.Fallback) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with Fallback

use of dev.failsafe.Fallback in project failsafe by jhalterman.

the class DelayableRetryPolicyTest method shouldDelayOnMatchingResult.

public void shouldDelayOnMatchingResult() {
    AtomicInteger delays = new AtomicInteger(0);
    RetryPolicy<Object> retryPolicy = RetryPolicy.builder().handleResultIf(result -> true).withMaxRetries(4).withDelayFnWhen(ctx -> {
        // side-effect for test purposes
        delays.incrementAndGet();
        return Duration.ofNanos(1);
    }, "expected").build();
    Fallback<Object> fallback = Fallback.<Object>builder(123).handleResultIf(result -> true).build();
    AtomicInteger attempts = new AtomicInteger(0);
    Object result = Failsafe.with(fallback, retryPolicy).get(() -> {
        int i = attempts.getAndIncrement();
        switch(i) {
            case 0:
            case 3:
                return "expected";
            default:
                return i;
        }
    });
    assertEquals(result, 123, "Fallback should be used");
    assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
    assertEquals(delays.get(), 2, "Expecting two dynamic delays matching String result");
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RetryPolicy(dev.failsafe.RetryPolicy) ExecutionContext(dev.failsafe.ExecutionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Fallback(dev.failsafe.Fallback) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

ExecutionContext (dev.failsafe.ExecutionContext)2 Failsafe (dev.failsafe.Failsafe)2 Fallback (dev.failsafe.Fallback)2 RetryPolicy (dev.failsafe.RetryPolicy)2 Testing (dev.failsafe.testing.Testing)2 Duration (java.time.Duration)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assert.assertEquals (org.testng.Assert.assertEquals)2 Test (org.testng.annotations.Test)2