Skip to content

Commit

Permalink
GH-464: Fix backOff API in UniformRandomBackOffPolicy
Browse files Browse the repository at this point in the history
Fixes: #464
Issue link: #464

When `maxBackOffPeriod` is less than `minBackOffPeriod`, delta is taken
taken as zero in `UniformRandomBackOffPolicy` `backOff()` method.

* Update tests to use `DummySleeper`
* Extract `UniformRandomBackOffPolicy` `backOffSuppliers` into local variables
* Modify `UniformRandomBackOffPolicy` tests
  • Loading branch information
LokeshAlamuri authored Sep 9, 2024
1 parent db47bc6 commit c0a4941
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

}

0 comments on commit c0a4941

Please sign in to comment.