Search in sources :

Example 1 with RetryPolicy

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

the class Java8Example method main.

@SuppressWarnings("unused")
public static void main(String... args) {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
    RetryPolicy<Object> retryPolicy = RetryPolicy.ofDefaults();
    // Create a retryable functional interface
    Function<String, String> bar = value -> Failsafe.with(retryPolicy).get(() -> value + "bar");
    // Create a retryable Stream operation
    Failsafe.with(retryPolicy).get(() -> Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).collect(Collectors.toList()));
    // Create an individual retryable Stream operation
    Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);
    // Create a retryable CompletableFuture
    Failsafe.with(retryPolicy).with(executor).getStageAsync(() -> CompletableFuture.supplyAsync(() -> "foo").thenApplyAsync(value -> value + "bar").thenAccept(System.out::println));
    // Create an individual retryable CompletableFuture stages
    CompletableFuture.supplyAsync(() -> Failsafe.with(retryPolicy).get(() -> "foo")).thenApplyAsync(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).thenAccept(System.out::println);
}
Also used : Stream(java.util.stream.Stream) RetryPolicy(dev.failsafe.RetryPolicy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Failsafe(dev.failsafe.Failsafe) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)

Example 2 with RetryPolicy

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

the class Issue5Test 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<Object> retryPolicy = RetryPolicy.builder().withDelay(Duration.ofMillis(100)).withMaxDuration(Duration.ofSeconds(2)).withMaxRetries(3).handleResult(null).build();
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Failsafe.with(retryPolicy).with(executor).onFailure(e -> {
        waiter.assertNull(e.getResult());
        waiter.assertNull(e.getException());
        waiter.resume();
    }).getAsync(() -> null);
    waiter.await(1000);
    executor.shutdownNow();
}
Also used : Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(dev.failsafe.RetryPolicy) java.util.concurrent(java.util.concurrent) Duration(java.time.Duration) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) Waiter(net.jodah.concurrentunit.Waiter)

Example 3 with RetryPolicy

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

the class Issue36Test method failedAttemptListener_WithFailedResponses_ShouldBeCalled.

@Test
public void failedAttemptListener_WithFailedResponses_ShouldBeCalled() {
    AtomicInteger listenerCallbacks = new AtomicInteger();
    RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onFailedAttempt(e -> listenerCallbacks.incrementAndGet()).build();
    Failsafe.with(policy).get(() -> false);
    assertEquals(listenerCallbacks.get(), 4);
}
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 4 with RetryPolicy

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

the class Issue192Test method testAsync.

/**
 * Asserts the handling of multiple retry policies with an async execution.
 */
public void testAsync() {
    AtomicInteger exceptionA = new AtomicInteger();
    AtomicInteger exceptionB = new AtomicInteger();
    AtomicInteger exceptionC = new AtomicInteger();
    RetryPolicy<Object> policyA = RetryPolicy.builder().handle(ExceptionA.class).withMaxRetries(5).onRetry(evt -> exceptionA.incrementAndGet()).build();
    RetryPolicy<Object> policyB = RetryPolicy.builder().handle(ExceptionB.class).withMaxRetries(3).onRetry(evt -> exceptionB.incrementAndGet()).build();
    RetryPolicy<Object> policyC = RetryPolicy.builder().handle(ExceptionC.class).withMaxRetries(2).onRetry(evt -> exceptionC.incrementAndGet()).build();
    Asserts.assertThrows(() -> Failsafe.with(policyA, policyB, policyC).getAsyncExecution(execution -> Testing.futureException(executor, new ExceptionB()).whenComplete((result, failure) -> {
        // System.out.println("Result = " + result + "; failure = " + failure);
        execution.record(result, failure);
    })).get(), ExecutionException.class, ExceptionB.class);
    Assert.assertEquals(exceptionA.get(), 0);
    Assert.assertEquals(exceptionB.get(), 3);
    Assert.assertEquals(exceptionC.get(), 0);
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) AfterClass(org.testng.annotations.AfterClass) RetryPolicy(dev.failsafe.RetryPolicy) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeClass(org.testng.annotations.BeforeClass) Asserts(dev.failsafe.testing.Asserts) 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)

Example 5 with RetryPolicy

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

the class Issue260Test method test.

public void test() throws Throwable {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Timeout<Object> timeout = Timeout.builder(Duration.ofMillis(300)).withInterrupt().onFailure(e -> System.out.println("Interrupted")).build();
    RetryPolicy<Object> rp = RetryPolicy.builder().onRetry(e -> System.out.println("Retrying")).onSuccess(e -> System.out.println("Success")).build();
    Function<Integer, ContextualRunnable> task = (taskId) -> ctx -> {
        System.out.println("Starting execution of task " + taskId);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            System.out.println("Interrupted task " + taskId);
            throw e;
        }
    };
    Future<?> f1 = Failsafe.with(rp, timeout).with(executor).runAsync(task.apply(1));
    Future<?> f2 = Failsafe.with(rp, timeout).with(executor).runAsync(task.apply(2));
    Future<?> f3 = Failsafe.with(rp, timeout).with(executor).runAsync(task.apply(3));
    f1.get(1, TimeUnit.SECONDS);
    f2.get(1, TimeUnit.SECONDS);
    f3.get(1, TimeUnit.SECONDS);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Future(java.util.concurrent.Future) RetryPolicy(dev.failsafe.RetryPolicy) Duration(java.time.Duration) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) ContextualRunnable(dev.failsafe.function.ContextualRunnable) Timeout(dev.failsafe.Timeout) Function(java.util.function.Function) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) ContextualRunnable(dev.failsafe.function.ContextualRunnable) ExecutorService(java.util.concurrent.ExecutorService)

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