简体   繁体   English

嵌入式Kafka生产者测试

[英]Embedded kafka producer test

I'm writing integration tests to test kafka producer. 我正在编写集成测试以测试kafka生产者。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {kafkaProducerConfig.class, KafkaProducerIT.InnerConfig.class})
@EnableConfigurationProperties(KafkaProducerInfo.class)
@ComponentScan(basePackages = "...")
public class KafkaProducerIT {

    @ClassRule
    public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, "testtopic");

    @Autowired
    CustomKafkaProducer<String, String> KafkaProducer;

    @Autowired
    KafkaController kafkaController;

    @Test
    public void whenSendMessage_thenConsumeIt() throws InterruptedException {
        KafkaProducer.produceMessageToKafkaTopic("ahahahwow", "testtopic");
        kafkaController.countDownLatch.await();
    }

    @Configuration
    public static class InnerConfig {

        @Bean
        public KafkaListenerContainerFactory<?> kafkaListenerContainerFactory(ConsumerFactory<String, Object> replyConsumerFactory) {
            ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
            factory.setConsumerFactory(replyConsumerFactory);
            factory.setBatchListener(true);
            return factory;
        }

        @Bean
        KafkaController kafkaController() {
            return new KafkaController();
        }

    }

    public static class KafkaController {

        CountDownLatch countDownLatch = new CountDownLatch(1);

        @KafkaListener(topics = "testtopic")
        public void listen(final String payload) {
            countDownLatch.countDown();
        }
    }

}

Idea is that I want to send message to topic, read it using KafkaController and CountDownLatch . 想法是我想向主题发送消息,使用KafkaControllerCountDownLatch阅读。 Issue that I have is that CountDownLatch is never triggered and test just hangs on await . CountDownLatch问题是CountDownLatch永远不会触发,而测试只是在await挂起。

CustomKafkaProducer is just a wrapper which uses regular kafkaTemplate under the hood. CustomKafkaProducer只是一个包装,它在kafkaTemplate使用常规的kafkaTemplate

ps ps

During debug, there were several cases when flow entered listener and test passed. 在调试期间,在几种情况下,流进入侦听器并通过测试。 So issue is not related to wrong topic names etc. 因此,问题与错误的主题名称等无关。

You need to set auto.offset.reset=earliest for the consumer. 您需要为使用者设置auto.offset.reset =最早。 The default is latest so there is a race condition if the consumer starts after the record is sent. 默认值为最新,因此如果使用者在发送记录后开始,则存在竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kafka Producer 集成测试定义 - Kafka Producer Integration Test Definition 使用scalatest-embedded-kafka集成测试Flink和Kafka - Integration test Flink and Kafka with scalatest-embedded-kafka 使用嵌入式 Kafka 测试 Flink - Testing Flink with embedded Kafka 嵌入式Zookeeper用于单元/集成测试 - embedded zookeeper for unit/integration test 测试kafka和flink集成流程 - Test kafka and flink integration flow 当活动配置文件为“test”时使用Embedded MongoDB - Use Embedded MongoDB when active profile is “test” 嵌入式mongodb的jenkins中的集成测试失败 - Integration Test fails in jenkins with embedded mongodb 在Spring Cloud Stream中使用Embedded Kafka进行集成测试时,如何立即验证是否已确认消息? - How can you verify immediately that a message was acknowledged when integration testing using Embedded Kafka in Spring Cloud Stream? spring-boot 嵌入式 kafka 问题:我收到无效接收(大小 = 369296129 大于 104857600) - spring-boot embedded kafka issue : I am getting Invalid receive (size = 369296129 larger than 104857600) 如何运行嵌入式TomEE以使用测试资源进行集成测试 - How to run embedded TomEE for integration tests with test resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM