Refaktorisierungen aus 'wordcount' nach 'stored-offsets' zurück portiert
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index 408a826..a632a89 100644 (file)
@@ -6,6 +6,7 @@ import org.apache.kafka.clients.consumer.KafkaConsumer;
 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.*;
@@ -20,7 +21,6 @@ import org.springframework.kafka.test.context.EmbeddedKafka;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 
-import java.time.Clock;
 import java.time.Duration;
 import java.util.*;
 import java.util.concurrent.ExecutionException;
@@ -61,7 +61,7 @@ class ApplicationTests
        @Autowired
        KafkaProducer<String, Bytes> kafkaProducer;
        @Autowired
-       KafkaConsumer<String, String> kafkaConsumer;
+       KafkaConsumer<String, Long> kafkaConsumer;
        @Autowired
        PartitionStatisticsRepository partitionStatisticsRepository;
        @Autowired
@@ -70,18 +70,21 @@ class ApplicationTests
        ExecutorService executor;
        @Autowired
        PartitionStatisticsRepository repository;
-  @Autowired
-       WordcountRecordHandler wordcountRecordHandler;
+       @Autowired
+       KeyCountingRebalanceListener keyCountingRebalanceListener;
+       @Autowired
+       KeyCountingRecordHandler keyCountingRecordHandler;
 
-       EndlessConsumer<String, String> endlessConsumer;
+       EndlessConsumer<String, Long> endlessConsumer;
        Map<TopicPartition, Long> oldOffsets;
        Map<TopicPartition, Long> newOffsets;
-       Set<ConsumerRecord<String, String>> receivedRecords;
+       Set<ConsumerRecord<String, Long>> 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)));
@@ -103,6 +106,41 @@ class ApplicationTests
                                .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 */
 
@@ -148,7 +186,7 @@ class ApplicationTests
                        Optional<Long> offset = partitionStatisticsRepository.findById(partition).map(document -> document.offset);
                        consumer.accept(tp, offset.orElse(0l));
                });
-       }
+               }
 
        List<TopicPartition> partitions()
        {
@@ -215,10 +253,10 @@ class ApplicationTests
                        newOffsets.put(tp, offset - 1);
                });
 
-               TestRecordHandler<String, String> captureOffsetAndExecuteTestHandler =
-                               new TestRecordHandler<String, String>(wordcountRecordHandler) {
+               TestRecordHandler<String, Long> captureOffsetAndExecuteTestHandler =
+                               new TestRecordHandler<String, Long>(keyCountingRecordHandler) {
                                        @Override
-                                       public void onNewRecord(ConsumerRecord<String, String> record)
+                                       public void onNewRecord(ConsumerRecord<String, Long> record)
                                        {
                                                newOffsets.put(
                                                                new TopicPartition(record.topic(), record.partition()),
@@ -233,6 +271,7 @@ class ApplicationTests
                                                properties.getClientId(),
                                                properties.getTopic(),
                                                kafkaConsumer,
+                                               keyCountingRebalanceListener,
                                                captureOffsetAndExecuteTestHandler);
 
                endlessConsumer.start();