Search in sources :

Example 1 with Failsafe

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

the class NoPolicyTest method testWithNoPolicy.

public void testWithNoPolicy() {
    AtomicInteger successCounter = new AtomicInteger();
    AtomicInteger failureCounter = new AtomicInteger();
    FailsafeExecutor<Object> failsafe = Failsafe.none().onFailure(e -> {
        System.out.println("Failure");
        failureCounter.incrementAndGet();
    }).onSuccess(e -> {
        System.out.println("Success");
        successCounter.incrementAndGet();
    });
    // Test success
    testGetSuccess(() -> {
        successCounter.set(0);
        failureCounter.set(0);
    }, failsafe, ctx -> {
        return "success";
    }, (f, e) -> {
        assertEquals(e.getAttemptCount(), 1);
        assertEquals(e.getExecutionCount(), 1);
        assertEquals(successCounter.get(), 1);
        assertEquals(failureCounter.get(), 0);
    }, "success");
    // Test failure
    testRunFailure(() -> {
        successCounter.set(0);
        failureCounter.set(0);
    }, failsafe, ctx -> {
        throw new IllegalStateException();
    }, (f, e) -> {
        assertEquals(e.getAttemptCount(), 1);
        assertEquals(e.getExecutionCount(), 1);
        assertEquals(successCounter.get(), 0);
        assertEquals(failureCounter.get(), 1);
    }, IllegalStateException.class);
}
Also used : FailsafeExecutor(dev.failsafe.FailsafeExecutor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Failsafe (dev.failsafe.Failsafe)1 FailsafeExecutor (dev.failsafe.FailsafeExecutor)1 Testing (dev.failsafe.testing.Testing)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Assert.assertEquals (org.testng.Assert.assertEquals)1 Test (org.testng.annotations.Test)1