I expanded the example from http://doc.akka.io/docs/akka/snapshot/scala/testing.html#Using_Multiple_Probe_Actors .
import akka.actor._ import akka.testkit.{TestProbe, TestKit} import org.scalatest.{Suites, BeforeAndAfter, BeforeAndAfterAll, FlatSpecLike} import scala.concurrent.duration._ class TestProbesTestSuites extends Suites(new TestProbesTest) class TestProbesTest extends TestKit(ActorSystem("TestProbesTestSystem")) with FlatSpecLike with BeforeAndAfterAll with BeforeAndAfter { override def afterAll: Unit = { TestKit.shutdownActorSystem(system) } "A TestProbeTest" should "test TestProbes" in { val actorRef = system.actorOf(Props[TestActor], "TestActor") val tester1 = TestProbe() val tester2 = TestProbe() Thread.sleep(500.milliseconds.toMillis) actorRef ! (tester1.ref, tester2.ref)
I do not think this is a good or good use for a test. When I delete tester1.expectMsg(500.milliseconds, "Hello") , the test fails, so testing tester2 depends on test tester1. In my opinion, this is bad.
Also, changing the line context.system.scheduler.scheduleOnce(400.milliseconds, actorRef1, "Hello")(context.system.dispatcher) to a delay of 100 milliseconds, run the test. Therefore, test message 2 depends on sending message 1. In my opinion, this is also bad.
To solve this problem, I would add Thread.sleep after sending the message and change the wait time for #expectMsg to 0 milliseconds. Thread.sleep is also not suitable for me in tests, but I think it is necessary for testing the actor. Is it correct?
I thought TestProbe was designed to test multiple participants. But for me, the #expectMsg timeout parameter is completely useless when testing multiple participants.
Any comments are welcome.
source share