Akka TestProbe测试context.watch()/终止处理

编程入门 行业动态 更新时间:2024-10-24 01:54:16
本文介绍了Akka TestProbe测试context.watch()/终止处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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()/终止处理

本文发布于:2023-11-25 12:05:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   TestProbe   Akka   context   watch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!