Harden NullEmptyStateSpec (#30610)

* similar wait for termination as was done for a few others
* also improved corresponding test for durable state
This commit is contained in:
Patrik Nordwall 2021-09-02 10:28:18 +02:00 committed by GitHub
parent 7590c976e6
commit bb839128ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 24 deletions

View file

@ -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")

View file

@ -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")
}
}
}

View file

@ -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")
}
}
}