Wednesday, March 19, 2014

Testing in Akka: sneaky automatic restarts

Restarts are awesome when stuff fails and you want it to work. Akka does this by default for every actor, and that's great in production. In testing, we're looking for failure. We want to see it, not hide it. In testing, it's sneaky of Akka to restart our whole actor hierarchy when it barfs.

Change this sneaky behavior in actor system configuration by altering the supervisor strategy of the guardian actor. The guardian is the parent of any actor created with system.actorOf(...), so its supervision strategy determines what happens when your whole hierarchy goes down.

Then, in your test's assertions, make sure your actor hasn't been terminated. If the guardian has a StoppingSupervisionStrategy and your actor goes down, it'll stay down.

Code example in this gist.


  1. I would actually recommend using the StoppingStrategy in production and having the whole application dangle off a single top-level actor with an appropriate supervisorStrategy. system.actorOf() should be considered an anti-pattern except for the first such actor.

    1. That makes sense. We learned that recently too, that context.system.actorOf(...) is never what we meant!