From 741775f3c1d8ede8cc1b97de4d24d4dc5eef4f80 Mon Sep 17 00:00:00 2001 From: Diego Pineda Date: Tue, 30 Aug 2022 15:02:31 -0400 Subject: [PATCH 1/2] Added limitRetriersWithFixedDelay which is implemented in the docs --- .../shared/src/main/scala/retry/RetryPolicies.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/modules/core/shared/src/main/scala/retry/RetryPolicies.scala b/modules/core/shared/src/main/scala/retry/RetryPolicies.scala index 59e32aa4..897b72e5 100644 --- a/modules/core/shared/src/main/scala/retry/RetryPolicies.scala +++ b/modules/core/shared/src/main/scala/retry/RetryPolicies.scala @@ -72,6 +72,16 @@ object RetryPolicies { show"limitRetries(maxRetries=$maxRetries)" ) + /** Retry up to maxRetries with fixed delay between retries + */ + def limitRetriesWithFixedDelay[M[_]: Applicative]( + maxRetries: Int, + delay: FiniteDuration + ) = + RetryPolicies + .limitRetries[M](maxRetries) + .join(RetryPolicies.constantDelay(delay)) + /** Delay(n) = Delay(n - 2) + Delay(n - 1) * * e.g. if `baseDelay` is 10 milliseconds, the delays before each retry will be From 9fb99a478fb19badb1d7e597f3f0bc992c4f0acf Mon Sep 17 00:00:00 2001 From: Diego Pineda Date: Tue, 30 Aug 2022 16:17:24 -0400 Subject: [PATCH 2/2] added tests --- .../src/main/scala/retry/RetryPolicies.scala | 2 +- .../test/scala/retry/RetryPoliciesSpec.scala | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/modules/core/shared/src/main/scala/retry/RetryPolicies.scala b/modules/core/shared/src/main/scala/retry/RetryPolicies.scala index 897b72e5..8c063a8b 100644 --- a/modules/core/shared/src/main/scala/retry/RetryPolicies.scala +++ b/modules/core/shared/src/main/scala/retry/RetryPolicies.scala @@ -77,7 +77,7 @@ object RetryPolicies { def limitRetriesWithFixedDelay[M[_]: Applicative]( maxRetries: Int, delay: FiniteDuration - ) = + ): RetryPolicy[M] = RetryPolicies .limitRetries[M](maxRetries) .join(RetryPolicies.constantDelay(delay)) diff --git a/modules/core/shared/src/test/scala/retry/RetryPoliciesSpec.scala b/modules/core/shared/src/test/scala/retry/RetryPoliciesSpec.scala index b4dfc5c0..ba79f4df 100644 --- a/modules/core/shared/src/test/scala/retry/RetryPoliciesSpec.scala +++ b/modules/core/shared/src/test/scala/retry/RetryPoliciesSpec.scala @@ -49,6 +49,13 @@ class RetryPoliciesSpec extends AnyFlatSpec with Checkers { s"exponentialBackoff($baseDelay)" ) ), + for { + delay <- genFiniteDuration + maxRetries <- Gen.posNum[Int] + } yield LabelledRetryPolicy( + limitRetriesWithFixedDelay(maxRetries, delay), + s"limitRetriesWithFixedDelay($maxRetries, $delay)" + ), Gen .posNum[Int] .map(maxRetries => @@ -198,6 +205,22 @@ class RetryPoliciesSpec extends AnyFlatSpec with Checkers { } } + behavior of "limitRetriesWithFixedDelay" + + it should "always retry with the same delay or give up after maxRetries is reached" in check { + (status: RetryStatus) => + val limit = 500 + val verdict = + limitRetriesWithFixedDelay(limit, 1.millisecond).decideNextRetry( + status + ) + if (status.retriesSoFar < limit) { + verdict == PolicyDecision.DelayAndRetry(1.millisecond) + } else { + verdict == PolicyDecision.GiveUp + } + } + behavior of "limitRetriesByDelay" it should "give up if the underlying policy chooses a delay greater than the threshold" in {