gpt4 book ai didi

scala - akka:如何测试 Actor 是否被停止

转载 作者:行者123 更新时间:2023-12-04 00:21:00 27 4
gpt4 key购买 nike

我想知道测试 Actor 是否在 akka 中停止的规范方法是什么。这是我目前正在做的一个例子;我担心我把它复杂化了。

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)
}

最佳答案

您可以摆脱单独的观察者 Actor ,而直接从 testProbe 观看目标 Actor 。 Actor :

val testProbe = TestProbe()
testProbe watch myActor
myActor ! MyActor.Stop
testProbe.expectTerminated(myActor)

here对于 Akka 测试文档的相关部分。

关于scala - akka:如何测试 Actor 是否被停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com