Apache kafka 在多个ScalateTest套装中生成时,Embedded kafka引发异常
下面是如何配置我的测试套装Apache kafka 在多个ScalateTest套装中生成时,Embedded kafka引发异常,apache-kafka,scalatest,Apache Kafka,Scalatest,下面是如何配置我的测试套装 "test payments" should { "Add 100 credits" in { runTeamTest { team => withRunningKafka { val addCreditsRequest = AddCreditsRequest(team.id.stringify, member1Email, 100) TestCommon.makeRequestAn
"test payments" should {
"Add 100 credits" in {
runTeamTest { team =>
withRunningKafka {
val addCreditsRequest = AddCreditsRequest(team.id.stringify, member1Email, 100)
TestCommon.makeRequestAndCheck(
member1Email,
TeamApiGenerated.addCredits().url,
Helpers.POST,
Json.toJson(addCreditsRequest),
OK
)
val foundTeam = TestCommon.waitForFuture(TeamDao.findOneById(team.id))
foundTeam.get.credits mustEqual initialCreditAmount + 100
}
}
}
"deduct 100 credits" in {
runTeamTest { team =>
withRunningKafka {
val deductCreditsRequest = DeductCreditsRequest(team.id.stringify, member1Email, 100)
TestCommon.makeRequestAndCheck(
member1Email,
TeamApiGenerated.deductCredits().url,
Helpers.POST,
Json.toJson(deductCreditsRequest),
OK
)
val foundTeam = TestCommon.waitForFuture(TeamDao.findOneById(team.id))
foundTeam.get.credits mustEqual initialCreditAmount - 100
}
}
}
在Scalatest中,最重要的套装名称是
“测试付款”
,其中的后续测试在第一个测试运行后出现问题。如果我分别运行这两个测试中的每一个,它们都会成功,但是如果我运行整个测试,第一个测试成功,第二个测试返回org.apache.kafka.common.errors.UnknownTopicOrPartitionException:此服务器不承载此主题分区。
exception。上面的代码没有显示正在测试的控制器中的代码,但是在控制器中,我有一个kafka消费者,它不断地轮询,并且在测试中没有对它调用close()
我建议您在before
和afterAll
部分中使用伴随对象方法EmbeddedKafka.start()
和EmbeddedKafka.stop()
。通过这种方式,您还可以避免为单个测试类再次停止/启动Kafka
另外,请确保您没有试图同时在同一端口上启动两个或多个Kafka实例。是否有办法检查传递了停止命令的Kafka群集是否已实际死亡@Manubyo可以尝试检查Kafka端口上是否有TCP服务器在侦听。如果你检查源代码中的测试,那就是服务器如何运行的验证hi@manub,我知道已经3年了,但是你能不能也评论一下[这里]()?非常感谢。事实上,我的spark应用程序在本地存储检查点,因此重新运行测试不会在EmbeddedKafka上读取相同的传入消息,因此出现错误。因此,我通过在每次运行测试套件时删除checkpoints目录来解决这个问题。如果您有类似的设置,删除检查点可能会有所帮助。