Springify: Einen Test für das Empfangen gemischter Nachrichten hinzugefügt
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index 8461824..c3fbe45 100644 (file)
@@ -1,47 +1,52 @@
 package de.juplo.kafka;
 
+import lombok.Value;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
 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.WakeupException;
-import org.apache.kafka.common.serialization.BytesSerializer;
-import org.apache.kafka.common.serialization.LongSerializer;
-import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.common.serialization.*;
 import org.apache.kafka.common.utils.Bytes;
-import org.junit.jupiter.api.MethodOrderer;
-import org.junit.jupiter.api.Order;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.*;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
+import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
 import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
 import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Primary;
+import org.springframework.kafka.listener.adapter.ConsumerRecordMetadata;
+import org.springframework.kafka.support.serializer.JsonSerializer;
 import org.springframework.kafka.test.context.EmbeddedKafka;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 
+import java.time.Duration;
+import java.time.LocalDateTime;
 import java.util.*;
-import java.util.concurrent.ExecutorService;
 import java.util.function.BiConsumer;
-import java.util.function.Consumer;
-import java.util.function.Function;
+import java.util.function.BiFunction;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
 import static de.juplo.kafka.ApplicationTests.TOPIC;
-import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.*;
+import static org.awaitility.Awaitility.*;
 
 
-@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
-@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@SpringJUnitConfig(
+               initializers = ConfigDataApplicationContextInitializer.class,
+               classes = {
+                               EndlessConsumer.class,
+                               KafkaAutoConfiguration.class,
+                               ApplicationTests.Configuration.class })
 @TestPropertySource(
                properties = {
-                               "consumer.bootstrap-server=${spring.embedded.kafka.brokers}",
+                               "spring.kafka.consumer.bootstrap-servers=${spring.embedded.kafka.brokers}",
+                               "spring.kafka.producer.bootstrap-servers=${spring.embedded.kafka.brokers}",
                                "consumer.topic=" + TOPIC })
 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
 @Slf4j
@@ -51,54 +56,195 @@ class ApplicationTests
        public static final int PARTITIONS = 10;
 
 
-       StringSerializer stringSerializer = new StringSerializer();
-       LongSerializer longSerializer = new LongSerializer();
-
+       @Autowired
+       Serializer valueSerializer;
        @Autowired
        KafkaProducer<String, Bytes> kafkaProducer;
        @Autowired
-       KafkaConsumer<String, Long> kafkaConsumer;
+       KafkaConsumer<Bytes, Bytes> offsetConsumer;
+       @Autowired
+       ApplicationProperties applicationProperties;
+       @Autowired
+       KafkaProperties kafkaProperties;
        @Autowired
-       ApplicationProperties properties;
+       EndlessConsumer endlessConsumer;
        @Autowired
-       ExecutorService executor;
+       MessageHandler<ClientMessage> clientMessageHandler;
+       @Autowired
+       MessageHandler<Greeting> greetingsHandler;
+
+       Map<TopicPartition, Long> oldOffsets;
+       Map<TopicPartition, Long> newOffsets;
+       Set<Object> received;
 
 
+       /** Tests methods */
+
        @Test
-       @Order(1) // << The poistion pill is not skipped. Hence, this test must run first
        void commitsCurrentOffsetsOnSuccess()
        {
-               send100Messages(i ->  new Bytes(longSerializer.serialize(TOPIC, i)));
+               send100Messages((key, counter) -> serializeAsClientMessage(key, counter));
 
-               Set<ConsumerRecord<String, Long>> received = new HashSet<>();
-               Map<TopicPartition, Long> offsets = runEndlessConsumer(record ->
-               {
-                       received.add(record);
-                       if (received.size() == 100)
-                               throw new WakeupException();
-               });
+               await("100 records received")
+                               .atMost(Duration.ofSeconds(30))
+                               .until(() -> received.size() == 100);
+
+               await("Offsets committed")
+                               .atMost(Duration.ofSeconds(10))
+                               .untilAsserted(() ->
+                               {
+                                       checkSeenOffsetsForProgress();
+                                       compareToCommitedOffsets(newOffsets);
+                               });
 
-               check(offsets);
+               assertThat(endlessConsumer.isRunning())
+                               .describedAs("Consumer should still be running")
+                               .isTrue();
        }
 
+
        @Test
