JitteredDelaySupplier implements RetryDelaySupplier { @Override @SuppressFBWarnings("PREDICTABLE_RANDOM") public long nextDelay() { long nextDelay = wrapped.nextDelay(); if (nextDelay > 1) { long jitter = (long) (nextDelay * jitterFactor); if (jitter > 0) { return ThreadLocalRandom.current().nextLong(Math.max(0, nextDelay - jitter), nextDelay); } else { return nextDelay; } } else { return nextDelay; } } JitteredDelaySupplier(final RetryDelaySupplier wrapped, final double jitterFactor); @Override @SuppressFBWarnings("PREDICTABLE_RANDOM") // we can use predicatable randoms here long nextDelay(); @Override String toString();  }
@Test public void testJitteredDelay() { JitteredDelaySupplier sup = new JitteredDelaySupplier(new FibonacciRetryDelaySupplier(3, 0, 3000000000L), 0); Assert.assertEquals(0, sup.nextDelay()); Assert.assertEquals(0, sup.nextDelay()); Assert.assertEquals(0, sup.nextDelay()); Assert.assertEquals(1, sup.nextDelay()); }
@Test public void testJitteredDelay2() { JitteredDelaySupplier sup = new JitteredDelaySupplier(new FibonacciRetryDelaySupplier(3, 1, 3000000000L), 0); Assert.assertEquals(1, sup.nextDelay()); Assert.assertEquals(1, sup.nextDelay()); Assert.assertEquals(1, sup.nextDelay()); Assert.assertEquals(2, sup.nextDelay()); }
@Test public void testJitteredDelay3() { JitteredDelaySupplier sup = new JitteredDelaySupplier(new FibonacciRetryDelaySupplier(3, 1, 3000000000L), 0.3); Assert.assertEquals(1, sup.nextDelay()); Assert.assertEquals(1, sup.nextDelay()); Assert.assertEquals(1, sup.nextDelay()); Assert.assertEquals(2, sup.nextDelay()); }
@Test public void testJitteredDelay4() { JitteredDelaySupplier sup = new JitteredDelaySupplier(new FibonacciRetryDelaySupplier(3, 10, 3000000000L), 0.3); Assert.assertThat(sup.nextDelay(), Matchers.greaterThanOrEqualTo(7L)); Assert.assertThat(sup.nextDelay(), Matchers.lessThanOrEqualTo(10L)); }