-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ox.retry | ||
|
||
import org.scalatest.Inspectors | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.concurrent.duration.* | ||
|
||
class JitterTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Jitter" | ||
|
||
private val basePolicy = RetryPolicy.Backoff(maxRetries = 3, initialDelay = 100.millis) | ||
|
||
it should "use no jitter" in { | ||
// given | ||
val policy = basePolicy | ||
|
||
// when | ||
val delays = (1 to 5).map(policy.nextDelay(_, None)) | ||
|
||
// then | ||
delays should contain theSameElementsInOrderAs Seq(200, 400, 800, 1600, 3200).map(_.millis) | ||
} | ||
|
||
it should "use full jitter" in { | ||
// given | ||
val policy = basePolicy.copy(jitter = Jitter.Full) | ||
|
||
// when | ||
val delays = (1 to 5).map(policy.nextDelay(_, None)) | ||
|
||
// then | ||
Inspectors.forEvery(delays.zipWithIndex) { case (delay, i) => | ||
val backoffDelay = RetryPolicy.Backoff.delay(i + 1, policy.initialDelay, policy.maxDelay) | ||
delay should (be >= 0.millis and be <= backoffDelay) | ||
} | ||
} | ||
|
||
it should "use equal jitter" in { | ||
// given | ||
val policy = basePolicy.copy(jitter = Jitter.Equal) | ||
|
||
// when | ||
val delays = (1 to 5).map(policy.nextDelay(_, None)) | ||
|
||
// then | ||
Inspectors.forEvery(delays.zipWithIndex) { case (delay, i) => | ||
val backoffDelay = RetryPolicy.Backoff.delay(i + 1, policy.initialDelay, policy.maxDelay) | ||
delay should (be >= backoffDelay / 2 and be <= backoffDelay) | ||
} | ||
} | ||
|
||
it should "use decorrelated jitter" in { | ||
// given | ||
val policy = basePolicy.copy(jitter = Jitter.Decorrelated) | ||
|
||
// when | ||
val delays = (1 to 5).map(policy.nextDelay(_, None)) | ||
|
||
// then | ||
Inspectors.forEvery(delays.sliding(2).map(_.toList).toList) { | ||
case List(previousDelay, delay) => | ||
delay should (be >= policy.initialDelay and be <= previousDelay * 3) | ||
case _ => succeed // so that the match is exhaustive | ||
} | ||
} | ||
} |