@Test public void testNoRetry() { Random random = mock(Random.class); ExponentialBackoff backoff = new ExponentialBackoff(0, ONE_SECOND, random); assertThat(backoff.nextBackoff()).isEmpty(); }
private Stream<Worker> createWorkers( Meter rate, Histogram avgRetries, int numThreads, int rateLimit, Duration delay) { RateLimiter rateLimiter = RateLimiter.create(rateLimit); return IntStream.range(0, numThreads) .mapToObj(unused -> new Worker( () -> new ExponentialBackoff(4, Duration.ofMillis(250), ThreadLocalRandom.current()), limiters, delay, rateLimiter, rate, avgRetries)); }
() -> new ExponentialBackoff( config.maxNumRetries(), config.backoffSlotSize(), ThreadLocalRandom.current()), urlSelector,
@Test public void testRetriesCorrectNumberOfTimesAndFindsRandomBackoffWithInExponentialInterval() { Random random = mock(Random.class); ExponentialBackoff backoff = new ExponentialBackoff(3, ONE_SECOND, random); when(random.nextDouble()).thenReturn(1.0); assertThat(backoff.nextBackoff()).contains(ONE_SECOND.multipliedBy(2)); when(random.nextDouble()).thenReturn(1.0); assertThat(backoff.nextBackoff()).contains(ONE_SECOND.multipliedBy(4)); when(random.nextDouble()).thenReturn(0.5); assertThat(backoff.nextBackoff()).contains(ONE_SECOND.multipliedBy(4 /* 8 * 0.5 (exp * jitter), see above */)); assertThat(backoff.nextBackoff()).isEmpty(); } }
() -> new ExponentialBackoff( config.maxNumRetries(), config.backoffSlotSize(), ThreadLocalRandom.current()), urlSelector,