Skip to content

Commit dcaa107

Browse files
authored
Validate negative base delay for exponential backoff (Azure#22357)
* Validate negative base delay for exponential backoff * update javadoc
1 parent bea489b commit dcaa107

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/http/policy/ExponentialBackoff.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public ExponentialBackoff() {
3939
* @param maxRetries The max retry attempts that can be made.
4040
* @param baseDelay The base delay duration for retry.
4141
* @param maxDelay The max delay duration for retry.
42+
* @throws IllegalArgumentException if {@code maxRetries} is less than 0 or {@code baseDelay} is less than or
43+
* equal to 0 or {@code maxDelay} is less than {@code baseDelay}.
4244
*/
4345
public ExponentialBackoff(int maxRetries, Duration baseDelay, Duration maxDelay) {
4446
if (maxRetries < 0) {
@@ -47,8 +49,8 @@ public ExponentialBackoff(int maxRetries, Duration baseDelay, Duration maxDelay)
4749
Objects.requireNonNull(baseDelay, "'baseDelay' cannot be null.");
4850
Objects.requireNonNull(maxDelay, "'maxDelay' cannot be null.");
4951

50-
if (baseDelay.isZero()) {
51-
throw logger.logExceptionAsError(new IllegalArgumentException("'baseDelay' cannot be 0."));
52+
if (baseDelay.isZero() || baseDelay.isNegative()) {
53+
throw logger.logExceptionAsError(new IllegalArgumentException("'baseDelay' cannot be negative or 0."));
5254
}
5355

5456
if (baseDelay.compareTo(maxDelay) > 0) {

sdk/core/azure-core/src/test/java/com/azure/core/http/policy/ExponentialBackoffTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public void testNegativeMaxRetries() {
4646
Duration.ofMillis(5000)));
4747
}
4848

49+
@Test
50+
public void testNegativeBaseDelay() {
51+
assertThrows(IllegalArgumentException.class, () -> new ExponentialBackoff(5, Duration.ofSeconds(-1),
52+
Duration.ofMillis(5000)));
53+
}
54+
4955
@Test
5056
public void testBaseEqualToMaxDelay() {
5157
ExponentialBackoff expBackoff = new ExponentialBackoff(3, Duration.ofSeconds(1),

0 commit comments

Comments
 (0)