阿卡 - 测试监督策略

时间:2016-01-28 12:58:32

标签: scala akka akka-testkit akka-supervision

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

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

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

1 个答案:

答案 0 :(得分:3)

似乎childActorRef不是用supervisorActorRef的上下文创建的(我的意思是你在测试代码中创建的supervisorActor的ActorContext)。 因此,childActor(childActorRef)不是测试代码中supervisorActor(supervisorActorRef)的子代。这就是supervisorActor的主管策略不能达到目的的原因。