Why does TestKit maintain the same ActorSystem between tests?

367 views
Skip to first unread message

Spencer Judge

unread,
Apr 14, 2016, 3:06:21 PM4/14/16
to Akka User List
I started writing some tests using the TestKit, and it's quite nice - but I quickly realized the ActorSystem persists between tests. This seems a bit odd to me, since it introduces potential coupling between the order of test execution and their results. It doesn't feel like what I would expect the default behavior to be.

Is there a particular reason for this? Is the fact that I want to reset my system between each test a smell? I want to do this in my particular test class because I am testing persistence.

Lastly, is there an easy way to change the behavior?
I found this old thread:
https://groups.google.com/forum/embed/#!topic/akka-user/-Cvf9K5P0a0

Which has a solution, albeit a somewhat complicated one. It seems like this would be a very good thing to be able to do simply, without needing to create and destroy a new ActorSystem for every test manually (ex: have a "reset system" method I can call in test teardown). I'm wondering if there's an easier way to do it today.

Thank you!
Spencer

Johan Andrén

unread,
Apr 15, 2016, 2:08:30 PM4/15/16
to Akka User List
Hi Spencer,

ActorSystems are semi-expensive, starting the dispatcher thread pool etc. on startup so this is the rationale for sharing one throughout the test. TestKit is built around this and will require a single actor system throughout the test. As there are no ties to any specific testing framework in the akka testkit there isn't anything shipped with it that hooks up before-and-after style hooks - those would be test framework specific. Most of the tools that make up testkit is however not dependent on TestKit but can be used stand-alone. Starting your actor system is one line of code, and shutting it down is one line (TestKit.shutdownActorSystem(system)) so hooking it into any test framework should not be much work.
For ScalaTest you can read about using BeforeAndAfter here: http://www.scalatest.org/user_guide/sharing_fixtures#beforeAndAfter especially the "Calling loan fixture-fixture methods" section where the ActorSystem would be what you loan into each test case.

In general if you keep your actors loose coupled and keep them from knowing about the absolute paths to other actors tests will be isolated enough even if they share actor system that they can even run simultaneously. Some times you need isolation though, I wouldn't say that in itself is a smell. If no single actor in your entire app can be tested without being isolated it may be something to think twice about though.

I hope this helps!
--
Johan Andrén
Akka Team, Lightbend Inc.

Spencer Judge

unread,
Apr 15, 2016, 5:16:18 PM4/15/16
to Akka User List
Thanks Johan,

That makes sense. I think when I'm not testing persistence it'll be no issue for me to keep the same actor system around. While testing persistence, however, it seems like a good idea to start fresh each time. I suppose if I keep using different actor names each time it'd probably work out just fine still, but it gives me peace of mind to start fresh.

Thanks again!
Spencer

Patrik Nordwall

unread,
Apr 16, 2016, 2:25:28 AM4/16/16
to Akka User List
With persistence you need to clean the data, eg remove files if you use leveldb for testing. We have plenty of tests in the akka code base that does that. There is also community maintained in-mem journal for testing. See http://akka.io/community/

/Patrik
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Spencer Judge

unread,
Apr 16, 2016, 3:27:16 PM4/16/16
to Akka User List
Yup, I'm using the in-mem journal and the FS-based snapshots for my test - deleting them in my code that makes it use a new TestKit fixture for each test has worked out just great. The tests are fairly quick even creating a whole new TestKit every time so seems I've got my problems solved.

Thanks all

You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/z813tWbUL2c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages