[英]akka: how to test that an actor was stopped
I am wondering what the canonical way is of testing whether an actor has stopped in akka. 我想知道测试演员是否已经停止在akka的规范方式是什么。 Here is an example of how I am currently doing;
这是我目前正在做的一个例子; I'm worried I'm over complicating it.
我担心我会让它变得复杂。
import akka.actor.{Terminated, Actor, Props, ActorSystem}
import akka.testkit.TestProbe
class MyActor extends Actor {
import MyActor._
override def receive: Receive = {
case Stop => context.stop(self)
}
}
object MyActor {
def props = Props(new MyActor)
case object Stop
}
object MyActorSpec {
val system = ActorSystem()
val myActor = system.actorOf(MyActor.props)
val testProbe = TestProbe()
case object MyActorStopped
val watcher = system.actorOf(Props(new Actor {
context.watch(myActor)
override def receive: Actor.Receive = {
case Terminated(`myActor`) => testProbe.ref ! MyActorStopped
}
}))
myActor ! MyActor.Stop
testProbe.expectMsg(MyActorStopped)
}
You can get rid of the separate watcher actor, and just watch the target actor directly from the testProbe
actor: 你可以摆脱单独的观察者角色,直接从
testProbe
actor中观察目标actor:
val testProbe = TestProbe()
testProbe watch myActor
myActor ! MyActor.Stop
testProbe.expectTerminated(myActor)
See here for the relevant section of the Akka testing docs. 请参阅此处了解Akka测试文档的相关部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.