@@ -3,6 +3,7 @@ package lerna.akka.entityreplication.typed.internal.testkit
33import akka .actor .testkit .typed .scaladsl .{ ActorTestKit , SerializationTestKit }
44import akka .actor .typed .{ ActorRef , Behavior }
55import akka .actor .typed .scaladsl .adapter ._
6+ import lerna .akka .entityreplication .ReplicationRegion
67import lerna .akka .entityreplication .model .NormalizedEntityId
78import lerna .akka .entityreplication .raft .RaftProtocol
89import lerna .akka .entityreplication .raft .model .{ EntityEvent , NoOp , ReplicatedLog , Term }
@@ -11,6 +12,7 @@ import lerna.akka.entityreplication.typed.ClusterReplication.ShardCommand
1112import lerna .akka .entityreplication .typed .{ ReplicatedEntityContext , ReplicatedEntityTypeKey }
1213import lerna .akka .entityreplication .typed .testkit .ReplicatedEntityBehaviorTestKit
1314
15+ import scala .annotation .tailrec
1416import scala .util .control .NonFatal
1517
1618private [entityreplication] class ReplicatedEntityBehaviorTestKitImpl [Command , Event , State ](
@@ -103,18 +105,22 @@ private[entityreplication] class ReplicatedEntityBehaviorTestKitImpl[Command, Ev
103105 }
104106
105107 private [this ] def handleReplicatedEvent (): Option [Event ] = {
106- val shardCommand : Option [ShardCommand ] =
107- try {
108- Option (shardProbe.receiveMessage())
109- } catch {
110- case NonFatal (_) => None // It wasn't replicated until the timeout
111- }
112- val replicateCommand =
108+ @ tailrec
109+ def findReplicateCommand (): Option [RaftProtocol .Replicate ] = {
110+ val shardCommand : Option [ShardCommand ] =
111+ try {
112+ Option (shardProbe.receiveMessage())
113+ } catch {
114+ case NonFatal (_) => None // It wasn't replicated until the timeout
115+ }
113116 shardCommand match {
114- case Some (r : RaftProtocol .Replicate ) => Option (r)
115- case Some (m) => throw new IllegalStateException (s " Shard received unexpected message [ $m] " )
116- case None => None
117+ case Some (r : RaftProtocol .Replicate ) => Option (r)
118+ case Some (r : ReplicationRegion .Passivate ) => findReplicateCommand() // retry
119+ case Some (m) => throw new IllegalStateException (s " Shard received unexpected message [ $m] " )
120+ case None => None
117121 }
122+ }
123+ val replicateCommand = findReplicateCommand()
118124 replicateCommand.foreach { replicate =>
119125 // side effects
120126 replicatedLog = replicatedLog.append(EntityEvent (Option (normalizedEntityId), replicate.event), term)
0 commit comments