我从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)
// When you comment the next line the test fails
tester1.expectMsg(500.milliseconds, "Hello")
tester2.expectMsg(500.milliseconds, "Hello")
// Alternative test
// Thread.sleep(500.milliseconds.toMillis)
// tester1.expectMsg(0.milliseconds, "Hello")
// tester2.expectMsg(0.milliseconds, "Hello")
()
}
}
class TestActor extends Actor with ActorLogging {
override def receive: Receive = {
case (actorRef1: ActorRef, actorRef2: ActorRef) => {
// When you change the schedule time in the next line to 100.milliseconds the test fails
context.system.scheduler.scheduleOnce(400.milliseconds, actorRef1, "Hello")(context.system.dispatcher)
context.system.scheduler.scheduleOnce(800.milliseconds, actorRef2, "Hello")(context.system.dispatcher)
}
case x => log.warning(x.toString)
}
}
我不认为这是一个正确或好的测试用法。当我删除tester1.expectMsg(500.milliseconds, "Hello")
测试失败时,tester2 的测试取决于测试 tester1。在我看来这很糟糕。
还将线路更改context.system.scheduler.scheduleOnce(400.milliseconds, actorRef1, "Hello")(context.system.dispatcher)
为 100 毫秒的延迟会使测试失败。所以测试消息 2 取决于发送消息 1。在我看来这也很糟糕。
为了解决这个问题,我将在发送消息后添加一个 Thread.sleep 并将 #expectMsg 的等待时间更改为 0 毫秒。Thread.sleep 在测试中也不适合我,但我认为在演员测试中它是必须的。这是正确的方法吗?
我认为 TestProbe 是为测试多个参与者而设计的。但对我来说,在测试多个参与者时,#expectMsg 的等待时间参数是毫无用处的。
欢迎任何意见。