From bb839128ab72df6fa4bcff219233bed9f0695e1a Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Thu, 2 Sep 2021 10:28:18 +0200 Subject: [PATCH] Harden NullEmptyStateSpec (#30610) * similar wait for termination as was done for a few others * also improved corresponding test for durable state --- .../typed/scaladsl/NullEmptyStateSpec.scala | 9 ++++-- .../state/scaladsl/NullEmptyStateSpec.scala | 30 +++++++++++-------- .../state/scaladsl/PrimitiveStateSpec.scala | 20 +++++++------ 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/scaladsl/NullEmptyStateSpec.scala b/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/scaladsl/NullEmptyStateSpec.scala index 7aa6e85560..79c34fcb5c 100644 --- a/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/scaladsl/NullEmptyStateSpec.scala +++ b/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/scaladsl/NullEmptyStateSpec.scala @@ -28,7 +28,7 @@ class NullEmptyStateSpec implicit val testSettings: TestKitSettings = TestKitSettings(system) - def primitiveState(persistenceId: PersistenceId, probe: ActorRef[String]): Behavior[String] = + def nullState(persistenceId: PersistenceId, probe: ActorRef[String]): Behavior[String] = EventSourcedBehavior[String, String, String]( persistenceId, emptyState = null, @@ -46,10 +46,10 @@ class NullEmptyStateSpec probe.tell("onRecoveryCompleted:" + state) } - "A typed persistent actor with primitive state" must { + "A typed persistent actor with null empty state" must { "persist events and update state" in { val probe = TestProbe[String]() - val b = primitiveState(PersistenceId.ofUniqueId("a"), probe.ref) + val b = nullState(PersistenceId.ofUniqueId("a"), probe.ref) val ref1 = spawn(b) probe.expectMessage("onRecoveryCompleted:null") ref1 ! "one" @@ -58,6 +58,9 @@ class NullEmptyStateSpec probe.expectMessage("eventHandler:one:two") ref1 ! "stop" + // wait till ref1 stops + probe.expectTerminated(ref1) + val ref2 = testKit.spawn(b) // eventHandler from reply probe.expectMessage("eventHandler:null:one") diff --git a/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/NullEmptyStateSpec.scala b/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/NullEmptyStateSpec.scala index ab632bdf49..da3f7bf1e4 100644 --- a/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/NullEmptyStateSpec.scala +++ b/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/NullEmptyStateSpec.scala @@ -33,28 +33,34 @@ class NullEmptyStateSpec implicit val testSettings: TestKitSettings = TestKitSettings(system) - def primitiveState(persistenceId: PersistenceId, probe: ActorRef[String]): Behavior[String] = - DurableStateBehavior[String, String](persistenceId, emptyState = null, commandHandler = (_, command) => { - if (command == "stop") - Effect.stop() - else - Effect.persist(command).thenReply(probe)(_ => command) - }) + def nullState(persistenceId: PersistenceId, probe: ActorRef[String]): Behavior[String] = + DurableStateBehavior[String, String]( + persistenceId, + emptyState = null, + commandHandler = (state, command) => { + if (command == "stop") + Effect.stop() + else if (state == null) + Effect.persist(command).thenReply(probe)(newState => newState) + else + Effect.persist(s"$state:$command").thenReply(probe)(newState => newState) + }) - "A typed persistent actor with primitive state" must { + "A typed persistent actor with null empty state" must { "persist and update state" in { val probe = TestProbe[String]() - val b = primitiveState(PersistenceId.ofUniqueId("a"), probe.ref) + val b = nullState(PersistenceId.ofUniqueId("a"), probe.ref) val ref1 = spawn(b) ref1 ! "one" probe.expectMessage("one") ref1 ! "two" - probe.expectMessage("two") + probe.expectMessage("one:two") ref1 ! "stop" probe.expectTerminated(ref1) - val _ = spawn(b) - probe.expectNoMessage() + val ref2 = spawn(b) + ref2 ! "three" + probe.expectMessage("one:two:three") } } } diff --git a/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/PrimitiveStateSpec.scala b/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/PrimitiveStateSpec.scala index b2c4f0ecfd..dd4b533845 100644 --- a/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/PrimitiveStateSpec.scala +++ b/akka-persistence-typed-tests/src/test/scala/akka/persistence/typed/state/scaladsl/PrimitiveStateSpec.scala @@ -27,12 +27,15 @@ class PrimitiveStateSpec with LogCapturing { def primitiveState(persistenceId: PersistenceId, probe: ActorRef[String]): Behavior[Int] = - DurableStateBehavior[Int, Int](persistenceId, emptyState = 0, commandHandler = (_, command) => { - if (command < 0) - Effect.stop() - else - Effect.persist(command).thenReply(probe)(_ => command.toString) - }) + DurableStateBehavior[Int, Int]( + persistenceId, + emptyState = 0, + commandHandler = (state, command) => { + if (command < 0) + Effect.stop() + else + Effect.persist(state + command).thenReply(probe)(newState => newState.toString) + }) "A typed persistent actor with primitive state" must { "persist primitive state and update" in { @@ -42,15 +45,14 @@ class PrimitiveStateSpec ref1 ! 1 probe.expectMessage("1") ref1 ! 2 - probe.expectMessage("2") + probe.expectMessage("3") ref1 ! -1 probe.expectTerminated(ref1) val ref2 = spawn(b) - probe.expectNoMessage() ref2 ! 3 - probe.expectMessage("3") + probe.expectMessage("6") } } }