I am testing a Spring-boot application that produces kafka messages, I have created a consumer in the tests to validate that we are sending the message properly. When I use KafkaTestUtils.getRecords() or KafkaTestUtils.getSingleRecord() in the test the time it takes to receive the records varies a lot on each run. Sometimes takes 1 second and other 20 seconds, Is this expected? Is there any way to improve performance?
This is the test:
void ProducerTest() {
//given
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");//Running docker kafka locally
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group_id");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, DTODeserializer.class);
ConsumerFactory<String, DTO> cf = new DefaultKafkaConsumerFactory<>(props);
Consumer<String, DTO> consumerTest = cf.createConsumer();
consumerTest.subscribe(Collections.singleton("topic"));
//when
//call to api-rest that produces the kafka messate in the "topic"
//then
ConsumerRecords<String, DTO> records = KafkaTestUtils.getRecords(consumerTest);
//Assert
}