Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/fix 0 retry times #981

Merged
merged 10 commits into from
May 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public <T> T withRetry(Callable<T> action, int maxAttempts) {
try {
return action.call();
} catch (Exception e) {
if (attempt == maxAttempts) {
if (attempt >= maxAttempts) {
throw new RuntimeException(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,36 @@ void testMaxAttemptsReached() throws Exception {
verify(mockAction, times(3)).call();
verifyNoMoreInteractions(mockAction);
}

@Test
void testZeroAttemptsReached() throws Exception {
@SuppressWarnings("unchecked")
Callable<String> mockAction = mock(Callable.class);
when(mockAction.call()).thenThrow(new RuntimeException());

RetryUtils.RetryPolicy policy = RetryUtils.retryPolicyBuilder()
.delayMillis(100)
.build();

assertThatThrownBy(() -> policy.withRetry(mockAction, 0))
.isInstanceOf(RuntimeException.class);
verify(mockAction, times(1)).call();
verifyNoMoreInteractions(mockAction);
}

@Test
void testIllegalAttemptsReached() throws Exception {
@SuppressWarnings("unchecked")
Callable<String> mockAction = mock(Callable.class);
when(mockAction.call()).thenThrow(new RuntimeException());

RetryUtils.RetryPolicy policy = RetryUtils.retryPolicyBuilder()
.delayMillis(100)
.build();

assertThatThrownBy(() -> policy.withRetry(mockAction, -1))
.isInstanceOf(RuntimeException.class);
verify(mockAction, times(1)).call();
verifyNoMoreInteractions(mockAction);
}
}