-       @Order(2)
-       void commitsNoOffsetsOnError()
+       void mixedMessages()
        {
-               send100Messages(counter ->
+               send100Messages((key, counter) ->
+                               counter%3 == 0
+                                               ? serializeAsGreeting(key)
+                                               : serializeAsClientMessage(key, counter));
+
+               await("100 records received")
+                               .atMost(Duration.ofSeconds(30))
+                               .until(() -> received.size() == 100);
+
+               await("Offsets committed")
+                               .atMost(Duration.ofSeconds(10))
+                               .untilAsserted(() ->
+                               {
+                                       checkSeenOffsetsForProgress();
+                                       compareToCommitedOffsets(newOffsets);
+                               });
+
+               assertThat(endlessConsumer.isRunning())
+                               .describedAs("Consumer should still be running")
+                               .isTrue();
+       }
+
+       @Test
+       void commitsCurrentOffsetsOnDeserializationError()
+       {
+               send100Messages((key, counter) ->
                                counter == 77
-                                               ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
-                                               : new Bytes(longSerializer.serialize(TOPIC, counter)));
+                                               ? serializeString("BOOM!", "message")
+                                               : serializeAsClientMessage(key, counter));
 
-               Map<TopicPartition, Long> oldOffsets = new HashMap<>();
-               doForCurrentOffsets((tp, offset) -> oldOffsets.put(tp, offset -1));
-               Map<TopicPartition, Long> newOffsets = runEndlessConsumer((record) -> {});
+               await("99 records received")
+                               .atMost(Duration.ofSeconds(30))
+                               .until(() -> received.size() == 99);
+
+               await("Offsets committed")
+                               .atMost(Duration.ofSeconds(10))
+                               .untilAsserted(() ->
+                               {
+                                       // UNSCHÖN:
+                                       // Funktioniert nur, weil nach der Nachrichten, die den
+                                       // Deserialisierungs-Fehler auslöst noch valide Nachrichten
+                                       // gelesen werden.
+                                       // GRUND:
+                                       // Der MessageHandler sieht den Offset der Fehlerhaften
+                                       // Nachricht nicht!
+                                       checkSeenOffsetsForProgress();
+                                       compareToCommitedOffsets(newOffsets);
+                               });
+
+               assertThat(endlessConsumer.isRunning())
+                               .describedAs("Consumer should still be running")
+                               .isTrue();
+       }
+
+       @Test
+       void commitsOffsetOnProgramLogicErrorFoo()
+       {
+               clientMessageHandler.testHandler = (clientMessage, metadata) ->
+               {
+                       if (Integer.parseInt(clientMessage.message)%10 ==0)
+                               throw new RuntimeException("BOOM: " + clientMessage.message + "%10 == 0");
+               };
 
-               check(oldOffsets);
+               send100Messages((key, counter) -> serializeAsClientMessage(key, counter));
+
+               await("80 records received")
+                               .atMost(Duration.ofSeconds(30))
+                               .until(() -> received.size() == 100);
+
+               await("Offsets committed")
+                               .atMost(Duration.ofSeconds(10))
+                               .pollDelay(Duration.ofSeconds(1))
+                               .untilAsserted(() ->
+                               {
+                                       checkSeenOffsetsForProgress();
+                                       compareToCommitedOffsets(newOffsets);
+                               });
+
+               assertThat(endlessConsumer.isRunning())
+                               .describedAs("Consumer should still be running")
+                               .isTrue();
+       }
+
+
+       /** Helper methods for the verification of expectations */
+
+       void compareToCommitedOffsets(Map<TopicPartition, Long> offsetsToCheck)
+       {
+               doForCurrentOffsets((tp, offset) ->
+               {
+                       Long expected = offsetsToCheck.get(tp) + 1;
+                       log.debug("TEST: Comparing the expected offset of {} for {} to {}", expected, tp, offset);
+                       assertThat(offset)
+                                       .describedAs("Committed offset corresponds to the offset of the consumer")
+                                       .isEqualTo(expected);
+               });
+       }
+
+       void checkSeenOffsetsForProgress()
+       {
+               // Be sure, that some messages were consumed...!
+               Set<TopicPartition> withProgress = new HashSet<>();
+               partitions().forEach(tp ->
+               {
+                       Long oldOffset = oldOffsets.get(tp);
+                       Long newOffset = newOffsets.get(tp);
+                       if (!oldOffset.equals(newOffset))
+                       {
+                               log.debug("TEST: Progress for {}: {} -> {}", tp, oldOffset, newOffset);
+                               withProgress.add(tp);
+                       }
+               });
+               log.debug("TEST: Offsets with progress: {}", withProgress);
+               assertThat(withProgress)
+                               .describedAs("Some offsets must have changed, compared to the old offset-positions")
+                               .isNotEmpty();
+       }
+
+
+       /** Helper methods for setting up and running the tests */
+
+       void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
+       {
+               offsetConsumer.assign(partitions());
+               partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
+               offsetConsumer.unsubscribe();
+       }
+
+       List<TopicPartition> partitions()
+       {
+               return
+                               IntStream
+                                               .range(0, PARTITIONS)
+                                               .mapToObj(partition -> new TopicPartition(TOPIC, partition))
+                                               .collect(Collectors.toList());
        }
 
 
