Skip to content

Commit 1df5299

Browse files
committed
add a test that could generate negative with the old code.
Signed-off-by: Ken McCracken <ken.mccracken@here.com>
1 parent 0fa1638 commit 1df5299

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

here-oauth-client/src/main/java/com/here/account/oauth2/retry/Socket5xxExponentialRandomBackoffPolicy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public boolean shouldRetry(RetryContext retryContext) {
5252
public int getNextRetryIntervalMillis(RetryContext retryContext){
5353
int retryCount = retryContext.getRetryCount();
5454
int factor = Math.min(1 << (Math.min(retryCount, 30)), maxRetryFactor);
55-
return retryIntervalMillis * ThreadLocalRandom.current().nextInt(factor);
55+
long value = ((long) retryIntervalMillis) * ThreadLocalRandom.current().nextInt(factor);
56+
return (int) Math.min(Integer.MAX_VALUE, value);
5657
}
5758
}

here-oauth-client/src/test/java/com/here/account/oauth2/retry/Socket5xxExponentialRandomBackoffPolicyTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ public void setUp() {
2222
maxRetryFactor);
2323
}
2424

25+
@Test
26+
public void test_largeRetryCount_notNegative() {
27+
for (int j = 0; j < 10; j++) {
28+
RetryContext retryContext = new RetryContext();
29+
for (int i = 0; i < 40; i++) {
30+
retryContext.incrementRetryCount();
31+
int nextRetryIntervalMillis = socket5xxExponentialRandomBackoffPolicy.getNextRetryIntervalMillis(retryContext);
32+
assertTrue("i=" + i + ", retryContext.getRetryCount()=" + retryContext.getRetryCount() + ", nextRetryIntervalMillis was negative " + nextRetryIntervalMillis, nextRetryIntervalMillis >= 0);
33+
}
34+
}
35+
}
36+
2537
@Test
2638
public void test_shouldRetry() {
2739
HttpProvider.HttpResponse httpResponse = Mockito.mock(HttpProvider.HttpResponse.class);

0 commit comments

Comments
 (0)