How to test Akka actors with virtual time?

416 Views Asked by At

I have a small number of actors (written in Java, using Akka typed APIs), which make use of TimerScheduler to schedule messages to themselves. I'd like to write tests that check their interactions.

When using the ActorTestKit recommended in the documentation, it can execute them using normal timers, which means that when the actor schedules a message to itself in 10 seconds, then the test has to last 10 seconds.

In order to speed up tests, I'd like to use virtual time for the test, where the scheduler in the test does not actually wait but advances a virtual clock instead. Unfortunately, ActorTestKit does not seem to support this concept.

Is there any established pattern for running such tests?

1

There are 1 best solutions below

0
On BEST ANSWER

The ActorTestKit includes ManualTime (in the Java API: akka.actor.testkit.typed.javadsl.ManualTime) which lets you advance the timer by durations (e.g. between assertions). See controlling the Scheduler in the ActorTestKit docs.