gpt4 book ai didi

Spring Boot 和 Kafka 升级破坏了我的测试

转载 作者:行者123 更新时间:2023-12-02 12:44:41 52 4
gpt4 key购买 nike

我更新了一个项目的库,特别是我更新了 Spring Boot,从版本 2.2.6 到 2.3.2。

the migration documentation 中所述,这也意味着我的 Kafka 依赖项发生了变化,升级到 Spring Kafka 和 Kafka 2.5

但是,由于此更改,我的一些测试“随机”失败,例如,当我执行所有测试时,我在其中一些测试中遇到了这种失败(但并非总是在相同的测试中):

java.lang.IllegalStateException: More than one record for topic found

来自这一行:

KafkaTestUtils.getSingleRecord(consumer, topicConfiguration.getTopic(event))

文档中没有提及发生这种情况的原因,也没有其他日志错误消息阐明发生这种情况的原因。由于我没有更改我的应用程序的逻辑,我是否在迁移过程中遗漏了什么?

如果我尝试这样的事情:

KafkaTestUtils.getRecords(consumer)
.records(topicConfiguration.getTopic(event)).map { it }[0]

为了只获得第一个,之前有效的测试将因索引而失败并出现 IndexOutOfBoundsException

最佳答案

根据test documentation在 Spring Kafka 库中,从 2.5 版开始,他们改变了配置消费者的方式。这是确切的引述:

Starting with version 2.5, the consumerProps method sets the ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to earliest. This is because, in most cases, you want the consumer to consume any messages sent in a test case. The ConsumerConfig default is latest which means that messages already sent by a test, before the consumer starts, will not receive those records. To revert to the previous behavior, set the property to latest after calling the method.

这很可能是描述的测试错误的原因。

关于Spring Boot 和 Kafka 升级破坏了我的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63409601/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com