记录发送到Akka TestKit TestProbe的所有消息

fre*_*oma 8 scala akka testkit

我正在尝试记录TestKit TestProbe 收到的所有消息,这被证明有点困难.我知道文档中的Actor Logging部分,它说应该将该debug.receive选项与LogginReceive块结合使用.然而,当我无法控制演员的实现时,这不起作用.

我唯一的想法是继承akka.testkit.TestActor使用a LoggingReceive和子类TestKit来使它创建我的TestActor子类的实例,但是这不起作用,因为大多数功能都是akka命名空间的私有(并且有充分的理由,我想).

Rol*_*uhn 14

有一个(可能令人惊讶的)简单答案:

probe.setAutoPilot(new TestActor.AutoPilot {
  def run(sender: ActorRef, msg: Any) = {
    log.debug("whatever")
    this
  }
})
Run Code Online (Sandbox Code Playgroud)


mar*_*cob 5

使用Ronald的答案我写这个有一个更简单的方法来定义我的探针:

object LoggingTestProbe {
  def apply()(implicit system: ActorSystem) : TestProbe = {
    val probe = TestProbe()
    probe.setAutoPilot(new TestActor.AutoPilot {
      def run(sender: ActorRef, msg: Any) = {
        val other = sender.path
        val me = probe.ref.path
        system.log.debug(s"$me received $msg from $other")
        this
      }
    })
    probe
  }
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我用LoggingTestProbe()而不是定义我的探针TestProbe().

我是Scala的新手,所以这可能不是最佳选择,但对我来说非常有用.


dre*_*xin 4

抱歉,一开始你的问题有点错误,所以这是我的方法。

\n\n

创建一个包装器参与者,用于记录消息:

\n\n
class LoggingActor(fac: => Actor) extends Actor {\n\n  val underlying = context.system.actorOf(Props(fac))\n\n  def receive = {\n    LoggingReceive {\n      case x \xe2\x87\x92 underlying.tell(x, sender)\n    }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后将TestActorRef你的演员包裹在以下内容中创建你的LoggingActor

\n\n
  val echo = TestActorRef(new LoggingActor(new FooActor))\n\n  echo ! "hello world"\n
Run Code Online (Sandbox Code Playgroud)\n