SlowModeOutputRateCalculator implements ModeOutputRateCalculator { @Override public OutputRateCalculationResult calculateOutputRate(double currentRate, double maximumOutputRate, SendCounters counters) { if (counters.majorityOfFailures()) { return new OutputRateCalculationResult(heartbeatModeRate, OutputRateCalculator.Mode.HEARTBEAT); } else if (counters.onlySuccessess()) { return new OutputRateCalculationResult(currentRate, OutputRateCalculator.Mode.NORMAL); } return new OutputRateCalculationResult(currentRate, OutputRateCalculator.Mode.SLOW); } SlowModeOutputRateCalculator(double heartbeatModeRate); @Override OutputRateCalculationResult calculateOutputRate(double currentRate,
                                                           double maximumOutputRate,
                                                           SendCounters counters);  }
@Test public void shouldStayInSlowModeIfThereWereSomeFailures() { counters.incrementSuccesses() .incrementFailures(); assertThat(calculator.calculateOutputRate(10, 20, counters)).hasRate(10).isInMode(OutputRateCalculator.Mode.SLOW); }
@Test public void shouldSwitchToNormalModeWithoutModifyingRateWhenOnlySuccesses() { counters.incrementSuccesses(); assertThat(calculator.calculateOutputRate(10, 20, counters)).hasRate(10).isInMode(OutputRateCalculator.Mode.NORMAL); }
@Test public void shouldSwitchToHeartbeatModeWhenThereWereOnlyFailures() { counters.incrementFailures(); assertThat(calculator.calculateOutputRate(10, 20, counters)).hasRate(HEARTBEAT_RATE).isInMode(OutputRateCalculator.Mode.HEARTBEAT); }