use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanAbortForNull.
public void testCanAbortForNull() {
RetryPolicy policy = new RetryPolicy();
assertFalse(policy.canAbortFor(null, null));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanRetryForCompletionPredicate.
public void testCanRetryForCompletionPredicate() {
RetryPolicy policy = new RetryPolicy().retryIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
assertTrue(policy.canRetryFor("test", null));
// No retries needed for successful result
assertFalse(policy.canRetryFor(0, null));
assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
assertFalse(policy.canRetryFor(null, new IllegalStateException()));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class NettyExample method main.
public static void main(String... args) throws Throwable {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = createBootstrap(group);
RetryPolicy retryPolicy = new RetryPolicy().withDelay(1, TimeUnit.SECONDS);
Failsafe.with(retryPolicy).with(group).runAsync(execution -> bootstrap.connect(HOST, PORT).addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
System.out.println("Connected!");
try {
channelFuture.sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception ignore) {
group.shutdownGracefully();
}
} else if (!execution.retryOn(channelFuture.cause()))
System.out.println("Connection attempts failed");
}));
Thread.sleep(5000);
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36 method retryListener_WithFailedResponses_ShouldBeCalled.
@Test
public void retryListener_WithFailedResponses_ShouldBeCalled() throws Exception {
RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
AtomicInteger listenerCallbacks = new AtomicInteger();
Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> false);
assertEquals(listenerCallbacks.get(), 3);
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36 method retryListener_WithExceptions_ShouldBeCalled.
@Test
public void retryListener_WithExceptions_ShouldBeCalled() throws Exception {
RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
AtomicInteger listenerCallbacks = new AtomicInteger();
try {
Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> {
throw new RuntimeException();
});
} catch (RuntimeException e) {
}
assertEquals(listenerCallbacks.get(), 3);
}
Aggregations