Skip to content

Commit

Permalink
Don't silently swallow predicate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chaptastic committed May 22, 2015
1 parent 47d9f67 commit 9724cbc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/com/nurkiewicz/asyncretry/RetryJob.java
Expand Up @@ -53,11 +53,20 @@ protected void handleThrowable(Throwable t, long duration) {

protected void handleUserThrowable(Throwable t, long duration) {
final AsyncRetryContext nextRetryContext = context.nextRetry(t);
if (parent.getRetryPolicy().shouldContinue(nextRetryContext)) {
final long delay = calculateNextDelay(duration, nextRetryContext, parent.getBackoff());
retryWithDelay(nextRetryContext, delay, duration);
} else {
logFailure(nextRetryContext, duration);

try {
if (parent.getRetryPolicy().shouldContinue(nextRetryContext)) {
final long delay = calculateNextDelay(duration, nextRetryContext, parent.getBackoff());
retryWithDelay(nextRetryContext, delay, duration);
} else {
logFailure(nextRetryContext, duration);
future.completeExceptionally(t);
}
} catch (Throwable t2) {
log.error("Threw while trying to decide on retry {} after {}",
nextRetryContext.getRetryCount(),
duration,
t2);
future.completeExceptionally(t);
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/nurkiewicz/asyncretry/AsyncRetryJobTest.java
Expand Up @@ -106,6 +106,32 @@ public void shouldRethrowOriginalExceptionFromUserFutureCompletion() throws Exce
}
}

@Test
public void shouldRethrowOriginalExceptionFromUserFutureCompletionAndAbortWhenTestFails() throws Exception {
//given
final RetryExecutor executor = new AsyncRetryExecutor(schedulerMock).
abortIf(t -> { throw new RuntimeException("test invalid"); });

given(serviceMock.safeAsync()).willReturn(
failedAsync(new SocketException(DON_T_PANIC))
);

//when
final CompletableFuture<String> future = executor.getFutureWithRetry(ctx -> serviceMock.safeAsync());

//then
assertThat(future.isCompletedExceptionally()).isTrue();

try {
future.get();
failBecauseExceptionWasNotThrown(ExecutionException.class);
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
assertThat(cause).isInstanceOf(SocketException.class);
assertThat(cause).hasMessage(DON_T_PANIC);
}
}

@Test
public void shouldAbortWhenTargetFutureWantsToAbort() throws Exception {
//given
Expand Down

0 comments on commit 9724cbc

Please sign in to comment.