Springify: Der Consumer kann unterschiedliche Nachrichten-Typen empfangen
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
1 package de.juplo.kafka;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.apache.kafka.clients.consumer.KafkaConsumer;
6 import org.apache.kafka.clients.producer.KafkaProducer;
7 import org.apache.kafka.clients.producer.ProducerRecord;
8 import org.apache.kafka.common.TopicPartition;
9 import org.apache.kafka.common.serialization.*;
10 import org.apache.kafka.common.utils.Bytes;
11 import org.junit.jupiter.api.*;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
14 import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
15 import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
16 import org.springframework.boot.test.context.TestConfiguration;
17 import org.springframework.context.annotation.Bean;
18 import org.springframework.context.annotation.Import;
19 import org.springframework.context.annotation.Primary;
20 import org.springframework.kafka.support.serializer.JsonSerializer;
21 import org.springframework.kafka.test.context.EmbeddedKafka;
22 import org.springframework.test.context.TestPropertySource;
23 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
24
25 import java.time.Duration;
26 import java.util.*;
27 import java.util.concurrent.ExecutionException;
28 import java.util.function.BiConsumer;
29 import java.util.function.BiFunction;
30 import java.util.function.Consumer;
31 import java.util.stream.Collectors;
32 import java.util.stream.IntStream;
33
34 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
35 import static de.juplo.kafka.ApplicationTests.TOPIC;
36 import static org.assertj.core.api.Assertions.*;
37 import static org.awaitility.Awaitility.*;
38
39
40 @SpringJUnitConfig(
41                 initializers = ConfigDataApplicationContextInitializer.class,
42                 classes = {
43                                 EndlessConsumer.class,
44                                 KafkaAutoConfiguration.class,
45                                 ApplicationTests.Configuration.class })
46 @TestPropertySource(
47                 properties = {
48                                 "spring.kafka.consumer.bootstrap-servers=${spring.embedded.kafka.brokers}",
49                                 "spring.kafka.producer.bootstrap-servers=${spring.embedded.kafka.brokers}",
50                                 "consumer.topic=" + TOPIC })
51 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
52 @Slf4j
53 class ApplicationTests
54 {
55         public static final String TOPIC = "FOO";
56         public static final int PARTITIONS = 10;
57
58
59         StringSerializer stringSerializer = new StringSerializer();
60
61         @Autowired
62         Serializer valueSerializer;
63         @Autowired
64         KafkaProducer<String, Bytes> kafkaProducer;
65         @Autowired
66         KafkaConsumer<Bytes, Bytes> offsetConsumer;
67         @Autowired
68         ApplicationProperties applicationProperties;
69         @Autowired
70         KafkaProperties kafkaProperties;
71         @Autowired
72         EndlessConsumer endlessConsumer;
73         @Autowired
74         RecordHandler recordHandler;
75
76         Map<TopicPartition, Long> oldOffsets;
77         Map<TopicPartition, Long> newOffsets;
78         Set<ConsumerRecord<String, ClientMessage>> receivedRecords;
79
80
81         /** Tests methods */
82
83         @Test
84         void commitsCurrentOffsetsOnSuccess() throws ExecutionException, InterruptedException
85         {
86                 send100Messages((key, counter) -> serialize(key, counter));
87
88                 await("100 records received")
89                                 .atMost(Duration.ofSeconds(30))
90                                 .until(() -> receivedRecords.size() == 100);
91
92                 await("Offsets committed")
93                                 .atMost(Duration.ofSeconds(10))
94                                 .untilAsserted(() ->
95                                 {
96                                         checkSeenOffsetsForProgress();
97                                         compareToCommitedOffsets(newOffsets);
98                                 });
99
100                 assertThat(endlessConsumer.isRunning())
101                                 .describedAs("Consumer should still be running")
102                                 .isTrue();
103         }
104
105         @Test
106         void commitsCurrentOffsetsOnDeserializationError()
107         {
108                 send100Messages((key, counter) ->
109                                 counter == 77
110                                                 ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
111                                                 : serialize(key, counter));
112
113                 await("99 records received")
114                                 .atMost(Duration.ofSeconds(30))
115                                 .until(() -> receivedRecords.size() == 99);
116
117                 await("Offsets committed")
118                                 .atMost(Duration.ofSeconds(10))
119                                 .untilAsserted(() ->
120                                 {
121                                         // UNSCHÖN:
122                                         // Funktioniert nur, weil nach der Nachrichten, die den
123                                         // Deserialisierungs-Fehler auslöst noch valide Nachrichten
124                                         // gelesen werden.
125                                         // GRUND:
126                                         // Der MessageHandler sieht den Offset der Fehlerhaften
127                                         // Nachricht nicht!
128                                         checkSeenOffsetsForProgress();
129                                         compareToCommitedOffsets(newOffsets);
130                                 });
131
132                 assertThat(endlessConsumer.isRunning())
133                                 .describedAs("Consumer should still be running")
134                                 .isTrue();
135         }
136
137         @Test
138         void commitsOffsetOnProgramLogicErrorFoo()
139         {
140                 recordHandler.testHandler = (record) ->
141                 {
142                         if (Integer.parseInt(record.value().message)%10 ==0)
143                                 throw new RuntimeException("BOOM: " + record.value().message + "%10 == 0");
144                 };
145
146                 send100Messages((key, counter) -> serialize(key, counter));
147
148                 await("80 records received")
149                                 .atMost(Duration.ofSeconds(30))
150                                 .until(() -> receivedRecords.size() == 100);
151
152                 await("Offsets committed")
153                                 .atMost(Duration.ofSeconds(10))
154                                 .pollDelay(Duration.ofSeconds(1))
155                                 .untilAsserted(() ->
156                                 {
157                                         checkSeenOffsetsForProgress();
158                                         compareToCommitedOffsets(newOffsets);
159                                 });
160
161                 assertThat(endlessConsumer.isRunning())
162                                 .describedAs("Consumer should still be running")
163                                 .isTrue();
164         }
165
166
167         /** Helper methods for the verification of expectations */
168
169         void compareToCommitedOffsets(Map<TopicPartition, Long> offsetsToCheck)
170         {
171                 doForCurrentOffsets((tp, offset) ->
172                 {
173                         Long expected = offsetsToCheck.get(tp) + 1;
174                         log.debug("TEST: Comparing the expected offset of {} for {} to {}", expected, tp, offset);
175                         assertThat(offset)
176                                         .describedAs("Committed offset corresponds to the offset of the consumer")
177                                         .isEqualTo(expected);
178                 });
179         }
180
181         void checkSeenOffsetsForProgress()
182         {
183                 // Be sure, that some messages were consumed...!
184                 Set<TopicPartition> withProgress = new HashSet<>();
185                 partitions().forEach(tp ->
186                 {
187                         Long oldOffset = oldOffsets.get(tp);
188                         Long newOffset = newOffsets.get(tp);
189                         if (!oldOffset.equals(newOffset))
190                         {
191                                 log.debug("TEST: Progress for {}: {} -> {}", tp, oldOffset, newOffset);
192                                 withProgress.add(tp);
193                         }
194                 });
195                 log.debug("TEST: Offsets with progress: {}", withProgress);
196                 assertThat(withProgress)
197                                 .describedAs("Some offsets must have changed, compared to the old offset-positions")
198                                 .isNotEmpty();
199         }
200
201
202         /** Helper methods for setting up and running the tests */
203
204         void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
205         {
206                 offsetConsumer.assign(partitions());
207                 partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
208                 offsetConsumer.unsubscribe();
209         }
210
211         List<TopicPartition> partitions()
212         {
213                 return
214                                 IntStream
215                                                 .range(0, PARTITIONS)
216                                                 .mapToObj(partition -> new TopicPartition(TOPIC, partition))
217                                                 .collect(Collectors.toList());
218         }
219
220
221         void send100Messages(BiFunction<Integer, Long, Bytes> messageGenerator)
222         {
223                 long i = 0;
224
225                 for (int partition = 0; partition < 10; partition++)
226                 {
227                         for (int key = 0; key < 10; key++)
228                         {
229                                 Bytes value = messageGenerator.apply(key, ++i);
230
231                                 ProducerRecord<String, Bytes> record =
232                                                 new ProducerRecord<>(
233                                                                 TOPIC,
234                                                                 partition,
235                                                                 Integer.toString(key%2),
236                                                                 value);
237
238                                 record.headers().add("__TypeId__", "message".getBytes());
239                                 kafkaProducer.send(record, (metadata, e) ->
240                                 {
241                                         if (metadata != null)
242                                         {
243                                                 log.debug(
244                                                                 "TEST: Sending partition={}, offset={} - {}={}",
245                                                                 metadata.partition(),
246                                                                 metadata.offset(),
247                                                                 record.key(),
248                                                                 record.value());
249                                         }
250                                         else
251                                         {
252                                                 log.warn(
253                                                                 "TEST: Exception for {}={}: {}",
254                                                                 record.key(),
255                                                                 record.value(),
256                                                                 e.toString());
257                                         }
258                                 });
259                         }
260                 }
261         }
262
263         Bytes serialize(Integer key, Long value)
264         {
265                 ClientMessage message = new ClientMessage();
266                 message.setClient(key.toString());
267                 message.setMessage(value.toString());
268                 return new Bytes(valueSerializer.serialize(TOPIC, message));
269         }
270
271
272         @BeforeEach
273         public void init()
274         {
275                 recordHandler.testHandler = (record) -> {};
276
277                 oldOffsets = new HashMap<>();
278                 newOffsets = new HashMap<>();
279                 receivedRecords = new HashSet<>();
280
281                 doForCurrentOffsets((tp, offset) ->
282                 {
283                         oldOffsets.put(tp, offset - 1);
284                         newOffsets.put(tp, offset - 1);
285                 });
286
287                 recordHandler.captureOffsets =
288                                 record ->
289                                 {
290                                         receivedRecords.add(record);
291                                         log.debug("TEST: Processing record #{}: {}", receivedRecords.size(), record.value());
292                                         newOffsets.put(
293                                                         new TopicPartition(record.topic(), record.partition()),
294                                                         record.offset());
295                                 };
296
297                 endlessConsumer.start();
298         }
299
300         @AfterEach
301         public void deinit()
302         {
303                 try
304                 {
305                         endlessConsumer.stop();
306                 }
307                 catch (Exception e)
308                 {
309                         log.info("TEST: Exception while stopping the consumer: {}", e.toString());
310                 }
311         }
312
313         public static class RecordHandler implements Consumer<ConsumerRecord<String, ClientMessage>>
314         {
315                 Consumer<ConsumerRecord<String, ClientMessage>> captureOffsets;
316                 Consumer<ConsumerRecord<String, ClientMessage>> testHandler;
317
318
319                 @Override
320                 public void accept(ConsumerRecord<String, ClientMessage> record)
321                 {
322                         captureOffsets
323                                         .andThen(testHandler)
324                                         .accept(record);
325                 }
326         }
327
328         @TestConfiguration
329         @Import(ApplicationConfiguration.class)
330         public static class Configuration
331         {
332                 @Primary
333                 @Bean
334                 public Consumer<ConsumerRecord<String, ClientMessage>> testHandler()
335                 {
336                         return new RecordHandler();
337                 }
338
339                 @Bean
340                 Serializer<ClientMessage> serializer()
341                 {
342                         return new JsonSerializer<>();
343                 }
344
345                 @Bean
346                 KafkaProducer<String, Bytes> kafkaProducer(KafkaProperties properties)
347                 {
348                         Properties props = new Properties();
349                         props.put("bootstrap.servers", properties.getConsumer().getBootstrapServers());
350                         props.put("linger.ms", 100);
351                         props.put("key.serializer", StringSerializer.class.getName());
352                         props.put("value.serializer", BytesSerializer.class.getName());
353
354                         return new KafkaProducer<>(props);
355                 }
356
357                 @Bean
358                 KafkaConsumer<Bytes, Bytes> offsetConsumer(KafkaProperties properties)
359                 {
360                         Properties props = new Properties();
361                         props.put("bootstrap.servers", properties.getConsumer().getBootstrapServers());
362                         props.put("client.id", "OFFSET-CONSUMER");
363                         props.put("group.id", properties.getConsumer().getGroupId());
364                         props.put("key.deserializer", BytesDeserializer.class.getName());
365                         props.put("value.deserializer", BytesDeserializer.class.getName());
366
367                         return new KafkaConsumer<>(props);
368                 }
369         }
370 }