diff --git a/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java b/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java index 68249f7b..0f172b2b 100644 --- a/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java +++ b/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java @@ -134,8 +134,8 @@ public long getMaxBackOffPeriod() { protected void doBackOff() throws BackOffInterruptedException { try { Long min = this.minBackOffPeriod.get(); - long delta = this.maxBackOffPeriod.get() == this.minBackOffPeriod.get() ? 0 - : this.random.nextInt((int) (this.maxBackOffPeriod.get() - min)); + Long max = this.maxBackOffPeriod.get(); + long delta = max <= min ? 0 : this.random.nextInt((int) (max - min)); this.sleeper.sleep(min + delta); } catch (InterruptedException e) { diff --git a/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java b/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java index 0ea18385..4ba6ac44 100644 --- a/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java +++ b/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java @@ -36,10 +36,18 @@ public void testSetSleeper() { int maxBackOff = 10000; backOffPolicy.setMinBackOffPeriod(minBackOff); backOffPolicy.setMaxBackOffPeriod(maxBackOff); - UniformRandomBackOffPolicy withSleeper = backOffPolicy.withSleeper(new DummySleeper()); + + DummySleeper dummySleeper = new DummySleeper(); + UniformRandomBackOffPolicy withSleeper = backOffPolicy.withSleeper(dummySleeper); assertThat(withSleeper.getMinBackOffPeriod()).isEqualTo(minBackOff); assertThat(withSleeper.getMaxBackOffPeriod()).isEqualTo(maxBackOff); + + assertThat(dummySleeper.getBackOffs()).isEmpty(); + withSleeper.backOff(null); + + assertThat(dummySleeper.getBackOffs()).hasSize(1); + assertThat(dummySleeper.getBackOffs()[0]).isLessThan(maxBackOff); } @Test @@ -60,4 +68,20 @@ public void sleep(long backOffPeriod) throws InterruptedException { assertThat(Thread.interrupted()).isTrue(); } + @Test + public void testMaxBackOffLessThanMinBackOff() { + UniformRandomBackOffPolicy backOffPolicy = new UniformRandomBackOffPolicy(); + int minBackOff = 1000; + int maxBackOff = 10; + backOffPolicy.setMinBackOffPeriod(minBackOff); + backOffPolicy.setMaxBackOffPeriod(maxBackOff); + + DummySleeper dummySleeper = new DummySleeper(); + UniformRandomBackOffPolicy withSleeper = backOffPolicy.withSleeper(dummySleeper); + assertThat(dummySleeper.getBackOffs()).isEmpty(); + withSleeper.backOff(null); + assertThat(dummySleeper.getBackOffs()).hasSize(1); + assertThat(dummySleeper.getBackOffs()[0]).isEqualTo(minBackOff); + } + }