From 508dd44f8d1227861ccee82263bb4adbcd4e8544 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 7 Dec 2020 13:01:58 +0100 Subject: [PATCH] logging of retries eventsByTag --- .../akka/persistence/cassandra/Retries.scala | 3 ++- .../persistence/cassandra/RetriesSpec.scala | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/core/src/main/scala/akka/persistence/cassandra/Retries.scala b/core/src/main/scala/akka/persistence/cassandra/Retries.scala index ea5eb2869..4bb2795c7 100644 --- a/core/src/main/scala/akka/persistence/cassandra/Retries.scala +++ b/core/src/main/scala/akka/persistence/cassandra/Retries.scala @@ -47,8 +47,9 @@ private[cassandra] object Retries { if (maxAttempts == -1 || maxAttempts - attempted != 1) { tryAttempt().recoverWith { - case NonFatal(_) => + case NonFatal(exc) => val nextDelay = BackoffSupervisor.calculateDelay(attempted, minBackoff, maxBackoff, randomFactor) + onFailure(attempted + 1, exc, nextDelay) after(nextDelay, scheduler) { retry(attempt, maxAttempts, onFailure, minBackoff, maxBackoff, randomFactor, attempted + 1) } diff --git a/core/src/test/scala/akka/persistence/cassandra/RetriesSpec.scala b/core/src/test/scala/akka/persistence/cassandra/RetriesSpec.scala index 80d01cfc0..be88d4dc1 100644 --- a/core/src/test/scala/akka/persistence/cassandra/RetriesSpec.scala +++ b/core/src/test/scala/akka/persistence/cassandra/RetriesSpec.scala @@ -4,17 +4,17 @@ package akka.persistence.cassandra +import scala.concurrent.Future +import scala.concurrent.duration._ + import akka.actor.ActorSystem import akka.testkit.TestKit -import akka.util.ConstantFun +import akka.testkit.TestProbe import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike -import scala.concurrent.Future -import scala.concurrent.duration.DurationInt - class RetriesSpec extends TestKit(ActorSystem("RetriesSpec")) with AnyWordSpecLike @@ -25,15 +25,20 @@ class RetriesSpec implicit val ec = system.dispatcher "Retries" should { "retry N number of times" in { + val failProbe = TestProbe() @volatile var called = 0 - Retries + val result = Retries .retry(() => { called += 1 - Future.failed(new RuntimeException("cats")) - }, 3, ConstantFun.scalaAnyThreeToUnit, 1.milli, 2.millis, 0.1) + Future.failed(new RuntimeException(s"cats $called")) + }, 3, (_, exc, _) => failProbe.ref ! exc, 1.milli, 2.millis, 0.1) .failed .futureValue called shouldEqual 3 + result.getMessage shouldEqual "cats 3" + failProbe.expectMsgType[RuntimeException].getMessage shouldEqual "cats 1" + failProbe.expectMsgType[RuntimeException].getMessage shouldEqual "cats 2" + failProbe.expectNoMessage() } }