我试图测试演员是否被终止,我知道有一种方法可以测试它是否被终止使用TestProbe expectTerminated
http://doc.akka.io/docs/akka/2.4.16/scala/testing.html#Watching_Other_Actors_from_Probes
但有没有办法测试相反的情况?
谢谢 :)
在我的场景中,我有一个用户actor,它有一个或多个子actor,当它的所有子节点都断开连接(终止)时它将自行终止.
行为工作正常但我的问题是我只能测试没有孩子的场景并终止自己.我无法找到正确的断言来检查它是否仍然有孩子离开时没有终止.
这是一个简化的测试版本:
"Terminates itself if there are no connected clients" in {
val userActor = system.ActorOf(UserActor.props())
userActor ! UserActor.ClientDisconnected()
val deathWatch = TestProbe()
deathWatch.watch(userActor)
deathWatch.expectTerminated(userActor, timeoutDuration)
}
"Not Terminates itself when there are still other clients connected" in {
val userActor = system.ActorOf(UserActor.props())
// Connecting a client
userActor ! UserActor.ClientConnected(sessionId, accessToken)
userActor ! UserActor.ClientDisconnected()
// How can I test that userActor is not terminated?
}
Run Code Online (Sandbox Code Playgroud)
从测试套件中汲取灵感,您可以执行以下操作:
import scala.concurrent.duration._
import akka.testkit._
def expectNoTerminated(target: ActorRef, max: FiniteDuration) {
val o = receiveOne(max.dilated)
if (o ne null)
assert(o != Terminated(target), s"received Terminated message $o")
}
Run Code Online (Sandbox Code Playgroud)
这只会检查您是否收到消息,但它不是Terminated
,因此它可能无法涵盖所有情况。
归档时间: |
|
查看次数: |
381 次 |
最近记录: |