我的应用程序使用内部初始化其他演员(孩子)的主管 akka 演员。但是,由于它是异步执行的,因此我在尝试使用 Akka TestKit 编写测试时遇到了问题。
例如,当尝试通过向主管的子actor发送消息来测试系统如何从另一个系统终止时,我创建了一个新的actor系统,然后使用actorOf一个接收配置的主管(用于创建孩子主管内部的演员),如下所示:
val anotherSystem: ActorSystem = ActorSystem("anotherSystem")
anotherSystem.actorOf(Props(classOf[Supervisor], new ConfigurationFromStatic(configsAnother), metrics), "AnotherSupervisor")
然后,如果我尝试使用actorSelection向子actor发送消息,AnotherSupervisor
它会失败,因为子actor还不是selectable
:
anotherSystem.actorSelection("/user/AnotherSupervisor/AnotherManagementReceiver") ! message
expectMsg(Ack)
失败,因为还没有AnotherManagementReceiver
创建。
使用Thread.sleep(5000)
作品,但这太糟糕了。
在寻找可能的解决方案后,我测试了:
anotherSystem.actorSelection("/user/AnotherSupervisor/AnotherManagementReceiver").resolveOne() ! message
也因现有的演员消息而失败。
还尝试了 EventFilter 来让子角色在日志中回显一些内容:
EventFilter.info("Management consumer started", occurrences = 1) intercept {
anotherSystem.actorOf(Props(classOf[Supervisor], new ConfigurationFromStatic(configsAnother), metrics), "AnotherSupervisor")
}
这一直等到超时(我什至看到了日志消息),但我猜 EventFilter 只是从 TestKit 创建的“主要”演员系统中读取。
关于如何处理这种情况的任何想法或建议?