ExponentialBackoffRetryHandler implements RetryHandler { Duration getSleepTime(int executionCount) { executionCount = executionCount > 30 ? 30 : executionCount; Duration sleepTime = baseBackOffDuration.multipliedBy(Math.max(1, random.nextInt(1 << executionCount))); return sleepTime.compareTo(maxBackOffDuration) > 0 ? maxBackOffDuration : sleepTime; }  @Override boolean retryRequest(IOException exception, int executionCount);  }
@Test public void testSleepTimeCalculation() throws Exception { ExponentialBackoffRetryHandler handler = new ExponentialBackoffRetryHandler(3, ofMillis(10), ofMillis(200), sleeper); assertThat(handler.getSleepTime(1), is(ofMillis(10L))); assertThat(handler.getSleepTime(2), is(both(greaterThan(ofMillis(9L))).and(lessThan(ofMillis(31L))))); assertThat(handler.getSleepTime(3), is(both(greaterThan(ofMillis(9L))).and(lessThan(ofMillis(71L))))); assertThat(handler.getSleepTime(4), is(both(greaterThan(ofMillis(9L))).and(lessThan(ofMillis(151L))))); assertThat(handler.getSleepTime(5), is(both(greaterThan(ofMillis(9L))).and(lessThan(ofMillis(201L))))); assertThat(handler.getSleepTime(6), is(both(greaterThan(ofMillis(9L))).and(lessThan(ofMillis(201L))))); }