@Test public void testCalculateBackoffMs() { TruncatedBinaryBackoff backoff = new TruncatedBinaryBackoff(Amount.of(1L, Time.MILLISECONDS), Amount.of(12L, Time.MILLISECONDS)); try { backoff.calculateBackoffMs(-1L); fail("calculateBackoffMs should throw an exception when give a negative value."); } catch (IllegalArgumentException e) { } long calculateBackoffMs0 = backoff.calculateBackoffMs(0); assertTrue(1 <= calculateBackoffMs0 && calculateBackoffMs0 <= 2); long calculateBackoffMs1 = backoff.calculateBackoffMs(1); assertTrue(1 <= calculateBackoffMs1 && calculateBackoffMs1 <= 2); long calculateBackoffMs2 = backoff.calculateBackoffMs(2); assertTrue(2 <= calculateBackoffMs2 && calculateBackoffMs2 <= 4); long calculateBackoffMs4 = backoff.calculateBackoffMs(4); assertTrue(4 <= calculateBackoffMs4 && calculateBackoffMs4 <= 8); long calculateBackoffMs8 = backoff.calculateBackoffMs(8); assertTrue(8 <= calculateBackoffMs8 && calculateBackoffMs8 <= 12); assertEquals(12, backoff.calculateBackoffMs(16)); }
@Override public long calculateBackoffMs(long lastBackoffMs) { Preconditions.checkArgument(lastBackoffMs >= 0); long halfBackoff = (lastBackoffMs == 0) ? initialBackoffMs : lastBackoffMs; return Math.min( maxBackoffIntervalMs, halfBackoff + Math.round(random.nextDouble() * halfBackoff)); }
TruncatedBinaryBackoff implements BackoffStrategy { @Override public long calculateBackoffMs(long lastBackoffMs) { Preconditions.checkArgument(lastBackoffMs >= 0); long halfBackoff = (lastBackoffMs == 0) ? initialBackoffMs : lastBackoffMs; return Math.min( maxBackoffIntervalMs, halfBackoff + Math.round(random.nextDouble() * halfBackoff)); } }
TruncatedBinaryBackoff implements BackoffStrategy { @Override public long calculateBackoffMs(long lastBackoffMs) { Preconditions.checkArgument(lastBackoffMs >= 0); long halfBackoff = (lastBackoffMs == 0) ? initialBackoffMs : lastBackoffMs; return Math.min( maxBackoffIntervalMs, halfBackoff + Math.round(random.nextDouble() * halfBackoff)); } TruncatedBinaryBackoff(
      Amount<Long, Time> initialBackoff,
      Amount<Long, Time> maxBackoff,
      boolean stopAtMax,
      Random random);  TruncatedBinaryBackoff(
      Amount<Long, Time> initialBackoff,
      Amount<Long, Time> maxBackoff,
      boolean stopAtMax);  TruncatedBinaryBackoff(Amount<Long, Time> initialBackoff, Amount<Long, Time> maxBackoff); }
TruncatedBinaryBackoff implements BackoffStrategy { @Override public long calculateBackoffMs(long lastBackoffMs) { Preconditions.checkArgument(lastBackoffMs >= 0); long halfBackoff = (lastBackoffMs == 0) ? initialBackoffMs : lastBackoffMs; return Math.min( maxBackoffIntervalMs, halfBackoff + Math.round(random.nextDouble() * halfBackoff)); } TruncatedBinaryBackoff(
      Amount<Long, Time> initialBackoff,
      Amount<Long, Time> maxBackoff,
      boolean stopAtMax,
      Random random);  TruncatedBinaryBackoff(
      Amount<Long, Time> initialBackoff,
      Amount<Long, Time> maxBackoff,
      boolean stopAtMax);  TruncatedBinaryBackoff(Amount<Long, Time> initialBackoff, Amount<Long, Time> maxBackoff); @Override long calculateBackoffMs(long lastBackoffMs); @Override boolean shouldContinue(long lastBackoffMs); }
TruncatedBinaryBackoff implements BackoffStrategy { @Override public long calculateBackoffMs(long lastBackoffMs) { Preconditions.checkArgument(lastBackoffMs >= 0); long halfBackoff = (lastBackoffMs == 0) ? initialBackoffMs : lastBackoffMs; return Math.min( maxBackoffIntervalMs, halfBackoff + Math.round(random.nextDouble() * halfBackoff)); } TruncatedBinaryBackoff(
      Amount<Long, Time> initialBackoff,
      Amount<Long, Time> maxBackoff,
      boolean stopAtMax,
      Random random);  TruncatedBinaryBackoff(
      Amount<Long, Time> initialBackoff,
      Amount<Long, Time> maxBackoff,
      boolean stopAtMax);  TruncatedBinaryBackoff(Amount<Long, Time> initialBackoff, Amount<Long, Time> maxBackoff); @Override long calculateBackoffMs(long lastBackoffMs); @Override boolean shouldContinue(long lastBackoffMs);  }