Skip to content

Commit

Permalink
Bugfix/fix 0 retry times (#981)
Browse files Browse the repository at this point in the history
## Context
#928 

## Change
Fix endless retry when maxy retry is ste to 0 or negative

## Checklist
Before submitting this PR, please check the following points:
- [x] I have added unit and integration tests for my change
- [x] All unit and integration tests in the module I have added/changed
are green
- [x] All unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules are green
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)
(only when a new module is added)
  • Loading branch information
VaderKai committed May 6, 2024
1 parent f34c543 commit 31a86b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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);
}
}

0 comments on commit 31a86b5

Please sign in to comment.