Akka Testkit expectNoMsg() timeout

I am using Akka Testkit in a project, and I’ve always used expectNoMsg() with the default timeout of three seconds. However, another developer says that we should be using expectNoMsg(0.seconds) instead so that our tests don’t take longer and longer to run as we add more of these asserts. I agree with the goal but am unsure about whether it’s safe to use a 0 second timeout.

The rational about using 0 seconds is that since we are invoking expectNoMsg on a TestActorRef, and the ! function to/in a TestActorRef is a synchronous operation, waiting for 0 seconds is enough. I tested with a couple of my test cases and found that the tests did fail if an unexpected message is sent. But is that reliable? Should we adopt “expectNoMsg(0.seconds)” as a testing best practice?

Please let me know if there are any subtleties or caveats to be aware of.


I’d argue best practice testing would be to avoid TestActorRef completely, extract synchronous logic to methods you can write normal unit tests for and do asynchronous testing for the async behaviors of the actor.

If what you want to verify is that a message isn’t sent in a particular message handling in the actor, then since TestActorRef uses the calling thread to execute the actor logic and the testprobe is basically just a queue (the inbox of the probe), passing a 0 to expectNoMessage becomes just checking if that queue is empty at that given point and should be fine.