@Test public void exponentialBackoff() throws InterruptedException { for (int i = 0; i < 100; i++) { ManualClock clock = new ManualClock(); ManualSleeper sleeper = new ManualSleeper(); long maxDurationMs = 500; long taskTimeMs = 20; ExponentialTimeBoundedRetry retry = ExponentialTimeBoundedRetry.builder() .withTimeCtx(new TimeContext(clock, sleeper)) .withMaxDuration(Duration.ofMillis(maxDurationMs)) .withInitialSleep(Duration.ofMillis(10)) .withMaxSleep(Duration.ofMillis(100)) .build(); Thread thread = new Thread(() -> { while (retry.attempt()) { clock.addTimeMs(taskTimeMs); } }); thread.setDaemon(true); thread.setName("time-bounded-exponential-backoff-test"); thread.start(); long timeRemainingMs = maxDurationMs - taskTimeMs; Iterator<Long> expectedBaseTimes = Arrays.asList(10L, 20L, 40L, 80L, 100L).iterator(); long expectedTime = expectedBaseTimes.next(); while (timeRemainingMs > 0) { Duration actualSleep = sleeper.waitForSleep(); checkBetween(expectedTime, expectedTime * 1.1, actualSleep); timeRemainingMs -= actualSleep.toMillis() + taskTimeMs; clock.addTime(actualSleep); sleeper.wakeUp(); if (expectedBaseTimes.hasNext()) { expectedTime = expectedBaseTimes.next(); } if (timeRemainingMs < 100) { expectedTime = timeRemainingMs; } } thread.interrupt(); thread.join(10 * Constants.SECOND_MS); assertFalse(retry.attempt()); } }
public static Builder builder() { return new Builder(); }
ExponentialTimeBoundedRetry extends TimeBoundedRetry { public static Builder builder() { return new Builder(); } }
ExponentialTimeBoundedRetry extends TimeBoundedRetry { public static Builder builder() { return new Builder(); } private  ExponentialTimeBoundedRetry(TimeContext timeCtx, Duration maxDuration,
      Duration initialSleep, Duration maxSleep, boolean skipInitialSleep); }
ExponentialTimeBoundedRetry extends TimeBoundedRetry { public static Builder builder() { return new Builder(); } private  ExponentialTimeBoundedRetry(TimeContext timeCtx, Duration maxDuration,
      Duration initialSleep, Duration maxSleep, boolean skipInitialSleep); static Builder builder(); }
ExponentialTimeBoundedRetry extends TimeBoundedRetry { public static Builder builder() { return new Builder(); } private  ExponentialTimeBoundedRetry(TimeContext timeCtx, Duration maxDuration,
      Duration initialSleep, Duration maxSleep, boolean skipInitialSleep); static Builder builder();  }