Search in sources :

Example 1 with Listeners

use of net.jodah.failsafe.Listeners 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)

Aggregations

Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Waiter (net.jodah.concurrentunit.Waiter)1 Failsafe (net.jodah.failsafe.Failsafe)1 FailsafeFuture (net.jodah.failsafe.FailsafeFuture)1 Listeners (net.jodah.failsafe.Listeners)1 RetryPolicy (net.jodah.failsafe.RetryPolicy)1 Testing.failures (net.jodah.failsafe.Testing.failures)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1 Assert.assertEquals (org.testng.Assert.assertEquals)1 Test (org.testng.annotations.Test)1