If you’re writing a play application and using an actor, chances are you’re going to want to test that they work. This is pretty straight forward, but what happens when the actor itself needs to frobitz a whatsits every bleventy seconds?
To do this you can easily have the scheduler schedule a repeated task.
This is all well and good, but how do you start it? There’s probably a better way to do this, but I stick it in the Play GlobalSettings object.
At this point, it’s obvious that the default parameter for the actor isn’t really needed. You should also note that I stuck in a configurable parameter for the interval. This can be specified in you application.conf, but more likely it will be handy during unit testing:
So instead of running tests forever, you can accelerate the intervals.
But wait! Next week when you have another 400 unit tests on the blahbedy module, your frobitzer is going to be slowing down all those tests (regardless of how elegant your frobitzing algorithms are). So let’s take another swing at the GlobalSettings to see if we can’t smooth this out.
and the Frobitz tests look more like:
Now we have a scheduled actor that only runs when either not in test mode, or the test explicitly requests it to be present. In the event the actor is both scheduled and expecting externally sourced messages, the actor can be initialized in the main section of preStart, and the schedule can be triggered by passing a message. If this is the case, you may find it’s better to pass the interval in the message as opposed to just using a symbol to trigger the heartbeat.
If you enjoyed this post and want to be updated when new ones are published: