Akka: Integration Tests for Single Node Cluster Sharding
Sep 3, 2015
Multi-JVM tests are the default way of testing an Akka cluster sharding application. But sometimes you just need the sharding functionality where multi-JVM tests are just overkill. I didn’t find any sample or guide describing the best practices for this situation.
I just found out that it’s not a bad idea to use Akka cluster sharding even if you don’t need the real clustering features. But if you follow the default recommended way of integration tests you will ended up with the multi-jvm tests which are rather slow and complicated.
Another issue with the cluster sharding tests is the persistence. You have to use in-memory persistence plug-in or tests must not be run in parallel.
I’ve build a simple test kit to be used as a Specs2 scope:
IsolatedCluster class provides an isolated actor system cluster which makes tests easier. It also removes the persistence files.
As you can see TestActorSystemManager is the responsible for building the actor system and setting up cluster sharding with a single node:
Note that I didn’t use in-memory persistence plugin. Instead, the path of snapshots and journal stores are set randomly which allows tests to be run in parallel while I keep the production persistence plugin.
This made the tests over 400% faster.
And here is the integration test:
Everything is like the default cluster sharding samples except that we have a single node. This way we can start simple and increase the node numbers and even scale-out the system, when necessary.