[英]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
. 想法是我想向主题发送消息,使用
KafkaController
和CountDownLatch
阅读。 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.