Monday, July 21, 2014

Testing akka actor termination

When testing akka code, I want to make sure a particular actor gets shut down within a time limit. I used to do it like this:


That isTerminated method is deprecated since Akka 2.2, and good thing too, since my test was wasting everyone's time. Today I'm doing this instead:

import akka.testkit.TestProbe

val probe = new TestProbe(actorSystem)
probe.expectMsgPF(2.seconds){ case Terminated(actorRef) => true }

This says: set up a TestProbe actor, and have it watch the actorRef of interest. Wait for the TestProbe to receive notification that the actor of interest has been terminated. If actorRef has already terminated, that message will come right away. My test doesn't have to wait the maximum allowed time.[1]

This works in any old test method with access to the actorSystem -- I don't have to extend akka.testkit.TestKit to use the TestProbe.

BONUS: In a property-based test, I don't want to throw an exception, but rather return a result, a property with a nice label. In that case my function gets a little weirder:

def shutsDown(actorSystem: ActorSystem, 
              actorRef: ActorRef): Prop = {
  val maxWait = 2.seconds
  val probe = new TestProbe(actorSystem)
  try {
   probe.expectMsgPF(maxWait){case Terminated(actorRef) => true }
  } catch { 
   case ae: AssertionError => 
    false :| s"actor not terminated within $maxWait"

[1] This is still blocking the thread until the Terminated message is received or the timeout expires. I eagerly await the day when test methods can return a Future[TestResult].

No comments:

Post a Comment