|
| 1 | +package com.evolutiongaming.akkaeffect.persistence |
| 2 | + |
| 3 | +import akka.actor.Props |
| 4 | +import akka.persistence.journal.AsyncWriteJournal |
| 5 | +import akka.persistence.{AtomicWrite, PersistentRepr} |
| 6 | +import cats.effect.unsafe.implicits.global |
| 7 | +import cats.effect.{IO, Resource} |
| 8 | +import cats.syntax.all.* |
| 9 | +import com.evolutiongaming.akkaeffect.testkit.TestActorSystem |
| 10 | +import com.evolutiongaming.akkaeffect.{Envelope, Receive} |
| 11 | +import com.evolutiongaming.catshelper.LogOf |
| 12 | +import org.scalatest.funsuite.AnyFunSuite |
| 13 | +import org.scalatest.matchers.should.Matchers |
| 14 | + |
| 15 | +import scala.concurrent.Future |
| 16 | +import scala.concurrent.duration.* |
| 17 | +import scala.util.Try |
| 18 | + |
| 19 | +class PersistenceFailureTest extends AnyFunSuite with Matchers { |
| 20 | + |
| 21 | + sealed trait Event extends FailOnEventJournal.Event |
| 22 | + object GoodEvent extends Event { val fail = false } |
| 23 | + object FailEvent extends Event { val fail = true } |
| 24 | + |
| 25 | + val eventSourced = EventSourcedOf.const { |
| 26 | + IO { |
| 27 | + |
| 28 | + val journal = "fail-on-event-journal" |
| 29 | + val snapshot = "inmemory-snapshot-store" |
| 30 | + |
| 31 | + EventSourced( |
| 32 | + eventSourcedId = EventSourcedId("test"), |
| 33 | + pluginIds = PluginIds(journal, snapshot), |
| 34 | + value = RecoveryStarted[Unit] { (_, _) => |
| 35 | + Recovering[Unit] { |
| 36 | + Replay.empty[IO, Event].pure[Resource[IO, *]] |
| 37 | + } { (_, journaller, _) => |
| 38 | + Receive[Envelope[Event]] { envelope => |
| 39 | + // persist event in forked thread thus don't fail actor directly |
| 40 | + journaller.append(Events.of(envelope.msg)).flatten.start.as(false) |
| 41 | + } { |
| 42 | + IO(true) |
| 43 | + }.pure[Resource[IO, *]] |
| 44 | + }.pure[Resource[IO, *]] |
| 45 | + }.typeless( |
| 46 | + sf = _ => IO.unit, |
| 47 | + ef = e => IO(e.asInstanceOf[Event]), |
| 48 | + cf = c => IO(c.asInstanceOf[Event]), |
| 49 | + ).pure[Resource[IO, *]], |
| 50 | + ) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + test("EventSourcedActorOf based actor must fail if journal fails to persist message") { |
| 55 | + |
| 56 | + implicit val log = LogOf.empty[IO] |
| 57 | + |
| 58 | + TestActorSystem[IO]("testing", none) |
| 59 | + .use { system => |
| 60 | + val persistence = EventSourcedPersistence.fromAkkaPlugins[IO](system, 1.second, 100) |
| 61 | + def actor = EventSourcedActorOf.actor[IO, Any, Any](eventSourced, persistence) |
| 62 | + val ref = system.actorOf(Props(actor)) |
| 63 | + def tell = IO(ref ! GoodEvent) |
| 64 | + def fail = IO(ref ! FailEvent) |
| 65 | + def look = IO.fromFuture(IO(system.actorSelection(ref.path).resolveOne(2.seconds))) |
| 66 | + |
| 67 | + for { |
| 68 | + _ <- tell |
| 69 | + _ <- tell |
| 70 | + _ <- IO.sleep(1.second) |
| 71 | + r <- look |
| 72 | + _ = r shouldBe ref |
| 73 | + _ <- fail |
| 74 | + _ <- IO.sleep(1.second) |
| 75 | + r <- look.attempt |
| 76 | + _ = r shouldBe a[Left[_, _]] |
| 77 | + } yield {} |
| 78 | + } |
| 79 | + .unsafeRunSync() |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + test("PersistentActorOf based actor must fail if journal fails to persist message") { |
| 84 | + |
| 85 | + TestActorSystem[IO]("testing", none) |
| 86 | + .use { system => |
| 87 | + def actor = PersistentActorOf[IO](eventSourced) |
| 88 | + val ref = system.actorOf(Props(actor)) |
| 89 | + def tell = IO(ref ! GoodEvent) |
| 90 | + def fail = IO(ref ! FailEvent) |
| 91 | + def look = IO.fromFuture(IO(system.actorSelection(ref.path).resolveOne(2.seconds))) |
| 92 | + |
| 93 | + for { |
| 94 | + _ <- tell |
| 95 | + _ <- tell |
| 96 | + _ <- IO.sleep(1.second) |
| 97 | + r <- look |
| 98 | + _ = r shouldBe ref |
| 99 | + _ <- fail |
| 100 | + _ <- IO.sleep(1.second) |
| 101 | + r <- look.attempt |
| 102 | + _ = r shouldBe a[Left[_, _]] |
| 103 | + } yield {} |
| 104 | + } |
| 105 | + .unsafeRunSync() |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +object FailOnEventJournal { |
| 112 | + val exception = new RuntimeException("test exception") |
| 113 | + |
| 114 | + trait Event { |
| 115 | + def fail: Boolean |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +class FailOnEventJournal extends AsyncWriteJournal { |
| 120 | + |
| 121 | + import scala.concurrent.ExecutionContext.Implicits.global |
| 122 | + import FailOnEventJournal._ |
| 123 | + |
| 124 | + override def asyncReplayMessages(persistenceId: String, fromSequenceNr: Long, toSequenceNr: Long, max: Long)( |
| 125 | + recoveryCallback: PersistentRepr => Unit, |
| 126 | + ): Future[Unit] = Future.successful {} |
| 127 | + |
| 128 | + override def asyncReadHighestSequenceNr(persistenceId: String, fromSequenceNr: Long): Future[Long] = |
| 129 | + Future.successful(42L) |
| 130 | + |
| 131 | + override def asyncWriteMessages(messages: Seq[AtomicWrite]): Future[Seq[Try[Unit]]] = |
| 132 | + Future.traverse(messages.flatMap(_.payload)) { repr => |
| 133 | + repr.payload match { |
| 134 | + case event: Event if event.fail => Future.failed(exception) |
| 135 | + case _ => Future.successful(Try(())) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + override def asyncDeleteMessagesTo(persistenceId: String, toSequenceNr: Long): Future[Unit] = |
| 140 | + Future.successful {} |
| 141 | + |
| 142 | +} |
0 commit comments