@Autowired
KafkaConsumer<K, V> kafkaConsumer;
@Autowired
+ Consumer<ConsumerRecord<K, V>> consumer;
+ @Autowired
ApplicationProperties properties;
@Autowired
ExecutorService executor;
KafkaProducer<Bytes, Bytes> testRecordProducer;
KafkaConsumer<Bytes, Bytes> offsetConsumer;
- Consumer<ConsumerRecord<K, V>> testHandler;
EndlessConsumer<K, V> endlessConsumer;
Map<TopicPartition, Long> oldOffsets;
Map<TopicPartition, Long> newOffsets;
@Test
void commitsCurrentOffsetsOnSuccess()
{
- recordGenerator.generate(100, Set.of(), messageSender);
+ recordGenerator.generate(100, Set.of(), Set.of(), messageSender);
await("100 records received")
.atMost(Duration.ofSeconds(30))
@Test
void commitsOffsetOfErrorForReprocessingOnDeserializationError()
{
- recordGenerator.generate(100, Set.of(77), messageSender);
+ recordGenerator.generate(100, Set.of(77), Set.of(), messageSender);
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
.containsInstanceOf(RecordDeserializationException.class);
}
+ @Test
+ void doesNotCommitOffsetsOnLogicError()
+ {
+ recordGenerator.generate(100, Set.of(), Set.of(77), messageSender);
+
+ await("Consumer failed")
+ .atMost(Duration.ofSeconds(30))
+ .pollInterval(Duration.ofSeconds(1))
+ .until(() -> !endlessConsumer.running());
+
+ checkSeenOffsetsForProgress();
+ compareToCommitedOffsets(oldOffsets);
+
+ endlessConsumer.start();
+ await("Consumer failed")
+ .atMost(Duration.ofSeconds(30))
+ .pollInterval(Duration.ofSeconds(1))
+ .until(() -> !endlessConsumer.running());
+
+ checkSeenOffsetsForProgress();
+ compareToCommitedOffsets(oldOffsets);
+ assertThat(receivedRecords.size())
+ .describedAs("Received not all sent events")
+ .isLessThan(100);
+
+ assertThatNoException()
+ .describedAs("Consumer should not be running")
+ .isThrownBy(() -> endlessConsumer.exitStatus());
+ assertThat(endlessConsumer.exitStatus())
+ .describedAs("Consumer should have exited abnormally")
+ .containsInstanceOf(RuntimeException.class);
+ }
+
/** Helper methods for the verification of expectations */
{
void generate(
int numberOfMessagesToGenerate,
- Set<Integer> poistionPills,
+ Set<Integer> poisonPills,
+ Set<Integer> logicErrors,
Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
}
props.put("value.deserializer", BytesDeserializer.class.getName());
offsetConsumer = new KafkaConsumer<>(props);
- testHandler = record -> {} ;
-
seekToEnd();
oldOffsets = new HashMap<>();
new TopicPartition(record.topic(), record.partition()),
record.offset());
receivedRecords.add(record);
- testHandler.accept(record);
+ consumer.accept(record);
};
endlessConsumer =
@TestConfiguration
@Import(ApplicationConfiguration.class)
- public static class Configuration {}
+ public static class Configuration
+ {
+ }
}