Logging all messages sent to TestKrobe Akka TestKit

I am trying to register all messages received by TestKit TestProbe, which is somewhat complicated. I know the " debug.receive " section in the documents, which says that you should use the debug.receive option in combination with the LogginReceive block. This, however, does not work when I do not control the implementation of the actor.

The only idea I had was to subclass akka.testkit.TestActor to use LoggingReceive and then subclass TestKit to instantiate my TestActor subclass TestActor , but that didn't work because most functions are private in the akka namespace ( and not in vain, I suppose).

+8
scala akka
source share
3 answers

Sorry, at first your question was a bit wrong, so here is my approach.

Create a wrapper actor that logs messages:

 class LoggingActor(fac: => Actor) extends Actor { val underlying = context.system.actorOf(Props(fac)) def receive = { LoggingReceive { case x ⇒ underlying.tell(x, sender) } } } 

and then just create a TestActorRef with your actor wrapped in a LoggingActor :

  val echo = TestActorRef(new LoggingActor(new FooActor)) echo ! "hello world" 
+4
source share

There is (perhaps surprisingly) a simple answer:

 probe.setAutoPilot(new TestActor.AutoPilot { def run(sender: ActorRef, msg: Any) = { log.debug("whatever") this } }) 
+11
source share

Using Ronald's answer, I wrote this to have an easier way to define my probes:

 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 } } 

After that, I define my probes using LoggingTestProbe() instead of TestProbe() .

I am new to Scala, so this may not be optimal, but works fine for me.

+5
source share

All Articles