diff --git a/src/main/java/com/github/nkzawa/backo/Backoff.java b/src/main/java/com/github/nkzawa/backo/Backoff.java index bf0ca01..65f4260 100644 --- a/src/main/java/com/github/nkzawa/backo/Backoff.java +++ b/src/main/java/com/github/nkzawa/backo/Backoff.java @@ -17,6 +17,10 @@ public class Backoff { int deviation = (int) Math.floor(rand * this.jitter * ms); ms = (((int) Math.floor(rand * 10)) & 1) == 0 ? ms - deviation : ms + deviation; } + if (ms < this.ms) { + // overflow happened + ms = Long.MAX_VALUE; + } return Math.min(ms, this.max); } diff --git a/src/test/java/com/github/nkzawa/backo/BackoffTest.java b/src/test/java/com/github/nkzawa/backo/BackoffTest.java index 66f8734..443a913 100644 --- a/src/test/java/com/github/nkzawa/backo/BackoffTest.java +++ b/src/test/java/com/github/nkzawa/backo/BackoffTest.java @@ -19,4 +19,17 @@ public class BackoffTest { assertTrue(100 == b.duration()); assertTrue(200 == b.duration()); } + + @Test + public void durationOverflow() { + Backoff b = new Backoff(); + b.setMin(100); + b.setMax(10000); + b.setJitter(1.0); + + for (int i = 0; i < 100; i++) { + long duration = b.duration(); + assertTrue(100 <= duration && duration <= 10000); + } + } }