Skip to content

Commit

Permalink
add a test that could generate negative with the old code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmccracken committed Oct 21, 2022
1 parent 3afd7d8 commit 3fd8ec1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public boolean shouldRetry(RetryContext retryContext) {
public int getNextRetryIntervalMillis(RetryContext retryContext){
int retryCount = retryContext.getRetryCount();
int factor = Math.min(1 << (Math.min(retryCount, 30)), maxRetryFactor);
return retryIntervalMillis * ThreadLocalRandom.current().nextInt(factor);
long value = ((long) retryIntervalMillis) * ThreadLocalRandom.current().nextInt(factor);
return (int) Math.min(Integer.MAX_VALUE, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ public void setUp() {
maxRetryFactor);
}

@Test
public void test_largeRetryCount_notNegative() {
for (int j = 0; j < 10; j++) {
RetryContext retryContext = new RetryContext();
for (int i = 0; i < 40; i++) {
retryContext.incrementRetryCount();
int nextRetryIntervalMillis = socket5xxExponentialRandomBackoffPolicy.getNextRetryIntervalMillis(retryContext);
assertTrue("i=" + i + ", retryContext.getRetryCount()=" + retryContext.getRetryCount() + ", nextRetryIntervalMillis was negative " + nextRetryIntervalMillis, nextRetryIntervalMillis >= 0);
}
}
}

@Test
public void test_shouldRetry() {
HttpProvider.HttpResponse httpResponse = Mockito.mock(HttpProvider.HttpResponse.class);
Expand Down

0 comments on commit 3fd8ec1

Please sign in to comment.