Search in sources :

Example 11 with RetryPolicy

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

the class Issue190Test method test.

public void test() throws Throwable {
    RetryPolicy<Object> policy = RetryPolicy.builder().withMaxRetries(5).build();
    AtomicInteger failureEvents = new AtomicInteger();
    AtomicInteger successEvents = new AtomicInteger();
    Waiter waiter = new Waiter();
    Failsafe.with(policy).onFailure(e -> {
        failureEvents.incrementAndGet();
        waiter.resume();
    }).onSuccess(e -> {
        successEvents.incrementAndGet();
        waiter.resume();
    }).getAsyncExecution(execution -> Testing.futureResult(executor, true).whenComplete((result, failure) -> {
        execution.recordResult(result);
    })).get();
    waiter.await(1000);
    Assert.assertEquals(failureEvents.get(), 0);
    Assert.assertEquals(successEvents.get(), 1);
}
Also used : AfterClass(org.testng.annotations.AfterClass) Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(dev.failsafe.RetryPolicy) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeClass(org.testng.annotations.BeforeClass) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) Executors(java.util.concurrent.Executors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Waiter(net.jodah.concurrentunit.Waiter)

Example 12 with RetryPolicy

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

the class Issue36Test method retryListener_WithExceptions_ShouldBeCalled.

@Test
public void retryListener_WithExceptions_ShouldBeCalled() {
    AtomicInteger listenerCallbacks = new AtomicInteger();
    RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onRetry(e -> listenerCallbacks.incrementAndGet()).build();
    Testing.ignoreExceptions(() -> Failsafe.with(policy).get(() -> {
        throw new RuntimeException();
    }));
    assertEquals(listenerCallbacks.get(), 3);
}
Also used : RetryPolicy(dev.failsafe.RetryPolicy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) RetryPolicyBuilder(dev.failsafe.RetryPolicyBuilder) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Example 13 with RetryPolicy

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

the class Issue36Test method retryListener_WithFailedResponses_ShouldBeCalled.

@Test
public void retryListener_WithFailedResponses_ShouldBeCalled() {
    AtomicInteger listenerCallbacks = new AtomicInteger();
    RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onRetry(e -> listenerCallbacks.incrementAndGet()).build();
    Failsafe.with(policy).get(() -> false);
    assertEquals(listenerCallbacks.get(), 3);
}
Also used : RetryPolicy(dev.failsafe.RetryPolicy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) RetryPolicyBuilder(dev.failsafe.RetryPolicyBuilder) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Example 14 with RetryPolicy

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

the class Issue146Test method shouldRespectFailureCondition.

public void shouldRespectFailureCondition() {
    AtomicInteger successCounter = new AtomicInteger();
    AtomicInteger failureCounter = new AtomicInteger();
    AtomicInteger failedAttemptCounter = new AtomicInteger();
    RetryPolicy<Object> retryPolicy = RetryPolicy.builder().handleResultIf(Objects::isNull).withMaxRetries(2).onSuccess(e -> successCounter.incrementAndGet()).onFailure(e -> failureCounter.incrementAndGet()).onFailedAttempt(e -> failedAttemptCounter.incrementAndGet()).build();
    Failsafe.with(retryPolicy).get(() -> null);
    assertEquals(3, failedAttemptCounter.get());
    assertEquals(0, successCounter.get());
    assertEquals(1, failureCounter.get());
}
Also used : Objects(java.util.Objects) RetryPolicy(dev.failsafe.RetryPolicy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 15 with RetryPolicy

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

the class RetryPolicyTest method assertScheduledRetryDelay.

/**
 * Asserts that the ExecutionScheduledEvent.getDelay is as expected.
 */
public void assertScheduledRetryDelay() throws Throwable {
    // Given
    Waiter waiter = new Waiter();
    RetryPolicy<Object> rp = RetryPolicy.builder().withDelay(Duration.ofMillis(10)).onRetryScheduled(e -> {
        waiter.assertEquals(e.getDelay().toMillis(), 10L);
        waiter.resume();
    }).build();
    // Sync when / then
    ignoreExceptions(() -> Failsafe.with(rp).run(() -> {
        throw new IllegalStateException();
    }));
    waiter.await(1000);
    // Async when / then
    Failsafe.with(rp).runAsync(() -> {
        throw new IllegalStateException();
    });
    waiter.await(1000);
}
Also used : Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(dev.failsafe.RetryPolicy) Duration(java.time.Duration) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) Waiter(net.jodah.concurrentunit.Waiter)

Aggregations

Failsafe (dev.failsafe.Failsafe)15 RetryPolicy (dev.failsafe.RetryPolicy)15 Test (org.testng.annotations.Test)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Testing (dev.failsafe.testing.Testing)9 Assert.assertEquals (org.testng.Assert.assertEquals)8 Duration (java.time.Duration)7 RetryPolicyBuilder (dev.failsafe.RetryPolicyBuilder)4 Executors (java.util.concurrent.Executors)4 Assert.fail (org.testng.Assert.fail)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 ExecutionException (java.util.concurrent.ExecutionException)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Waiter (net.jodah.concurrentunit.Waiter)3 ExecutionContext (dev.failsafe.ExecutionContext)2 Fallback (dev.failsafe.Fallback)2 Function (java.util.function.Function)2 Assert (org.testng.Assert)2 AfterClass (org.testng.annotations.AfterClass)2