Search in sources :

Example 1 with Failsafe

use of net.jodah.failsafe.Failsafe in project failsafe by jhalterman.

the class Issue36 method failedAttemptListener_WithExceptions_ShouldBeCalled.

@Test
public void failedAttemptListener_WithExceptions_ShouldBeCalled() throws Exception {
    RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
    AtomicInteger listenerCallbacks = new AtomicInteger();
    try {
        Failsafe.<Boolean>with(policy).onFailedAttempt((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> {
            throw new RuntimeException();
        });
    } catch (RuntimeException e) {
    }
    assertEquals(listenerCallbacks.get(), 4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Failsafe(net.jodah.failsafe.Failsafe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy) Test(org.testng.annotations.Test)

Example 2 with Failsafe

use of net.jodah.failsafe.Failsafe in project failsafe by jhalterman.

the class Issue36 method failedAttemptListener_WithFailedResponses_ShouldBeCalled.

@Test
public void failedAttemptListener_WithFailedResponses_ShouldBeCalled() throws Exception {
    RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
    AtomicInteger listenerCallbacks = new AtomicInteger();
    Failsafe.<Boolean>with(policy).onFailedAttempt((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> false);
    assertEquals(listenerCallbacks.get(), 4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Failsafe(net.jodah.failsafe.Failsafe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy) Test(org.testng.annotations.Test)

Example 3 with Failsafe

use of net.jodah.failsafe.Failsafe in project failsafe by jhalterman.

the class Issue36 method retryListener_WithFailedResponses_ShouldBeCalled.

@Test
public void retryListener_WithFailedResponses_ShouldBeCalled() throws Exception {
    RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
    AtomicInteger listenerCallbacks = new AtomicInteger();
    Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> false);
    assertEquals(listenerCallbacks.get(), 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Failsafe(net.jodah.failsafe.Failsafe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy) Test(org.testng.annotations.Test)

Example 4 with Failsafe

use of net.jodah.failsafe.Failsafe in project failsafe by jhalterman.

the class Issue36 method retryListener_WithExceptions_ShouldBeCalled.

@Test
public void retryListener_WithExceptions_ShouldBeCalled() throws Exception {
    RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
    AtomicInteger listenerCallbacks = new AtomicInteger();
    try {
        Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> {
            throw new RuntimeException();
        });
    } catch (RuntimeException e) {
    }
    assertEquals(listenerCallbacks.get(), 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Failsafe(net.jodah.failsafe.Failsafe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy) Test(org.testng.annotations.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Failsafe (net.jodah.failsafe.Failsafe)4 RetryPolicy (net.jodah.failsafe.RetryPolicy)4 Assert.assertEquals (org.testng.Assert.assertEquals)4 Assert.fail (org.testng.Assert.fail)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Test (org.testng.annotations.Test)4