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);
}
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);
}
Aggregations