Skip to content

Commit f29d825

Browse files
committed
chore: ensure backoff is reset along with retryContext
1 parent 2e8a0ee commit f29d825

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/DefaultRetryContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void reset() {
8282
}
8383
lastRecordedErrorNs = retryingDependencies.getClock().nanoTime();
8484
clearPendingBackoff();
85+
backoff.reset();
8586
} finally {
8687
lock.unlock();
8788
}

google-cloud-storage/src/test/java/com/google/cloud/storage/RetryContextTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public boolean shouldRetry(Throwable previousThrowable, Object previousResponse)
210210
Arrays.stream(suppressed).map(Throwable::getMessage).collect(Collectors.toList());
211211
assertThat(suppressedMessages)
212212
.containsExactly(
213-
"Unretryable error (attempts: 1, maxAttempts: 6, elapsed: PT9.002S, nextBackoff: PT3S)");
213+
"Unretryable error (attempts: 1, maxAttempts: 6, elapsed: PT0S, nextBackoff: PT3S)");
214214
});
215215
}
216216

@@ -381,6 +381,35 @@ public boolean shouldRetry(Throwable previousThrowable, Object previousResponse)
381381
assertThat(retryContextSplits.size()).isEqualTo(retryHelperSplits.size());
382382
}
383383

384+
@Test
385+
public void resetAlsoResetsBackoffState() throws Exception {
386+
Throwable t1 = apiException(Code.INTERNAL, "{err1}");
387+
Throwable t2 = apiException(Code.INTERNAL, "{err2}");
388+
RetryContext ctx =
389+
RetryContext.of(
390+
scheduledExecutorService, maxAttempts(1), Retrying.alwaysRetry(), Jitterer.noJitter());
391+
392+
AtomicReference<Throwable> err1 = new AtomicReference<>();
393+
AtomicReference<Throwable> err2 = new AtomicReference<>();
394+
ctx.recordError(t1, failOnSuccess(), err1::set);
395+
ctx.reset();
396+
ctx.recordError(t2, failOnSuccess(), err2::set);
397+
398+
assertAll(
399+
() -> {
400+
String messages = TestUtils.messagesToText(err1.get());
401+
assertThat(messages)
402+
.contains(
403+
"Operation failed to complete within attempt budget (attempts: 1, maxAttempts: 1, elapsed: PT0S, nextBackoff: PT3S)");
404+
},
405+
() -> {
406+
String messages = TestUtils.messagesToText(err2.get());
407+
assertThat(messages)
408+
.contains(
409+
"Operation failed to complete within attempt budget (attempts: 1, maxAttempts: 1, elapsed: PT0S, nextBackoff: PT3S)");
410+
});
411+
}
412+
384413
private static ApiException apiException(Code code, String message) {
385414
return ApiExceptionFactory.createException(message, null, GrpcStatusCode.of(code), false);
386415
}

0 commit comments

Comments
 (0)