import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.TopicPartition;
-import org.apache.kafka.common.errors.RecordDeserializationException;
import org.apache.kafka.common.serialization.*;
import org.apache.kafka.common.utils.Bytes;
import org.junit.jupiter.api.*;
@Autowired
KafkaProducer<String, Bytes> kafkaProducer;
@Autowired
- KafkaConsumer<String, Long> kafkaConsumer;
+ KafkaConsumer<String, String> kafkaConsumer;
@Autowired
PartitionStatisticsRepository partitionStatisticsRepository;
@Autowired
@Autowired
PartitionStatisticsRepository repository;
- Consumer<ConsumerRecord<String, Long>> testHandler;
- EndlessConsumer<String, Long> endlessConsumer;
+ Consumer<ConsumerRecord<String, String>> testHandler;
+ EndlessConsumer endlessConsumer;
Map<TopicPartition, Long> oldOffsets;
Map<TopicPartition, Long> newOffsets;
- Set<ConsumerRecord<String, Long>> receivedRecords;
+ Set<ConsumerRecord<String, String>> receivedRecords;
/** Tests methods */
@Test
- @Order(1) // << The poistion pill is not skipped. Hence, this test must run first
void commitsCurrentOffsetsOnSuccess() throws ExecutionException, InterruptedException
{
send100Messages(i -> new Bytes(valueSerializer.serialize(TOPIC, i)));
.describedAs("Consumer should still be running");
}
- @Test
- @Order(2)
- void commitsOffsetOfErrorForReprocessingOnError()
- {
- send100Messages(counter ->
- counter == 77
- ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
- : new Bytes(valueSerializer.serialize(TOPIC, counter)));
-
- await("Consumer failed")
- .atMost(Duration.ofSeconds(30))
- .until(() -> !endlessConsumer.running());
-
- checkSeenOffsetsForProgress();
- compareToCommitedOffsets(newOffsets);
-
- endlessConsumer.start();
- await("Consumer failed")
- .atMost(Duration.ofSeconds(30))
- .until(() -> !endlessConsumer.running());
-
- checkSeenOffsetsForProgress();
- compareToCommitedOffsets(newOffsets);
- 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(RecordDeserializationException.class);
- }
-
/** Helper methods for the verification of expectations */
newOffsets.put(tp, offset - 1);
});
- Consumer<ConsumerRecord<String, Long>> captureOffsetAndExecuteTestHandler =
+ Consumer<ConsumerRecord<String, String>> captureOffsetAndExecuteTestHandler =
record ->
{
newOffsets.put(
};
endlessConsumer =
- new EndlessConsumer<>(
+ new EndlessConsumer(
executor,
repository,
properties.getClientId(),
properties.getTopic(),
Clock.systemDefaultZone(),
properties.getCommitInterval(),
- kafkaConsumer,
- captureOffsetAndExecuteTestHandler);
+ kafkaConsumer);
endlessConsumer.start();
}