Akka:使用Testkit测试预定的演员



我有一个父参与者,它创建了一个子参与者,子参与者每分钟都会向父参与者致意,如下所示

class MasterActor extends Actor with ActorLogging {
override def receive: Receive = {
case "Greet" =>
print("Hey child!!")
case "CreateChild" =>
context.actorOf(Props[ChildActor])
}
}
class ChildActor extends Actor with ActorLogging {
import context.dispatcher
override def preStart(): Unit = {
super.preStart()
context.system.scheduler.schedule(Duration("1 minutes").asInstanceOf[FiniteDuration],
Duration("1 minutes").asInstanceOf[FiniteDuration], context.parent, "Greet")
}
override def receive: Receive = {
case _ =>
print("child receives something")
}
}

我是演员系统的新手,如何使用TestKit测试时间表场景?

我在测试中尝试了下面这样的东西,但不起作用

"Master actor" should {
"receive a Greet message every minute" in {
val probe = TestProbe
val actor = system.actorOf(Props(new Child() {
import context.dispatcher
override def preStart() =
context.system.scheduler.scheduleOnce(Duration("1 seconds").asInstanceOf[FiniteDuration], probe.ref, "Greet")
}))
probe.expectMsg("Greet")
}
}

您可以在akka文档测试中的时序断言部分了解到这一点。within函数应该对您有所帮助。

例如,您可以尝试:

"Master actor" should {
"receive a Greet message every minute" in {
within(62 seconds) {
val probe = TestProbe
val actor = system.actorOf(Props[Child])
probe.expectMsg("Greet")
}
}
}

我想做的是,不要等那么久,在配置中设置延迟超时,并在测试场景中将其更改为几毫秒。

相关内容

  • 没有找到相关文章

最新更新