SweepDelay { long getNextPause(SweepIterationResult result) { return SweepIterationResults.caseOf(result) .success(this::updateCurrentPauseAndGet) .unableToAcquireShard_(maxPauseMillis) .insufficientConsistency_(BACKOFF) .otherError_(maxPauseMillis) .disabled_(BACKOFF); } SweepDelay(long configPause);   }
@Test public void iterationWithNormalBatchReturnsInitialPause() { assertThat(delay.getNextPause(SUCCESS)).isEqualTo(INITIAL_DELAY); }
@Test public void configurationBelowMinimumIsSetToMinimum() { SweepDelay negativeDelay = new SweepDelay(-5L); assertThat(negativeDelay.getNextPause(SUCCESS)).isEqualTo(MIN_PAUSE_MILLIS); }
@Test public void configurationAboveDefaultMaximumIsRespected() { SweepDelay largeDelay = new SweepDelay(2 * DEFAULT_MAX_PAUSE_MILLIS); assertThat(largeDelay.getNextPause(SUCCESS)).isEqualTo(2 * DEFAULT_MAX_PAUSE_MILLIS); }
@Test public void unableToAcquireShardReturnsMaxPause() { assertThat(delay.getNextPause(SweepIterationResults.unableToAcquireShard())) .isEqualTo(DEFAULT_MAX_PAUSE_MILLIS); }
@Test public void insufficientConsistencyReturnsBackoff() { assertThat(delay.getNextPause(SweepIterationResults.insufficientConsistency())).isEqualTo(BACKOFF); }
@Test public void otherErrorReturnsMaxPause() { assertThat(delay.getNextPause(SweepIterationResults.otherError())).isEqualTo(DEFAULT_MAX_PAUSE_MILLIS); }
@Test public void disabledReturnsBackoff() { assertThat(delay.getNextPause(SweepIterationResults.disabled())).isEqualTo(BACKOFF); }
@Test public void iterationWithSmallBatchIncreasesPause() { assertThat(delay.getNextPause(SUCCESS_TOO_FAST)).isGreaterThan(INITIAL_DELAY); }
@Test public void iterationWithFullBatchReducesPause() { assertThat(delay.getNextPause(SUCCESS_TOO_SLOW)).isLessThan(INITIAL_DELAY); }
@Test public void consistentSmallBatchesGravitatesTowardsMaximumPause() { sweepTwentyIterationsWithResult(SUCCESS_TOO_FAST); assertThat(delay.getNextPause(SUCCESS_TOO_FAST)) .isGreaterThanOrEqualTo((long) (DEFAULT_MAX_PAUSE_MILLIS * 0.95)); }
@Test public void consistentFullBatchesGravitatesTowardsMinimumPause() { sweepTwentyIterationsWithResult(SUCCESS_TOO_SLOW); assertThat(delay.getNextPause(SUCCESS_TOO_SLOW)).isGreaterThanOrEqualTo((long) (MIN_PAUSE_MILLIS * 1.05)); }
@Test public void consistentNormalBatchesAfterFullBatchesGravitatesTowardsInitialPause() { sweepTwentyIterationsWithResult(SUCCESS_TOO_SLOW); sweepTwentyIterationsWithResult(SUCCESS); long nextPause = delay.getNextPause(SUCCESS); assertThat(nextPause).isGreaterThanOrEqualTo((long) (INITIAL_DELAY * 0.95)); assertThat(nextPause).isLessThanOrEqualTo((long) (INITIAL_DELAY * 1.05)); }