Akka Scala TestKit 测试 PoisonPill 消息

Ayu*_*lik 3 testing scala akka testkit

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

这是我的主管。

class Supervisor(child: ActorRef) extends Actor {

  ...
  child ! "hello"
  child ! PoisonPill
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码

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

问题是PoisonPill没有收到消息。可能是因为探测被PoisonPill消息终止了?

断言失败

java.lang.AssertionError: assertion failed: timeout (3 seconds) 
during expectMsg while waiting for PoisonPill
Run Code Online (Sandbox Code Playgroud)

Gov*_*rna 5

我认为这个测试参与者系统应该回答你的问题:

从探测器中观看其他演员

TestProbe 可以将自己注册到任何其他 actor 的 DeathWatch:

val probe = TestProbe()
probe watch target
target ! PoisonPill
probe.expectTerminated(target)
Run Code Online (Sandbox Code Playgroud)