@Test public void testMaximumBackoffAndSeveralSteps() { Backoff backoff = Backoff.builder() .withInitialBackoff(Duration.ofSeconds(1)) .withMaximumBackoff(Duration.ofSeconds(11)) .withExpansion(2.0) .withAttemptLimit(8) .build(); int[] expected = {1, 2, 4, 8, 11, 11, 11}; for (int seconds : expected) { assertTrue(backoff.hasNext()); assertThat(backoff.nextBackoff(), equalTo(Duration.ofSeconds(seconds))); } assertFalse(backoff.hasNext()); } }
@Test public void testAttemptLimited() { int attemptLimit = 5; Backoff backoff = Backoff.builder().withAttemptLimit(attemptLimit).build(); int i = 1; while (backoff.hasNext()) { ++i; backoff.nextBackoff(); } assertThat(i, equalTo(attemptLimit)); }
@Test public void testExpansion() { Backoff backoff = Backoff.builder().withExpansion(2.0).withAttemptLimit(5).build(); assertTrue(backoff.hasNext()); Duration lastBackoff = backoff.nextBackoff(); assertTrue(backoff.hasNext()); while (backoff.hasNext()) { Duration nextBackoff = backoff.nextBackoff(); assertThat(nextBackoff, equalTo(lastBackoff.plus(lastBackoff))); lastBackoff = nextBackoff; } }