我有以下场景:父级主管演员使用构造函数中传递的工厂(函数)为每条消息创建一个子级。
class supervisorActor(childActorMaker: ActorRefFactory => ActorRef)
extends Actor with ActorLogging{
def receive: Receive = {
case "testThis" =>
val childActor = childActorMaker(context)
childActor!"messageForChild"
}
override val supervisorStrategy =
OneForOneStrategy() {
case _ => Stop
}
}
class childActor extends Actor {
def receive:Receive = {
case _ => /** whatever **/
}
}
在测试中,我覆盖子Receive以强制异常。由于我制定的监督策略,我的期望是每次都要停止儿童演员。
"When the child Actor throws an exception the Supervisor Actor " should " " +
" stop it" in {
val childActorRef = TestActorRef(new childActor() {
override def receive = {
case msg: String => throw new IllegalArgumentException("kaboom")
}
})
watch(childActorRef)
val maker = (_: ActorRefFactory) => childActorRef
val supervisorActorRef = system.actorOf(Props(new supervisorActor(maker)))
supervisorActorRef!"testThis"
expectTerminated(childActorRef, 1.second)
}
由于supervisorStrategy Stop,我希望儿童演员能够停止。 相反,我得到了这个错误:
java.lang.AssertionError: assertion failed: timeout (1 second) during expectMsg: Terminated
知道为什么会这样吗?谢谢
答案 0 :(得分:3)
似乎childActorRef
不是用supervisorActorRef
的上下文创建的(我的意思是你在测试代码中创建的supervisorActor
的ActorContext)。
因此,childActor(childActorRef
)不是测试代码中supervisorActor(supervisorActorRef
)的子代。这就是supervisorActor
的主管策略不能达到目的的原因。