From b9ff4d48b2a9c21b19bb2484cb82d5082621696e Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Fri, 29 May 2026 19:05:13 +0000 Subject: [PATCH] Add failing test: Fix AdaptiveRetry stopping on Left when shouldPayFailureCost is false --- .../ox/resilience/AdaptiveRetryTest.scala | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 core/src/test/scala/ox/resilience/AdaptiveRetryTest.scala diff --git a/core/src/test/scala/ox/resilience/AdaptiveRetryTest.scala b/core/src/test/scala/ox/resilience/AdaptiveRetryTest.scala new file mode 100644 index 00000000..8ef7a490 --- /dev/null +++ b/core/src/test/scala/ox/resilience/AdaptiveRetryTest.scala @@ -0,0 +1,32 @@ +package ox.resilience + +import org.scalatest.EitherValues +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import ox.scheduling.Schedule + +class AdaptiveRetryTest extends AnyFlatSpec with EitherValues with Matchers: + + behavior of "AdaptiveRetry" + + it should "not pay failureCost if result E is going to be retried and shouldPayFailureCost returns false" in: + // given + var counter = 0 + val errorMessage = "boom" + + def f: Either[String, Int] = + counter += 1 + Left(errorMessage) + + // Bucket smaller than the number of attempts: if the cost were paid, we'd stop early. + val adaptive = AdaptiveRetry(TokenBucket(2), 1, 1) + + // when + val result = adaptive.retryEither(Schedule.immediate.maxRetries(5), _ => false)(f) + + // then + result.left.value shouldBe errorMessage + // 1 initial + 5 retries, all for free since shouldPayFailureCost returns false + counter shouldBe 6 + +end AdaptiveRetryTest