我为演员写了如下测试:
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)
}
}
}
}
正如您在Fixture方法withKafkaOfflineSapOnline
中看到的那样,有一条语句Thread.sleep(4000)
可以确保这一点,
测试开始前,演员DetectorSupervisor
和他们的孩子已经准备就绪。
有更好的方法吗?
在没有Thread.sleep(4000)
语句的情况下,似乎在测试开始时参与者还没有准备好。
答案 0 :(得分:0)
如果您的角色仅与您直接控制的模拟进行通信并且您在测试中不需要真正的并发性,则可以使用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)