0

我有一个父演员,它创建了一个子演员,并且子演员每分钟都会向父母打招呼,如下所示

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

1 回答 1

1

您可以在 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")
  }
 }
}

我会做的,不要等待这么久,将延迟超时放入配置中,并在测试场景中将其更改为几毫秒。

于 2020-09-03T05:53:05.430 回答