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