Search in sources :

Example 6 with RetryPolicy

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

Example 7 with RetryPolicy

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);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) Waiter(net.jodah.concurrentunit.Waiter) FailsafeException(net.jodah.failsafe.FailsafeException) Assert.fail(org.testng.Assert.fail) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Executors(java.util.concurrent.Executors) Failsafe(net.jodah.failsafe.Failsafe) Waiter(net.jodah.concurrentunit.Waiter) ExecutionException(java.util.concurrent.ExecutionException) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 8 with RetryPolicy

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);
}
Also used : Listeners(net.jodah.failsafe.Listeners) FailsafeFuture(net.jodah.failsafe.FailsafeFuture) Assert.assertEquals(org.testng.Assert.assertEquals) Mockito.times(org.mockito.Mockito.times) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) Failsafe(net.jodah.failsafe.Failsafe) Waiter(net.jodah.concurrentunit.Waiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Testing.failures(net.jodah.failsafe.Testing.failures) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Mockito.mock(org.mockito.Mockito.mock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Listeners(net.jodah.failsafe.Listeners) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 9 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 10 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)

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