我正在使用TestKit测试akka系统。我正在测试的系统的一个参与者,在收到某种消息类型后, context.watch 成为发件人,并在发件人去世时自杀:
I'm testing an akka system using TestKit . One actor of the system I'm testing, upon receiving a certain message type, context.watches the sender, and kills itself when the sender dies:
trait Handler extends Actor { override def receive: Receive = { case Init => context.watch(sender) case Terminated => context.stop(self) } }在我的测试中,我m发送
In my test I'm sending
val probe = TestProbe(system) val target = TestActorRef(Props(classOf[Handler])) probe.send(target, Init)现在,测试手表/终止行为-我想模拟被杀死的测试探针。
Now, to test the watch / Terminated behavior - I want to simulate the testprobe being killed.
我可以做
probe.send(target, Terminated)但是,这前提是目标已调用 context.watch(sender),否则它将不会收到终止消息。
But, this presupposes that target has called context.watch(sender) , else it would not receive a Terminated.
我可以做到
probe.testActor ! Kill不会发送 Terminated ,除非目标已正确调用 context.watch(sender),但我实际上并不希望testprobe被杀死,因为它需要保持响应以测试目标(例如)是否继续发送消息而不是停止发送消息。
with doesn't send Terminated unless target has correctly called context.watch(sender) , but I don't actually want the testprobe killed, as it needs to remain responsive to test if (for example) target continues to send messages instead of stopping itself .
我现在遇到过几次,测试演员是否正确处理了上述情况的正确方法是什么?
I'm come across this a few times now, what's the correct way to test if an actor is handling the above situation correctly?
推荐答案您可以通过单独的探针观察被测演员的终止,而不必尝试通过发件人探针进行操作:
You could watch the actor under test for termination with a separate probe instead of trying to do that via the 'sender' probe:
val probe = TestProbe(system) val deathWatcher = TestProbe(system) val target = TestActorRef(Props(classOf[Handler])) deathWatcher.watch(target) probe.send(target, Init) // TODO make sure the message is processed.. perhaps ack it? probe ! Kill deathWatcher.expectTerminated(target)更多推荐
Akka TestProbe测试context.watch()/终止处理
发布评论