Skip to content
This repository was archived by the owner on Mar 30, 2019. It is now read-only.

Commit 9724cbc

Browse files
committed
Don't silently swallow predicate errors
1 parent 47d9f67 commit 9724cbc

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/main/java/com/nurkiewicz/asyncretry/RetryJob.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,20 @@ protected void handleThrowable(Throwable t, long duration) {
5353

5454
protected void handleUserThrowable(Throwable t, long duration) {
5555
final AsyncRetryContext nextRetryContext = context.nextRetry(t);
56-
if (parent.getRetryPolicy().shouldContinue(nextRetryContext)) {
57-
final long delay = calculateNextDelay(duration, nextRetryContext, parent.getBackoff());
58-
retryWithDelay(nextRetryContext, delay, duration);
59-
} else {
60-
logFailure(nextRetryContext, duration);
56+
57+
try {
58+
if (parent.getRetryPolicy().shouldContinue(nextRetryContext)) {
59+
final long delay = calculateNextDelay(duration, nextRetryContext, parent.getBackoff());
60+
retryWithDelay(nextRetryContext, delay, duration);
61+
} else {
62+
logFailure(nextRetryContext, duration);
63+
future.completeExceptionally(t);
64+
}
65+
} catch (Throwable t2) {
66+
log.error("Threw while trying to decide on retry {} after {}",
67+
nextRetryContext.getRetryCount(),
68+
duration,
69+
t2);
6170
future.completeExceptionally(t);
6271
}
6372
}

src/test/java/com/nurkiewicz/asyncretry/AsyncRetryJobTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ public void shouldRethrowOriginalExceptionFromUserFutureCompletion() throws Exce
106106
}
107107
}
108108

109+
@Test
110+
public void shouldRethrowOriginalExceptionFromUserFutureCompletionAndAbortWhenTestFails() throws Exception {
111+
//given
112+
final RetryExecutor executor = new AsyncRetryExecutor(schedulerMock).
113+
abortIf(t -> { throw new RuntimeException("test invalid"); });
114+
115+
given(serviceMock.safeAsync()).willReturn(
116+
failedAsync(new SocketException(DON_T_PANIC))
117+
);
118+
119+
//when
120+
final CompletableFuture<String> future = executor.getFutureWithRetry(ctx -> serviceMock.safeAsync());
121+
122+
//then
123+
assertThat(future.isCompletedExceptionally()).isTrue();
124+
125+
try {
126+
future.get();
127+
failBecauseExceptionWasNotThrown(ExecutionException.class);
128+
} catch (ExecutionException e) {
129+
final Throwable cause = e.getCause();
130+
assertThat(cause).isInstanceOf(SocketException.class);
131+
assertThat(cause).hasMessage(DON_T_PANIC);
132+
}
133+
}
134+
109135
@Test
110136
public void shouldAbortWhenTargetFutureWantsToAbort() throws Exception {
111137
//given

0 commit comments

Comments
 (0)