refactor: RebalanceListener als eigenständige Klasse
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index 431431b..f4c2104 100644 (file)
@@ -6,7 +6,6 @@ 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.*;
@@ -27,7 +26,6 @@ import java.util.*;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.function.BiConsumer;
-import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
@@ -63,7 +61,7 @@ class ApplicationTests
        @Autowired
        KafkaProducer<String, Bytes> kafkaProducer;
        @Autowired
-       KafkaConsumer<String, Long> kafkaConsumer;
+       KafkaConsumer<String, String> kafkaConsumer;
        @Autowired
        PartitionStatisticsRepository partitionStatisticsRepository;
        @Autowired
@@ -72,18 +70,20 @@ class ApplicationTests
        ExecutorService executor;
        @Autowired
        PartitionStatisticsRepository repository;
+       @Autowired
+       WordcountRebalanceListener wordcountRebalanceListener;
+       @Autowired
+       WordcountRecordHandler wordcountRecordHandler;
 
-       Consumer<ConsumerRecord<String, Long>> testHandler;
-       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 */
 
        @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)));
@@ -105,41 +105,6 @@ 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 */
 
@@ -242,8 +207,6 @@ class ApplicationTests
        @BeforeEach
        public void init()
        {
-               testHandler = record -> {} ;
-
                oldOffsets = new HashMap<>();
                newOffsets = new HashMap<>();
                receivedRecords = new HashSet<>();
@@ -254,25 +217,25 @@ class ApplicationTests
                        newOffsets.put(tp, offset - 1);
                });
 
-               Consumer<ConsumerRecord<String, Long>> captureOffsetAndExecuteTestHandler =
-                               record ->
-                               {
-                                       newOffsets.put(
-                                                       new TopicPartition(record.topic(), record.partition()),
-                                                       record.offset());
-                                       receivedRecords.add(record);
-                                       testHandler.accept(record);
+               TestRecordHandler<String, String> captureOffsetAndExecuteTestHandler =
+                               new TestRecordHandler<String, String>(wordcountRecordHandler) {
+                                       @Override
+                                       public void onNewRecord(ConsumerRecord<String, String> record)
+                                       {
+                                               newOffsets.put(
+                                                               new TopicPartition(record.topic(), record.partition()),
+                                                               record.offset());
+                                               receivedRecords.add(record);
+                                       }
                                };
 
                endlessConsumer =
                                new EndlessConsumer<>(
                                                executor,
-                                               repository,
                                                properties.getClientId(),
                                                properties.getTopic(),
-                                               Clock.systemDefaultZone(),
-                                               properties.getCommitInterval(),
                                                kafkaConsumer,
+                                               wordcountRebalanceListener,
                                                captureOffsetAndExecuteTestHandler);
 
                endlessConsumer.start();