Search in sources :

Example 16 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue5 method test.

/**
   * Asserts that a failure is handled as expected by a listener registered via whenFailure.
   */
public void test() throws Throwable {
    Waiter waiter = new Waiter();
    RetryPolicy retryPolicy = new RetryPolicy().withDelay(100, TimeUnit.MILLISECONDS).withMaxDuration(2, TimeUnit.SECONDS).withMaxRetries(3).retryWhen(null);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Failsafe.with(retryPolicy).with(executor).onFailure((result, failure) -> {
        waiter.assertNull(result);
        waiter.assertNull(failure);
        waiter.resume();
    }).get(() -> null);
    waiter.await(1000);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Waiter(net.jodah.concurrentunit.Waiter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Executors(java.util.concurrent.Executors) Failsafe(net.jodah.failsafe.Failsafe) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 17 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue52 method shouldCancelExecutionViaFuture.

public void shouldCancelExecutionViaFuture() throws Throwable {
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
    FailsafeFuture<String> proxyFuture = Failsafe.with(new RetryPolicy().withDelay(10, TimeUnit.MILLISECONDS)).with(scheduler).get(exec -> {
        throw new IllegalStateException();
    });
    Thread.sleep(100);
    proxyFuture.cancel(true);
    assertNull(proxyFuture.get());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 18 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue52 method shouldCancelExecutionViaCompletableFuture.

public void shouldCancelExecutionViaCompletableFuture() throws Throwable {
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
    AtomicInteger counter = new AtomicInteger();
    CompletableFuture<String> proxyFuture = Failsafe.with(new RetryPolicy().withDelay(10, TimeUnit.MILLISECONDS)).with(scheduler).future(exec -> {
        counter.incrementAndGet();
        CompletableFuture<String> result = new CompletableFuture<>();
        result.completeExceptionally(new RuntimeException());
        return result;
    });
    Thread.sleep(100);
    proxyFuture.cancel(true);
    int count = counter.get();
    assertTrue(proxyFuture.isCancelled());
    Asserts.assertThrows(() -> proxyFuture.get(), CancellationException.class);
    // Assert that execution has actually stopped
    Thread.sleep(20);
    assertEquals(count, counter.get());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 19 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue75 method testThatFailSafeIsBrokenWithFallback.

@Test
public void testThatFailSafeIsBrokenWithFallback() throws Exception {
    CircuitBreaker breaker = new CircuitBreaker().withFailureThreshold(10, 100).withSuccessThreshold(2).withDelay(100, TimeUnit.MILLISECONDS);
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    int result = Failsafe.with(breaker).with(service).withFallback((a, b) -> 999).future(() -> CompletableFuture.completedFuture(223)).get();
    Assert.assertEquals(result, 223);
}
Also used : CircuitBreaker(net.jodah.failsafe.CircuitBreaker) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.testng.annotations.Test)

Example 20 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService 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

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)821 Test (org.junit.Test)267 CountDownLatch (java.util.concurrent.CountDownLatch)79 ArrayList (java.util.ArrayList)72 Test (org.testng.annotations.Test)72 IOException (java.io.IOException)71 ExecutorService (java.util.concurrent.ExecutorService)70 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)65 HashMap (java.util.HashMap)57 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)53 List (java.util.List)51 Map (java.util.Map)51 TimeUnit (java.util.concurrent.TimeUnit)44 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 ThreadFactory (java.util.concurrent.ThreadFactory)40 CompletableFuture (java.util.concurrent.CompletableFuture)35 UUID (java.util.UUID)34 Cleanup (lombok.Cleanup)31 ExecutionException (java.util.concurrent.ExecutionException)30 HashSet (java.util.HashSet)25