|
33 | 33 | import org.apache.accumulo.core.util.Retry.RetryFactory;
|
34 | 34 | import org.easymock.EasyMock;
|
35 | 35 | import org.junit.jupiter.api.BeforeEach;
|
| 36 | +import org.junit.jupiter.api.Nested; |
36 | 37 | import org.junit.jupiter.api.Test;
|
37 | 38 | import org.slf4j.Logger;
|
38 | 39 | import org.slf4j.LoggerFactory;
|
@@ -351,4 +352,101 @@ public void testInfiniteRetryWithBackoff() throws InterruptedException {
|
351 | 352 | }
|
352 | 353 | }
|
353 | 354 | }
|
| 355 | + |
| 356 | + @Nested |
| 357 | + public class MaxRetriesWithinDuration { |
| 358 | + |
| 359 | + @Test |
| 360 | + public void noIncrement() { |
| 361 | + Duration retriesForDuration = Duration.ofSeconds(3); |
| 362 | + Duration retryAfter = Duration.ofMillis(100); |
| 363 | + Retry retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration) |
| 364 | + .retryAfter(retryAfter).incrementBy(Duration.ofMillis(0)).maxWait(Duration.ofMillis(1000)) |
| 365 | + .backOffFactor(1.0).logInterval(Duration.ofMinutes(3)).createRetry(); |
| 366 | + |
| 367 | + // with no increment, the number of retries should be the duration divided by the retryAfter |
| 368 | + // (which is used as the initial wait and in this case does not change) |
| 369 | + long expectedRetries = retriesForDuration.dividedBy(retryAfter); |
| 370 | + assertEquals(expectedRetries, retry.getMaxRetries()); |
| 371 | + |
| 372 | + // try again with lots of expected retries |
| 373 | + retriesForDuration = Duration.ofSeconds(30); |
| 374 | + retryAfter = Duration.ofMillis(10); |
| 375 | + retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration).retryAfter(retryAfter) |
| 376 | + .incrementBy(Duration.ofMillis(0)).maxWait(Duration.ofMillis(1000)).backOffFactor(1.0) |
| 377 | + .logInterval(Duration.ofMinutes(3)).createRetry(); |
| 378 | + |
| 379 | + expectedRetries = retriesForDuration.dividedBy(retryAfter); |
| 380 | + assertEquals(expectedRetries, retry.getMaxRetries()); |
| 381 | + } |
| 382 | + |
| 383 | + @Test |
| 384 | + public void withIncrement() { |
| 385 | + final Duration retriesForDuration = Duration.ofMillis(1500); |
| 386 | + final Duration retryAfter = Duration.ofMillis(100); |
| 387 | + final Duration increment = Duration.ofMillis(100); |
| 388 | + |
| 389 | + Retry retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration) |
| 390 | + .retryAfter(retryAfter).incrementBy(increment).maxWait(Duration.ofMillis(1000)) |
| 391 | + .backOffFactor(1.0).logInterval(Duration.ofMinutes(3)).createRetry(); |
| 392 | + |
| 393 | + // the max retries should be calculated like this: |
| 394 | + // 1. 100 |
| 395 | + // 2. 100 + 100 = 200 |
| 396 | + // 3. 200 + 100 = 300 |
| 397 | + // 4. 300 + 100 = 400 |
| 398 | + // 5. 400 + 100 = 500 |
| 399 | + |
| 400 | + // 100 + 200 + 300 + 400 + 500 = 1500 |
| 401 | + |
| 402 | + assertEquals(5, retry.getMaxRetries()); |
| 403 | + } |
| 404 | + |
| 405 | + @Test |
| 406 | + public void withBackoffFactorAndMaxWait() { |
| 407 | + final Duration retriesForDuration = Duration.ofSeconds(4); |
| 408 | + final Duration retryAfter = Duration.ofMillis(100); |
| 409 | + Retry retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration) |
| 410 | + .retryAfter(retryAfter).incrementBy(Duration.ofMillis(0)).maxWait(Duration.ofMillis(500)) |
| 411 | + .backOffFactor(1.5).logInterval(Duration.ofMinutes(3)).createRetry(); |
| 412 | + |
| 413 | + // max retries should be calculated like this: |
| 414 | + // 1. 100 |
| 415 | + // 2. 100 * 1.5 = 150 |
| 416 | + // 3. 150 * 1.5 = 225 |
| 417 | + // 4. 225 * 1.5 = 337 |
| 418 | + // 5. 337 * 1.5 = 505 (which is greater than the max wait of 500 so its capped) |
| 419 | + |
| 420 | + // 100 + 150 + 225 + 337 + 500 + 500 + 500 + 500 + 500 + 500 = 3812 |
| 421 | + assertEquals(10, retry.getMaxRetries()); |
| 422 | + } |
| 423 | + |
| 424 | + @Test |
| 425 | + public void smallDuration() { |
| 426 | + Duration retriesForDuration = Duration.ofMillis(0); |
| 427 | + final Duration retryAfter = Duration.ofMillis(100); |
| 428 | + Retry retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration) |
| 429 | + .retryAfter(retryAfter).incrementBy(Duration.ofMillis(0)).maxWait(Duration.ofMillis(500)) |
| 430 | + .backOffFactor(1.5).logInterval(Duration.ofMinutes(3)).createRetry(); |
| 431 | + assertEquals(0, retry.getMaxRetries()); |
| 432 | + |
| 433 | + retriesForDuration = Duration.ofMillis(99); |
| 434 | + assertTrue(retriesForDuration.compareTo(retryAfter) < 0); |
| 435 | + retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration).retryAfter(retryAfter) |
| 436 | + .incrementBy(Duration.ofMillis(0)).maxWait(Duration.ofMillis(500)).backOffFactor(1.5) |
| 437 | + .logInterval(Duration.ofMinutes(3)).createRetry(); |
| 438 | + assertEquals(0, retry.getMaxRetries()); |
| 439 | + } |
| 440 | + |
| 441 | + @Test |
| 442 | + public void equalDurationAndInitialWait() { |
| 443 | + final Duration retriesForDuration = Duration.ofMillis(100); |
| 444 | + final Duration retryAfter = Duration.ofMillis(100); |
| 445 | + assertEquals(0, retriesForDuration.compareTo(retryAfter)); |
| 446 | + Retry retry = Retry.builder().maxRetriesWithinDuration(retriesForDuration) |
| 447 | + .retryAfter(retryAfter).incrementBy(Duration.ofMillis(0)).maxWait(Duration.ofMillis(500)) |
| 448 | + .backOffFactor(1.5).logInterval(Duration.ofMinutes(3)).createRetry(); |
| 449 | + assertEquals(1, retry.getMaxRetries()); |
| 450 | + } |
| 451 | + } |
354 | 452 | }
|
0 commit comments