Backoff { @SuppressWarnings("checkstyle:designforextension") public void linearBackoff() throws InterruptedException { if (currentType != BackoffType.LINEAR) { reset(); } currentType = BackoffType.LINEAR; linearWaitFor += LINEAR_BACKOFF_INC; if (linearWaitFor > LINEAR_BACKOFF_MAX) { linearWaitFor = LINEAR_BACKOFF_MAX; } metrics.sendLinearBackoff.mark(); log.info("Linear backoff for {} ms", linearWaitFor); sleeper.sleep(linearWaitFor); } Backoff(
            final Sleeper sleeper,
            final Logger log,
            final Metrics metrics); @SuppressWarnings("checkstyle:designforextension") void linearBackoff(); @SuppressWarnings("checkstyle:designforextension") void exponentialBackoff(); @SuppressWarnings("checkstyle:designforextension") void waitUntil(final long waitUntil); @SuppressWarnings("checkstyle:designforextension") void reset();  }
@Test public void should_backoff_linearlly() throws InterruptedException { Logger loggger = mock(Logger.class); Metrics metrics = new Metrics(); Sleeper sleeper = mock(Sleeper.class); Backoff b = new Backoff(sleeper, loggger, metrics); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 2 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 3 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 4 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 5 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 6 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 7 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 8 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper).sleep(Backoff.LINEAR_BACKOFF + 9 * Backoff.LINEAR_BACKOFF_INC); b.linearBackoff(); verify(sleeper, times(2)).sleep(Backoff.LINEAR_BACKOFF_MAX); b.linearBackoff(); }
@Test public void should_mark_metric_for_linear_backoff() throws InterruptedException { Logger loggger = mock(Logger.class); Metrics metrics = new Metrics(); Sleeper sleeper = mock(Sleeper.class); Backoff b = new Backoff(sleeper, loggger, metrics); b.linearBackoff(); assertEquals(1, metrics.sendLinearBackoff.getCount()); }