-       void send100Messages(Function<Long, Bytes> messageGenerator)
+       void send100Messages(BiFunction<Integer, Long, BytesAndType> recordGenerator)
        {
                long i = 0;
 
@@ -106,21 +252,22 @@ class ApplicationTests
                {
                        for (int key = 0; key < 10; key++)
                        {
-                               Bytes value = messageGenerator.apply(++i);
+                               BytesAndType bat = recordGenerator.apply(key, ++i);
 
                                ProducerRecord<String, Bytes> record =
                                                new ProducerRecord<>(
                                                                TOPIC,
                                                                partition,
                                                                Integer.toString(key%2),
-                                                               value);
+                                                               bat.getValue());
 
+                               record.headers().add("__TypeId__", bat.getType());
                                kafkaProducer.send(record, (metadata, e) ->
                                {
                                        if (metadata != null)
                                        {
                                                log.debug(
-                                                               "{}|{} - {}={}",
+                                                               "TEST: Sending partition={}, offset={} - {}={}",
                                                                metadata.partition(),
                                                                metadata.offset(),
                                                                record.key(),
@@ -129,7 +276,7 @@ class ApplicationTests
                                        else
                                        {
                                                log.warn(
-                                                               "Exception for {}={}: {}",
+                                                               "TEST: Exception for {}={}: {}",
                                                                record.key(),
                                                                record.value(),
                                                                e.toString());
@@ -139,69 +286,156 @@ class ApplicationTests
                }
        }
 
-       Map<TopicPartition, Long> runEndlessConsumer(Consumer<ConsumerRecord<String, Long>> consumer)
+       BytesAndType serializeAsClientMessage(Integer key, Long value)
        {
-               Map<TopicPartition, Long> offsets = new HashMap<>();
-               doForCurrentOffsets((tp, offset) -> offsets.put(tp, offset -1));
-               Consumer<ConsumerRecord<String, Long>> captureOffset =
-                               record ->
-                                               offsets.put(
-                                                               new TopicPartition(record.topic(), record.partition()),
-                                                               record.offset());
-               EndlessConsumer<String, Long> endlessConsumer =
-                               new EndlessConsumer<>(
-                                               executor,
-                                               properties.getClientId(),
-                                               properties.getTopic(),
-                                               kafkaConsumer,
-                                               captureOffset.andThen(consumer));
-
-               endlessConsumer.run();
+               ClientMessage message = new ClientMessage();
+               message.setClient(key.toString());
+               message.setMessage(value.toString());
+               return new BytesAndType(serialize(message), "message");
+       }
 
-               return offsets;
+       BytesAndType serializeAsGreeting(Integer key)
+       {
+               Greeting greeting = new Greeting();
+               greeting.setName(key.toString());
+               greeting.setWhen(LocalDateTime.now());
+               return new BytesAndType(serialize(greeting), "greeting");
        }
 
-       List<TopicPartition> partitions()
+       BytesAndType serializeString(String message, String messageType)
        {
-               return
-                               IntStream
-                                               .range(0, PARTITIONS)
-                                               .mapToObj(partition -> new TopicPartition(TOPIC, partition))
-                                               .collect(Collectors.toList());
+               return new BytesAndType(new Bytes(message.getBytes()), messageType);
        }
 
-       void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
+       Bytes serialize(Object message)
        {
-               kafkaConsumer.assign(partitions());
-               partitions().forEach(tp -> consumer.accept(tp, kafkaConsumer.position(tp)));
-               kafkaConsumer.unsubscribe();
+               return new Bytes(valueSerializer.serialize(TOPIC, message));
        }
 
-       void check(Map<TopicPartition, Long> offsets)
+
+       @BeforeEach
+       public void init()
        {
+               clientMessageHandler.testHandler = (clientMessage, metadata) -> {};
+               greetingsHandler.testHandler = (greeting, metadata) -> {};
+
+               oldOffsets = new HashMap<>();
+               newOffsets = new HashMap<>();
+               received = new HashSet<>();
+
                doForCurrentOffsets((tp, offset) ->
                {
-                       Long expected = offsets.get(tp) + 1;
-                       log.debug("Checking, if the offset for {} is {}", tp, expected);
-                       assertThat(offset).isEqualTo(expected);
+                       oldOffsets.put(tp, offset - 1);
+                       newOffsets.put(tp, offset - 1);
                });
+
+               BiConsumer<?, ConsumerRecordMetadata> captureOffsets =
+                               (clientMessage, metadata) ->
+                               {
+                                       received.add(clientMessage);
+                                       log.debug("TEST: Processing record #{}: {}", received.size(), clientMessage);
+                                       newOffsets.put(
+                                                       new TopicPartition(metadata.topic(), metadata.partition()), metadata.offset());
+                               };
+
+               clientMessageHandler.captureOffsets =
+                               (BiConsumer<ClientMessage, ConsumerRecordMetadata>)captureOffsets;
+               greetingsHandler.captureOffsets =
+                               (BiConsumer<Greeting, ConsumerRecordMetadata>)captureOffsets;
+
+               endlessConsumer.start();
        }
 
+       @AfterEach
+       public void deinit()
+       {
+               try
+               {
+                       endlessConsumer.stop();
+               }
+               catch (Exception e)
+               {
+                       log.info("TEST: Exception while stopping the consumer: {}", e.toString());
+               }
+       }
+
+       public static class MessageHandler<T> implements BiConsumer<T, ConsumerRecordMetadata>
+       {
+               BiConsumer<T, ConsumerRecordMetadata> captureOffsets;
+               BiConsumer<T, ConsumerRecordMetadata> testHandler;
+
+
+               @Override
+               public void accept(T message, ConsumerRecordMetadata metadata)
+               {
+                       captureOffsets
+                                       .andThen(testHandler)
+                                       .accept(message, metadata);
+               }
+       }
 
        @TestConfiguration
        @Import(ApplicationConfiguration.class)
        public static class Configuration
        {
+               @Primary
+               @Bean
+               public MessageHandler<ClientMessage> messageHandler()
+               {
+                       return new MessageHandler<>();
+               }
+
+               @Primary
+               @Bean
+               public MessageHandler<Greeting> greetingsHandler()
+               {
+                       return new MessageHandler<>();
+               }
+
+               @Bean
+               Serializer<ClientMessage> serializer()
+               {
+                       return new JsonSerializer<>();
+               }
+
                @Bean
-               KafkaProducer<String, Bytes> kafkaProducer(ApplicationProperties properties)
+               KafkaProducer<String, Bytes> kafkaProducer(KafkaProperties properties)
                {
                        Properties props = new Properties();
-                       props.put("bootstrap.servers", properties.getBootstrapServer());
+                       props.put("bootstrap.servers", properties.getConsumer().getBootstrapServers());
                        props.put("linger.ms", 100);
                        props.put("key.serializer", StringSerializer.class.getName());
                        props.put("value.serializer", BytesSerializer.class.getName());
 
                        return new KafkaProducer<>(props);
                }
+
+               @Bean
+               KafkaConsumer<Bytes, Bytes> offsetConsumer(KafkaProperties properties)
+               {
+                       Properties props = new Properties();
+                       props.put("bootstrap.servers", properties.getConsumer().getBootstrapServers());
+                       props.put("client.id", "OFFSET-CONSUMER");
+                       props.put("group.id", properties.getConsumer().getGroupId());
+                       props.put("key.deserializer", BytesDeserializer.class.getName());
+                       props.put("value.deserializer", BytesDeserializer.class.getName());
+
+                       return new KafkaConsumer<>(props);
+               }
+       }
+
+
+       @Value
+       static class BytesAndType
+       {
+               private final Bytes value;
+               private final byte[] type;
+
+
+               BytesAndType(Bytes value, String type)
+               {
+                       this.value = value;
+                       this.type = type.getBytes();
+               }
        }
 }