ExponentialBackOff implements BackOff { @Override public long getBackOffInMillis() { if (++numberOfRetries > maximumRetries) { return -1; } double delta = randomizationFactor * currentIntervalMillis; double minInterval = currentIntervalMillis - delta; double maxInterval = currentIntervalMillis + delta; long interval = (long) (minInterval + (Math.random() * (maxInterval - minInterval + 1))); if (currentIntervalMillis >= maximumIntervalMillis / multiplier) { currentIntervalMillis = maximumIntervalMillis; } else { currentIntervalMillis *= multiplier; } return interval; } ExponentialBackOff();  ExponentialBackOff(Builder builder); @Override long getBackOffInMillis(); @Override void reset(); long getInitialIntervalMillis(); long getMaximumIntervalMillis(); double getRandomizationFactor(); double getMultiplier(); int getMaximumRetries(); int getNumberOfRetries(); static final long INITIAL_INTERVAL_MILLIS; static final long MAXIMUM_INTERVAL_MILLIS; static final double RANDOMIZATION_FACTOR; static final double MULTIPLIER; static final int MAXIMUM_RETRIES; }
@Test public void testMaximumRetries() { int maxRetries = 3; BackOff backOff = new ExponentialBackOff.Builder() .setMaximumRetries(maxRetries).build(); int retries = 0; while (backOff.getBackOffInMillis() != -1) { retries++; } assertEquals(maxRetries, retries); }