scala - 如何从类型参数实例化 Akka 演员
问题描述
我正在为 Akka 应用程序搭建测试脚手架,我希望将参与者注入测试类:
import akka.actor.ActorSystem
import akka.testkit.{ ImplicitSender, TestActors, TestKit }
import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike }
import akka.actor.Props
import akka.actor.Actor
import akka.event.Logging
import akka.actor.ActorRef
class Simulation[A <: SimulationActor : scala.reflect.ClassTag]
extends TestKit(ActorSystem("AkkaSimulation")) with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {
override def afterAll {
TestKit.shutdownActorSystem(system)
}
val invariantActor1 = system.actorOf(Props(classOf[A1]))
val invariantActor2 = system.actorOf(Props(classOf[A2], invariantActor1))
val actorUnderTest = system.actorOf(Props[SimulationActor]) // how to pass additional argument to Props here?
// test logic here
}
// then elsewhere use the above template:
class Simulation1 extends Simulation[Sim1]
class Simulation2 extends Simulation[Sim2]
class Simulation extends Simulation[Sim3]
// and so on...
我在此设计中遇到以下良性问题:
在提供类型参数 A 时,我在向 传递附加值参数时迷失了方向Props
。找不到适用于这种情况的语法,开始怀疑是否Props
以任何简单的方式启用此用例。以下行需要传递一个ActorRef
参数,因为SimulationActor
需要一个参数,但我找不到通过它的方法。是否有另一种参与者实例化形式,它允许参与者类型的类型参数和参与者构造函数的值参数?
val actorUnderTest = system.actorOf(Props[SimulationActor])
似乎不支持:
val actorUnderTest = system.actorOf(Props[SimulationActor], invariantActor2)
在这个阶段我对介绍 Akka Typed 保持沉默。为了纯粹的优雅,最好不要在启动后更改目标actor以将其值参数作为消息接收,否则这是一种临时的解决方法。
解决方案
推荐阅读
- sql-server - 内存不足,无法完成此操作。请稍后再试,当excel中可能出现更多可用内存错误时
- python-3.x - 函数应该将数据存储到字典中
- webauthn - 如何检测浏览器是否支持 webauthn
- c++ - c++:与 std::cout 一起使用时,constexpr 函数不会在编译时评估
- bash - 等待陷阱后退出
- javascript - 在 facebook 帐户工具包中生成 csrf 令牌
- ios - 截屏当前视图(包括键盘)Swift 4
- linux - Yocto 无法创建新的目标目录
- pandas - Pandas - 将数据框转换为特定格式
- reactjs - 如何将变量设置为上下文提供的值(React)