Akka Scala TestKit 测试 PoisonPill 消息

3

假设我有一个被注入了一个子actor的Supervisor actor,我该如何向子actor发送一个PoisonPill消息,并使用TestKit进行测试?

这是我的Supervisor。

class Supervisor(child: ActorRef) extends Actor {

  ...
  child ! "hello"
  child ! PoisonPill
}

这是我的测试代码

val probe = TestProbe()
val supervisor = system.actorOf(Props(classOf[Supervisor], probe.ref))
probe.expectMsg("hello")
probe.expectMsg(PoisonPill)

问题在于未接收到“PoisonPill”消息。 可能是因为探针被“PoisonPill”消息终止了吗?
断言失败。
java.lang.AssertionError: assertion failed: timeout (3 seconds) 
during expectMsg while waiting for PoisonPill
2个回答

5

我认为这篇测试Actor系统应该能回答你的问题:

从探针中观察其他Actor

TestProbe可以注册自己来监视任何其他actor的DeathWatch:

val probe = TestProbe()
probe watch target
target ! PoisonPill
probe.expectTerminated(target)

我曾经使用过这个,但最终更改了我的代码,使得子进程在完成后自行停止。 - Ayub Malik

2
在扩展测试工具包的测试用例中,您可以使用以下代码:
"receives ShutDown" must {
  "sends PosionPill to other actor" in {
    val other = TestProbe("Other")
    val testee = TestActorRef(new Testee(actor.ref))

    testee ! Testee.ShutDown

    watch(other.ref)
    expectTerminated(other.ref)
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接