scalatestingakkatestkit

Akka Scala TestKit test PoisonPill message


Given that I have a Supervisor actor which is injected with a child actor how do I send the child a PoisonPill message and test this using TestKit?

Here is my Superivisor.

class Supervisor(child: ActorRef) extends Actor {

  ...
  child ! "hello"
  child ! PoisonPill
}

here is my test code

val probe = TestProbe()
val supervisor = system.actorOf(Props(classOf[Supervisor], probe.ref))
probe.expectMsg("hello")
probe.expectMsg(PoisonPill)

The problem is that the PoisonPill message is not received. Possibly because the probe is terminated by the PoisonPill message?

The assertion fails with

java.lang.AssertionError: assertion failed: timeout (3 seconds) 
during expectMsg while waiting for PoisonPill

Solution

  • I think this Testing Actor Systems should answer your question:

    Watching Other Actors from Probes

    A TestProbe can register itself for DeathWatch of any other actor:

    val probe = TestProbe()
    probe watch target
    target ! PoisonPill
    probe.expectTerminated(target)