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
KafkaConsumer<Bytes, Bytes> offsetConsumer;
@Autowired
@Autowired
PartitionStatisticsRepository repository;
@Autowired
- KeyCountingRebalanceListener keyCountingRebalanceListener;
+ SumRebalanceListener sumRebalanceListener;
@Autowired
- KeyCountingRecordHandler keyCountingRecordHandler;
+ SumRecordHandler sumRecordHandler;
- EndlessConsumer<String, Long> endlessConsumer;
+ EndlessConsumer<String, String> endlessConsumer;
Map<TopicPartition, Long> oldOffsets;
Map<TopicPartition, Long> newOffsets;
- Set<ConsumerRecord<String, Long>> receivedRecords;
+ Set<ConsumerRecord<String, String>> receivedRecords;
/** Tests methods */
.describedAs("Consumer should still be running");
}
- @Test
- void commitsOffsetOfErrorForReprocessingOnDeserializationError()
- {
- send100Messages((partition, key, counter) ->
- {
- Bytes value = counter == 77
- ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
- : new Bytes(valueSerializer.serialize(TOPIC, counter));
- return new ProducerRecord<>(TOPIC, partition, key, value);
- });
-
- await("Consumer failed")
- .atMost(Duration.ofSeconds(30))
- .pollInterval(Duration.ofSeconds(1))
- .until(() -> !endlessConsumer.running());
-
- checkSeenOffsetsForProgress();
- compareToCommitedOffsets(newOffsets);
-
- endlessConsumer.start();
- await("Consumer failed")
- .atMost(Duration.ofSeconds(30))
- .pollInterval(Duration.ofSeconds(1))
- .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 */
Long offset = offsetConsumer.position(tp);
log.info("New position for {}: {}", tp, offset);
Integer partition = tp.partition();
- StatisticsDocument document =
+ StateDocument document =
partitionStatisticsRepository
.findById(partition.toString())
- .orElse(new StatisticsDocument(partition));
+ .orElse(new StateDocument(partition));
document.offset = offset;
partitionStatisticsRepository.save(document);
});
Optional<Long> offset = partitionStatisticsRepository.findById(partition).map(document -> document.offset);
consumer.accept(tp, offset.orElse(0l));
});
- }
+ }
List<TopicPartition> partitions()
{
newOffsets.put(tp, offset - 1);
});
- TestRecordHandler<String, Long> captureOffsetAndExecuteTestHandler =
- new TestRecordHandler<String, Long>(keyCountingRecordHandler) {
+ TestRecordHandler<String, String> captureOffsetAndExecuteTestHandler =
+ new TestRecordHandler<String, String>(sumRecordHandler) {
@Override
- public void onNewRecord(ConsumerRecord<String, Long> record)
+ public void onNewRecord(ConsumerRecord<String, String> record)
{
newOffsets.put(
new TopicPartition(record.topic(), record.partition()),
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
- keyCountingRebalanceListener,
+ sumRebalanceListener,
captureOffsetAndExecuteTestHandler);
endlessConsumer.start();