use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class DelayableRetryPolicyTest method shouldDelayOnMatchingFailureType.
public void shouldDelayOnMatchingFailureType() {
AtomicInteger delays = new AtomicInteger(0);
RetryPolicy<Integer> retryPolicy = RetryPolicy.<Integer>builder().handle(UncheckedExpectedException.class).withMaxRetries(4).withDelayFnOn(ctx -> {
// side-effect for test purposes
delays.incrementAndGet();
return Duration.ofNanos(1);
}, DelayException.class).build();
AtomicInteger attempts = new AtomicInteger(0);
int result = Failsafe.with(Fallback.of(123), retryPolicy).get(() -> {
int i = attempts.getAndIncrement();
switch(i) {
case 0:
case 2:
throw new DelayException();
default:
throw new UncheckedExpectedException();
}
});
assertEquals(result, 123, "Fallback should be used");
assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
assertEquals(delays.get(), 2, "Expecting two dynamic delays matching DelayException failure");
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class DelayableRetryPolicyTest method shouldDelayOnMatchingResult.
public void shouldDelayOnMatchingResult() {
AtomicInteger delays = new AtomicInteger(0);
RetryPolicy<Object> retryPolicy = RetryPolicy.builder().handleResultIf(result -> true).withMaxRetries(4).withDelayFnWhen(ctx -> {
// side-effect for test purposes
delays.incrementAndGet();
return Duration.ofNanos(1);
}, "expected").build();
Fallback<Object> fallback = Fallback.<Object>builder(123).handleResultIf(result -> true).build();
AtomicInteger attempts = new AtomicInteger(0);
Object result = Failsafe.with(fallback, retryPolicy).get(() -> {
int i = attempts.getAndIncrement();
switch(i) {
case 0:
case 3:
return "expected";
default:
return i;
}
});
assertEquals(result, 123, "Fallback should be used");
assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
assertEquals(delays.get(), 2, "Expecting two dynamic delays matching String result");
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36Test method failedAttemptListener_WithExceptions_ShouldBeCalled.
@Test
public void failedAttemptListener_WithExceptions_ShouldBeCalled() {
AtomicInteger listenerCallbacks = new AtomicInteger();
RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onFailedAttempt(e -> listenerCallbacks.incrementAndGet()).build();
Testing.ignoreExceptions(() -> Failsafe.with(policy).get(() -> {
throw new RuntimeException();
}));
assertEquals(listenerCallbacks.get(), 4);
}
use of dev.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<Object> retryPolicy = RetryPolicy.builder().withDelay(Duration.ofSeconds(1)).onSuccess(e -> System.out.println("Success!")).onFailure(e -> System.out.println("Connection attempts failed")).build();
Failsafe.with(retryPolicy).with(group).runAsyncExecution(execution -> bootstrap.connect(HOST, PORT).addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
execution.complete();
try {
channelFuture.sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception ignore) {
group.shutdownGracefully();
}
} else
execution.recordFailure(channelFuture.cause());
}));
Thread.sleep(5000);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class VertxExample method main.
/**
* A Vert.x sender and retryable receiver example.
*/
public static void main(String... args) throws Throwable {
// Receiver that fails 2 times then succeeds
AtomicInteger failures = new AtomicInteger();
vertx.eventBus().consumer("ping-address", message -> {
if (failures.getAndIncrement() < 2)
message.fail(1, "Failed");
else {
message.reply("pong!");
}
});
// Retryable sender
Failsafe.with(retryPolicy).with(scheduler).getAsyncExecution(execution -> vertx.eventBus().send("ping-address", "ping!", reply -> {
if (reply.succeeded())
execution.recordResult(reply.result());
else
execution.recordFailure(reply.cause());
}));
Thread.sleep(5000);
System.exit(0);
}
Aggregations