|
34 | 34 | import org.easymock.EasyMock;
|
35 | 35 | import org.junit.jupiter.api.BeforeEach;
|
36 | 36 | import org.junit.jupiter.api.Test;
|
| 37 | +import org.junit.jupiter.api.Timeout; |
37 | 38 | import org.slf4j.Logger;
|
38 | 39 | import org.slf4j.LoggerFactory;
|
39 | 40 |
|
@@ -352,36 +353,91 @@ public void testInfiniteRetryWithBackoff() throws InterruptedException {
|
352 | 353 | }
|
353 | 354 | }
|
354 | 355 |
|
| 356 | + @Timeout(30) |
355 | 357 | @Test
|
356 | 358 | public void testRetriesForDuration() throws InterruptedException {
|
357 |
| - final TimeUnit unit = MILLISECONDS; |
358 |
| - final long duration = 10_000; |
| 359 | + final Duration duration = Duration.ofSeconds(10); |
| 360 | + final Duration increment = Duration.ofMillis(500); |
| 361 | + Retry retry = Retry.builder().retriesForDuration(duration).retryAfter(Duration.ofMillis(0)) |
| 362 | + .incrementBy(increment).maxWait(increment).backOffFactor(1) |
| 363 | + .logInterval(Duration.ofMinutes(3)).createRetry(); |
| 364 | + |
| 365 | + // with the backoff factor set to 1 and with max wait set to the increment value, the expected |
| 366 | + // number of retries is the duration divided by the increment |
| 367 | + long expectedRetries = duration.dividedBy(increment); |
| 368 | + |
| 369 | + assertExpectedRetries(retry, duration, expectedRetries); |
| 370 | + } |
| 371 | + |
| 372 | + @Timeout(30) |
| 373 | + @Test |
| 374 | + public void testRetriesForDurationWithInitial() throws InterruptedException { |
| 375 | + final Duration duration = Duration.ofSeconds(10); |
| 376 | + final Duration increment = Duration.ofMillis(500); |
| 377 | + final Duration initialWait = Duration.ofMillis(400); |
| 378 | + Retry retry = |
| 379 | + Retry.builder().retriesForDuration(duration).retryAfter(initialWait).incrementBy(increment) |
| 380 | + .maxWait(increment).backOffFactor(1).logInterval(Duration.ofMinutes(3)).createRetry(); |
| 381 | + |
| 382 | + // with the backoff factor set to 1 and with max wait set to the increment value, the expected |
| 383 | + // number of retries is the duration minus the initial wait divided by the increment |
| 384 | + long expectedRetries = duration.minus(initialWait).dividedBy(increment); |
| 385 | + |
| 386 | + assertExpectedRetries(retry, duration, expectedRetries); |
| 387 | + } |
| 388 | + |
| 389 | + @Timeout(30) |
| 390 | + @Test |
| 391 | + public void testRetriesForDuration2() throws InterruptedException { |
| 392 | + final Duration increment = Duration.ofMillis(100); |
| 393 | + final double backOffFactor = 2; |
| 394 | + Duration maxWait = Duration.ofMillis(800); // Allows for 4 doublings 100- > 200 -> 400 -> 800 |
| 395 | + final Duration initialWait = Duration.ofMillis(50); |
| 396 | + |
| 397 | + // Total duration accounting for the initial wait, the max wait, and the backoff factor |
| 398 | + Duration duration = |
| 399 | + initialWait.plus(Duration.ofMillis(100 + 200 + 400 + 800 + 800 + 800 + 800)); |
| 400 | + |
| 401 | + // count up the expected retries from the calculation above |
| 402 | + long expectedRetries = 7; |
| 403 | + |
| 404 | + Retry retry = Retry.builder().retriesForDuration(duration).retryAfter(initialWait) |
| 405 | + .incrementBy(increment).maxWait(maxWait).backOffFactor(backOffFactor) |
| 406 | + .logInterval(Duration.ofMinutes(3)).createRetry(); |
359 | 407 |
|
360 |
| - Retry retry = Retry.builder().retriesForDuration(duration, unit).retryAfter(100, unit) |
361 |
| - .incrementBy(100, unit).maxWait(500, unit).backOffFactor(1.5).logInterval(3, MINUTES) |
362 |
| - .createRetry(); |
| 408 | + assertExpectedRetries(retry, duration, expectedRetries); |
| 409 | + } |
363 | 410 |
|
364 |
| - long totalTime = 0; |
365 |
| - long expectedRetries = 0; |
| 411 | + private static void assertExpectedRetries(Retry retry, Duration duration, long expectedRetries) |
| 412 | + throws InterruptedException { |
| 413 | + Duration totalTime = Duration.ZERO; |
366 | 414 |
|
| 415 | + int iterations = 0; |
367 | 416 | // While the total wait time is less than the duration and there are retries left
|
368 |
| - while (totalTime <= duration && retry.canRetry()) { |
369 |
| - totalTime += retry.getCurrentWait(); |
370 |
| - if (totalTime <= duration) { |
| 417 | + while (retry.canRetry() && (totalTime.compareTo(duration) <= 0)) { |
| 418 | + iterations++; |
| 419 | + totalTime = totalTime.plus(retry.getCurrentWait()); |
| 420 | + if (totalTime.compareTo(duration) <= 0) { |
371 | 421 | retry.useRetry();
|
372 |
| - expectedRetries++; |
373 | 422 | if (retry.canRetry()) {
|
374 |
| - try { |
375 |
| - retry.waitForNextAttempt(log, "Iteration " + expectedRetries); |
376 |
| - } catch (IllegalArgumentException | InterruptedException e) { |
377 |
| - log.error("Failed on iteration: {}", expectedRetries, e); |
378 |
| - throw e; |
379 |
| - } |
| 423 | + log.info("Iteration {} - Total time: {}ms - Current wait: {}ms", iterations, |
| 424 | + totalTime.toMillis(), retry.getCurrentWait().toMillis()); |
| 425 | + retry.waitForNextAttempt(log, "testRetriesForDuration"); |
380 | 426 | }
|
381 | 427 | }
|
382 | 428 | }
|
383 | 429 |
|
384 |
| - assertEquals(expectedRetries, retry.retriesCompleted()); |
| 430 | + assertEquals(expectedRetries, retry.retriesCompleted(), |
| 431 | + () -> getErrorMessage(expectedRetries, retry)); |
| 432 | + } |
| 433 | + |
| 434 | + static String getErrorMessage(long expectedRetries, Retry retry) { |
| 435 | + return String.format( |
| 436 | + "Expected %d retries, but only completed %d retries. Retry parameters: maxRetries=%dms, startWait=%sms, maxWait=%sms, waitIncrement=%sms, backOffFactor=%f, logInterval=%sms", |
| 437 | + expectedRetries, retry.retriesCompleted(), retry.getMaxRetries(), |
| 438 | + retry.getCurrentWait().toMillis(), retry.getMaxWait().toMillis(), |
| 439 | + retry.getWaitIncrement().toMillis(), retry.getWaitFactor(), |
| 440 | + retry.getLogInterval().toMillis()); |
385 | 441 | }
|
386 | 442 |
|
387 | 443 | }
|
0 commit comments