ExponentialBackoffRetryHandler implements RetryHandler { @Override public boolean retryRequest(IOException exception, int executionCount) { if (executionCount <= maxRetries) { Duration sleepTime = getSleepTime(executionCount); return sleeper.sleep(sleepTime); } else { return false; } }  @Override boolean retryRequest(IOException exception, int executionCount);  }
@Test public void testMaxRetryCount() throws Exception { ExponentialBackoffRetryHandler handler = new ExponentialBackoffRetryHandler(3, Duration.ZERO, Duration.ZERO, sleeper); doReturn(true).when(sleeper).sleep(any(Duration.class)); assertThat(handler.retryRequest(mock(IOException.class), 3), is(true)); assertThat(handler.retryRequest(mock(IOException.class), 4), is(false)); }
@Test public void testSleeping() throws Exception { ExponentialBackoffRetryHandler handler = new ExponentialBackoffRetryHandler(6, ofMillis(10), ofMillis(200), sleeper); handler.retryRequest(mock(IOException.class), 2); ArgumentCaptor<Duration> durationArgumentCaptor = ArgumentCaptor.forClass(Duration.class); verify(sleeper).sleep(durationArgumentCaptor.capture()); assertThat(durationArgumentCaptor.getValue(), is(both(greaterThan(ofMillis(9L))).and(lessThan(ofMillis(31L))))); }