### Question:
RandomJitter implements Jitter { long jitterOffsetCapped(BackoffDelay backoff) { try { return backoff.delay.multipliedBy((long) (100 * randomFactor)) .dividedBy(100) .toMillis(); } catch (ArithmeticException ae) { return Math.round(Long.MAX_VALUE * randomFactor); } } RandomJitter(double randomFactor); @Override Duration apply(BackoffDelay backoff); @Override String toString();  }### Answer:
@Test void jitterOffsetCappedForSuperLargeMaxDuration() { double factor = 0.5; Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofSeconds(Long.MAX_VALUE); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(factor); assertThat(jitter.jitterOffsetCapped(bd)) .isEqualTo(Math.round(Long.MAX_VALUE * factor)); }
@Test void jitterOffsetForMaximumMillisDuration() { double factor = 0.5; Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofMillis(Long.MAX_VALUE); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(factor); long expected = delay.multipliedBy(50) .dividedBy(100) .toMillis(); assertThat(jitter.jitterOffsetCapped(bd)) .isEqualTo(expected); }
@Test void jitterOffsetForReasonableDuration() { double factor = 0.5; Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofMillis(500); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(factor); assertThat(jitter.jitterOffsetCapped(bd)) .isEqualTo(250); }
@Test public void jitterOffsetCappedForSuperLargeMaxDuration() { double factor = 0.5; Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofSeconds(Long.MAX_VALUE); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(factor); assertThat(jitter.jitterOffsetCapped(bd)) .isEqualTo(Math.round(Long.MAX_VALUE * factor)); }
@Test public void jitterOffsetForMaximumMillisDuration() { double factor = 0.5; Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofMillis(Long.MAX_VALUE); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(factor); long expected = delay.multipliedBy(50) .dividedBy(100) .toMillis(); assertThat(jitter.jitterOffsetCapped(bd)) .isEqualTo(expected); }
@Test public void jitterOffsetForReasonableDuration() { double factor = 0.5; Duration min = Duration.ofMillis(100); Duration max = Duration.ofMillis(100); Duration delay = Duration.ofMillis(500); BackoffDelay bd = new BackoffDelay(min, max, delay); RandomJitter jitter = new RandomJitter(factor); assertThat(jitter.jitterOffsetCapped(bd)) .isEqualTo(250); }