use of net.jodah.concurrentunit.Waiter 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 net.jodah.concurrentunit.Waiter in project failsafe by jhalterman.
the class Issue76 method shouldAbortOnAsyncError.
public void shouldAbortOnAsyncError() throws Exception {
final AssertionError error = new AssertionError();
Waiter waiter = new Waiter();
Future<?> future = Failsafe.with(new RetryPolicy().abortOn(AssertionError.class)).with(Executors.newSingleThreadScheduledExecutor()).onAbort(e -> {
waiter.assertEquals(e, error);
waiter.resume();
}).run(() -> {
throw error;
});
waiter.await(1000);
try {
future.get();
fail();
} catch (ExecutionException e) {
assertEquals(e.getCause(), error);
}
}
use of net.jodah.concurrentunit.Waiter 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);
}
use of net.jodah.concurrentunit.Waiter in project failsafe by jhalterman.
the class AbstractFailsafeTest method shouldFallbackAfterFailureWithCircuitBreaker.
/**
* Asserts that fallback works after a failure with a breaker configured.
*/
public void shouldFallbackAfterFailureWithCircuitBreaker() throws Throwable {
// Given
CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(3).withDelay(1, TimeUnit.MINUTES);
Exception failure = new ConnectException();
when(service.connect()).thenThrow(failure);
Waiter waiter = new Waiter();
// When / Then
assertEquals(failsafeGet(breaker, (r, f) -> {
waiter.assertNull(r);
waiter.assertEquals(failure, f);
return false;
}, () -> service.connect()), Boolean.FALSE);
verify(service).connect();
// Given
reset(service);
breaker.close();
when(service.connect()).thenThrow(failure);
// When / Then
assertThrows(() -> failsafeGet(breaker, (r, f) -> {
waiter.assertNull(r);
waiter.assertEquals(failure, f);
throw new RuntimeException(f);
}, () -> service.connect()), RuntimeException.class, ConnectException.class);
verify(service).connect();
}
use of net.jodah.concurrentunit.Waiter in project failsafe by jhalterman.
the class AbstractFailsafeTest method shouldFallbackWhenCircuitBreakerIsOpen.
/**
* Asserts that fallback works when a circuit breaker is open.
*/
public void shouldFallbackWhenCircuitBreakerIsOpen() throws Throwable {
// Given
CircuitBreaker breaker = new CircuitBreaker().withSuccessThreshold(3).withDelay(1, TimeUnit.MINUTES);
breaker.open();
Exception failure = new ConnectException();
when(service.connect()).thenThrow(failure);
Waiter waiter = new Waiter();
// When / Then
assertEquals(failsafeGet(breaker, (r, f) -> {
waiter.assertNull(r);
waiter.assertTrue(f instanceof CircuitBreakerOpenException);
return false;
}, service::connect), Boolean.FALSE);
verify(service, times(0)).connect();
}
Aggregations