public static Builder builder() {
    return new Builder();
  }
@Test
  public void exponentialBackoff() throws InterruptedException {
    // Run the test multiple times to cover more cases due to randomness of jitter.
    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();
        // Account for 10% added jitter.
        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());
    }
  }