0

我有以下场景:父主管actor使用构造函数中传递的工厂(函数)为每条消息创建一个子节点。

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

知道为什么会这样吗?谢谢

4

1 回答 1

3

似乎childActorRef不是使用supervisorActorRef' 上下文创建的(我的意思是supervisorActor您在测试代码中创建的 ActorContext )。所以在你的测试代码中childActor( childActorRef) 不是 supervisorActor( ) 的孩子。supervisorActorRef这就是为什么主管策略supervisorActor不能达到目的的原因。

于 2016-01-30T19:10:01.457 回答