From 08e9f9b9e73ddd5f9c86bf8b32b8f882d3dcd309 Mon Sep 17 00:00:00 2001 From: odisseus Date: Wed, 13 Nov 2024 12:58:07 +0100 Subject: [PATCH] New test case for retry.When --- retry/src/main/scala/Policy.scala | 4 ++-- retry/src/test/scala/PolicySpec.scala | 28 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/retry/src/main/scala/Policy.scala b/retry/src/main/scala/Policy.scala index 6fa4d5d..3608afc 100644 --- a/retry/src/main/scala/Policy.scala +++ b/retry/src/main/scala/Policy.scala @@ -197,8 +197,8 @@ object Backoff { } } -/** A retry policy in which the a failure determines the way a future should be retried. The partial function provided - * may define the domain of both the success OR exceptional failure of a future fails explicitly. +/** A retry policy in which the type of failure determines the way a future should be retried. The partial function + * provided may define the domain of both the success OR exceptional failure of a future fails explicitly. * * {{{ * val policy = retry.When { diff --git a/retry/src/test/scala/PolicySpec.scala b/retry/src/test/scala/PolicySpec.scala index 6dd8c69..f5d7582 100644 --- a/retry/src/test/scala/PolicySpec.scala +++ b/retry/src/test/scala/PolicySpec.scala @@ -607,5 +607,33 @@ abstract class PolicySpec extends AsyncFunSpec with BeforeAndAfterAll { assert(retried.get() == 1) } } + + it("should stop repeating when the failure changes") { + implicit val success = Success[Boolean](identity) + object RecoverableException extends RuntimeException("recoverable exception") + object FatalException extends RuntimeException("fatal exception") + val retried = new AtomicInteger() + val retriedUntilFailure = 5 + def run() = { + println(retried.get()) + if (retried.getAndIncrement() < retriedUntilFailure) { + Future.failed(RecoverableException) + } else { + if(retried.get() > 2 * retriedUntilFailure) { + // this state should never be reached + Future(true) + } else Future.failed(FatalException) + } + } + + val policy = When { + case FatalException => throw FatalException + case _ => Directly.forever + } + policy(run()).failed.map { t => + assert(t.getMessage === "fatal exception") + assert(retried.get() == 5) + } + } } }