等到主管准备好后再执行测试

Wait until the supervisor is ready before execute the test

我已经为我的演员写了一个测试如下:

class DetectorSpec extends BddSpec {

    private val sap = new SapMock()
        .withExposedPorts(8080)
        .waitingFor(Wait.forHttp("/"))

    private val kafka = new KafkaContainer("5.2.1")


    sap.start()
    kafka.start()


    override def afterAll(): Unit = {
        sap.stop()
        kafka.stop()
    }

    private def withKafkaOfflineSapOnline(testCode: TestProbe[ServerEvent] => Unit)
    : Unit = {

        val config = ConfigFactory.parseString(
        s"""
            kafka {
                servers = "127.0.0.1:9092"
            }
            sap {
                server = "ws://${sap.getContainerIpAddress}:${sap.getMappedPort(8080)}"
            }""")

        val testKit = ActorTestKit("testSystem1", config)
        val inbox = testKit.createTestProbe[ServerEvent]("Receiver")
        testKit.spawn(DetectorSupervisor.create(), "DetectorSupervisor")
        testKit.system.receptionist ! Receptionist.Register(ServerStateKey, inbox.ref)

        Thread.sleep(4000)

        testCode(inbox)
        testKit.shutdownTestKit()
    }


    feature("Detect Kafka and SAP availability") {
        scenario("SAP is online and Kafka is offline") {
        withKafkaOfflineSapOnline { inbox =>
                Given("I am waiting for the current state message")
                When("I am receive the state message")
                val msg = inbox.receiveMessage(3.second)
                Then("it should contain `Kafka is offline`")
                msg should be(ServerOnlineApproved)
            }
        }

    }
}

正如你在夹具方法withKafkaOfflineSapOnline中看到的那样,有一个语句Thread.sleep(4000)来确保, 在测试开始之前,演员 DetectorSupervisor 和他们 children 已经准备就绪。

有更好的方法吗?

如果没有 Thread.sleep(4000) 语句,测试开始时,演员似乎还没有准备好。

如果您的 actor 仅与您直接控制的模拟进行通信,并且您在测试中不需要真正的并发,则可以使用 CallingThreadDispatcherConfigurator。当您为单个参与者编写单元测试或者您希望在测试中确定参与者交互时,这很有意义。

所有异步代码都将在测试线程上 运行。因此,当您向演员发送消息时,您通常可以确定它在下一个测试语句之前得到处理。

以下是如何为此目的创建测试套件的示例:

val config = ConfigFactory.parseString(
s"""akka.actor.default-dispatcher = {
   |  type = akka.testkit.CallingThreadDispatcherConfigurator
   |}
   |akka.actor.testkit.typed.single-expect-default = 0s
""".stripMargin
)

val testKit = ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config)