3

鉴于我有一个Supervisor注入了一个child演员的演员,我如何向孩子发送 PoisonPill 消息并使用 TestKit 对其进行测试?

这是我的主管。

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
4

2 回答 2

5

我认为这个测试演员系统应该回答你的问题:

从 Probes 观看其他演员

TestProbe 可以为任何其他参与者的 DeathWatch 注册自己:

val probe = TestProbe()
probe watch target
target ! PoisonPill
probe.expectTerminated(target)
于 2016-01-26T17:20:15.800 回答
2

在扩展了 testkit 的测试用例中,您可以使用以下代码:

"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)
  }
}
于 2017-05-03T11:03:25.130 回答