Search in sources :

Example 1 with Asserts

use of dev.failsafe.testing.Asserts in project failsafe by jhalterman.

the class FailsafeFutureTest method shouldCallOnCompleteWhenCancelled.

/**
 * Asserts that retries are stopped and completion handlers are called on cancel.
 */
public void shouldCallOnCompleteWhenCancelled() throws Throwable {
    Waiter waiter = new Waiter();
    CompletableFuture<String> future = Failsafe.with(RetryPolicy.ofDefaults()).with(executor).onComplete(e -> {
        waiter.assertNull(e.getResult());
        waiter.assertTrue(e.getException() instanceof CancellationException);
        waiter.resume();
    }).getAsync(() -> {
        Thread.sleep(1000);
        throw new IllegalStateException();
    });
    // Note: We have to add whenComplete to the returned future separately, otherwise cancel will not be noticed by
    // Failsafe
    future.whenComplete((result, failure) -> {
        waiter.assertNull(result);
        waiter.assertTrue(failure instanceof CancellationException);
        waiter.resume();
    });
    future.cancel(true);
    waiter.await(1000, 2);
    future.complete("unexpected2");
    Asserts.assertThrows(future::get, CancellationException.class);
}
Also used : AfterClass(org.testng.annotations.AfterClass) Waiter(net.jodah.concurrentunit.Waiter) Assert(org.testng.Assert) CancellationException(java.util.concurrent.CancellationException) Asserts(dev.failsafe.testing.Asserts) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) CancellationException(java.util.concurrent.CancellationException) Waiter(net.jodah.concurrentunit.Waiter)

Example 2 with Asserts

use of dev.failsafe.testing.Asserts 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)

Aggregations

Asserts (dev.failsafe.testing.Asserts)2 Executors (java.util.concurrent.Executors)2 Assert (org.testng.Assert)2 AfterClass (org.testng.annotations.AfterClass)2 Test (org.testng.annotations.Test)2 Failsafe (dev.failsafe.Failsafe)1 RetryPolicy (dev.failsafe.RetryPolicy)1 Testing (dev.failsafe.testing.Testing)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Waiter (net.jodah.concurrentunit.Waiter)1 BeforeClass (org.testng.annotations.BeforeClass)1