How test an actor (has timers, mostly sends messages to itself)


I have an actor that works as some kind of driver for a game.

The actors writes changes to a database on timer trigger, then it creates new timer and so on.

Practically the actors sends messages to itself.

After an error ( a database is down for example ) it should recover itself so it can continue to drive the game as soon as possible.

I want to cover all important scenarios with tests.

What is the best way to test an actor with timers that mostly sends messages to itself?

I’m thinking to pass ActorRef to this actor to act as listener so I can get messages in test scenarios.

Is there any other approach?


A common way to work with both timers and other actors is to make them injectable or overridable so that in the test you can use a close-to-zero timeout or a test probe for receiving messages rather than the actual other actor in the tests.

There’s a few different aspects of testing actors covered in the Testing section of the docs. (I’d recommend you steer clear of the TestActorRef/synchronous testing though it makes for brittle tests)

I’ve generally assumed the timers were reliable (as they’re part of the toolkit) and not tested them. So to test messages triggered by inactivity and timers, I just send the message to the actor, just as I would any other behavior.