ExponentialBackoffDelayStrategy implements DelayStrategy { @Override public long nextDelay(WaitContext context) { long delay = (long) Math.pow(2, context.getAttemptsMade()); delay *= 1000; if (delay <= 0) { return maxDelayInMillis; } return Math.min(delay, maxDelayInMillis); }  @Override long nextDelay(WaitContext context);  }
@Test public void getDelays() { ExponentialBackoffDelayStrategy strategy = new ExponentialBackoffDelayStrategy(30000L); WaitContext context = new WaitContext(System.currentTimeMillis()); assertEquals(1000L, strategy.nextDelay(context)); context.incrementAttempts(); assertEquals(2000L, strategy.nextDelay(context)); context.incrementAttempts(); assertEquals(4000L, strategy.nextDelay(context)); context.incrementAttempts(); assertEquals(8000L, strategy.nextDelay(context)); context.incrementAttempts(); assertEquals(16000L, strategy.nextDelay(context)); context.incrementAttempts(); assertEquals(30000L, strategy.nextDelay(context)); context.incrementAttempts(); assertEquals(30000L, strategy.nextDelay(context)); }
@Test public void getDelays_overflow() { ExponentialBackoffDelayStrategy strategy = new ExponentialBackoffDelayStrategy(30000L); WaitContext context = new WaitContext(System.currentTimeMillis()); for (int attempt = 0; attempt < 100; attempt++) { context.incrementAttempts(); assertTrue(strategy.nextDelay(context) > 0); assertTrue(strategy.nextDelay(context) <= 30000L); } }