1 package de.juplo.kafka;
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.errors.RecordDeserializationException;
10 import org.apache.kafka.common.serialization.*;
11 import org.apache.kafka.common.utils.Bytes;
12 import org.junit.jupiter.api.*;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
15 import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
16 import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
17 import org.springframework.boot.test.context.TestConfiguration;
18 import org.springframework.context.annotation.Import;
19 import org.springframework.kafka.test.context.EmbeddedKafka;
20 import org.springframework.test.context.TestPropertySource;
21 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
23 import java.time.Duration;
25 import java.util.concurrent.ExecutorService;
26 import java.util.function.BiConsumer;
27 import java.util.function.Consumer;
28 import java.util.stream.Collectors;
29 import java.util.stream.IntStream;
31 import static de.juplo.kafka.GenericApplicationTests.PARTITIONS;
32 import static de.juplo.kafka.GenericApplicationTests.TOPIC;
33 import static org.assertj.core.api.Assertions.*;
34 import static org.awaitility.Awaitility.*;
37 @SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
40 "consumer.bootstrap-server=${spring.embedded.kafka.brokers}",
41 "consumer.topic=" + TOPIC,
42 "consumer.commit-interval=1s",
43 "spring.mongodb.embedded.version=4.4.13" })
44 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
45 @EnableAutoConfiguration
46 @AutoConfigureDataMongo
48 abstract class GenericApplicationTests<K, V>
50 public static final String TOPIC = "FOO";
51 public static final int PARTITIONS = 10;
55 KafkaConsumer<K, V> kafkaConsumer;
57 Consumer<ConsumerRecord<K, V>> consumer;
59 ApplicationProperties properties;
61 ExecutorService executor;
63 PollIntervalAwareConsumerRebalanceListener rebalanceListener;
65 RecordHandler<K, V> recordHandler;
67 KafkaProducer<Bytes, Bytes> testRecordProducer;
68 KafkaConsumer<Bytes, Bytes> offsetConsumer;
69 EndlessConsumer<K, V> endlessConsumer;
70 Map<TopicPartition, Long> oldOffsets;
71 Map<TopicPartition, Long> newOffsets;
72 Set<ConsumerRecord<K, V>> receivedRecords;
75 final RecordGenerator recordGenerator;
76 final Consumer<ProducerRecord<Bytes, Bytes>> messageSender;
78 public GenericApplicationTests(RecordGenerator recordGenerator)
80 this.recordGenerator = recordGenerator;
81 this.messageSender = (record) -> sendMessage(record);
88 void commitsCurrentOffsetsOnSuccess()
90 int numberOfGeneratedMessages =
91 recordGenerator.generate(false, false, messageSender);
93 await(numberOfGeneratedMessages + " records received")
94 .atMost(Duration.ofSeconds(30))
95 .pollInterval(Duration.ofSeconds(1))
96 .until(() -> receivedRecords.size() >= numberOfGeneratedMessages);
98 await("Offsets committed")
99 .atMost(Duration.ofSeconds(10))
100 .pollInterval(Duration.ofSeconds(1))
103 checkSeenOffsetsForProgress();
104 compareToCommitedOffsets(newOffsets);
107 assertThatExceptionOfType(IllegalStateException.class)
108 .isThrownBy(() -> endlessConsumer.exitStatus())
109 .describedAs("Consumer should still be running");
111 recordGenerator.assertBusinessLogic();
115 @SkipWhenErrorCannotBeGenerated(poisonPill = true)
116 void commitsOffsetOfErrorForReprocessingOnDeserializationError()
118 int numberOfGeneratedMessages =
119 recordGenerator.generate(true, false, messageSender);
121 await("Consumer failed")
122 .atMost(Duration.ofSeconds(30))
123 .pollInterval(Duration.ofSeconds(1))
124 .until(() -> !endlessConsumer.running());
126 checkSeenOffsetsForProgress();
127 compareToCommitedOffsets(newOffsets);
129 endlessConsumer.start();
130 await("Consumer failed")
131 .atMost(Duration.ofSeconds(30))
132 .pollInterval(Duration.ofSeconds(1))
133 .until(() -> !endlessConsumer.running());
135 checkSeenOffsetsForProgress();
136 compareToCommitedOffsets(newOffsets);
137 assertThat(receivedRecords.size())
138 .describedAs("Received not all sent events")
139 .isLessThan(numberOfGeneratedMessages);
141 assertThatNoException()
142 .describedAs("Consumer should not be running")
143 .isThrownBy(() -> endlessConsumer.exitStatus());
144 assertThat(endlessConsumer.exitStatus())
145 .describedAs("Consumer should have exited abnormally")
146 .containsInstanceOf(RecordDeserializationException.class);
148 recordGenerator.assertBusinessLogic();
152 @SkipWhenErrorCannotBeGenerated(logicError = true)
153 void doesNotCommitOffsetsOnLogicError()
155 int numberOfGeneratedMessages =
156 recordGenerator.generate(false, true, messageSender);
158 await("Consumer failed")
159 .atMost(Duration.ofSeconds(30))
160 .pollInterval(Duration.ofSeconds(1))
161 .until(() -> !endlessConsumer.running());
163 checkSeenOffsetsForProgress();
164 compareToCommitedOffsets(oldOffsets);
166 endlessConsumer.start();
167 await("Consumer failed")
168 .atMost(Duration.ofSeconds(30))
169 .pollInterval(Duration.ofSeconds(1))
170 .until(() -> !endlessConsumer.running());
172 checkSeenOffsetsForProgress();
173 compareToCommitedOffsets(oldOffsets);
174 assertThat(receivedRecords.size())
175 .describedAs("Received not all sent events")
176 .isLessThan(numberOfGeneratedMessages);
178 assertThatNoException()
179 .describedAs("Consumer should not be running")
180 .isThrownBy(() -> endlessConsumer.exitStatus());
181 assertThat(endlessConsumer.exitStatus())
182 .describedAs("Consumer should have exited abnormally")
183 .containsInstanceOf(RuntimeException.class);
185 recordGenerator.assertBusinessLogic();
189 /** Helper methods for the verification of expectations */
191 void compareToCommitedOffsets(Map<TopicPartition, Long> offsetsToCheck)
193 doForCurrentOffsets((tp, offset) ->
195 Long expected = offsetsToCheck.get(tp) + 1;
196 log.debug("Checking, if the offset for {} is {}", tp, expected);
198 .describedAs("Committed offset corresponds to the offset of the consumer")
199 .isEqualTo(expected);
203 void checkSeenOffsetsForProgress()
205 // Be sure, that some messages were consumed...!
206 Set<TopicPartition> withProgress = new HashSet<>();
207 partitions().forEach(tp ->
209 Long oldOffset = oldOffsets.get(tp) + 1;
210 Long newOffset = newOffsets.get(tp) + 1;
211 if (!oldOffset.equals(newOffset))
213 log.debug("Progress for {}: {} -> {}", tp, oldOffset, newOffset);
214 withProgress.add(tp);
217 assertThat(withProgress)
218 .describedAs("Some offsets must have changed, compared to the old offset-positions")
223 /** Helper methods for setting up and running the tests */
227 offsetConsumer.assign(partitions());
228 offsetConsumer.seekToEnd(partitions());
229 partitions().forEach(tp ->
231 // seekToEnd() works lazily: it only takes effect on poll()/position()
232 Long offset = offsetConsumer.position(tp);
233 log.info("New position for {}: {}", tp, offset);
235 // The new positions must be commited!
236 offsetConsumer.commitSync();
237 offsetConsumer.unsubscribe();
240 void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
242 offsetConsumer.assign(partitions());
243 partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
244 offsetConsumer.unsubscribe();
247 List<TopicPartition> partitions()
251 .range(0, PARTITIONS)
252 .mapToObj(partition -> new TopicPartition(TOPIC, partition))
253 .collect(Collectors.toList());
257 public interface RecordGenerator
262 Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
264 default boolean canGeneratePoisonPill()
269 default boolean canGenerateLogicError()
274 default void assertBusinessLogic()
276 log.debug("No business-logic to assert");
280 void sendMessage(ProducerRecord<Bytes, Bytes> record)
282 testRecordProducer.send(record, (metadata, e) ->
284 if (metadata != null)
288 metadata.partition(),
296 "Exception for {}={}: {}",
309 props = new Properties();
310 props.put("bootstrap.servers", properties.getBootstrapServer());
311 props.put("linger.ms", 100);
312 props.put("key.serializer", BytesSerializer.class.getName());
313 props.put("value.serializer", BytesSerializer.class.getName());
314 testRecordProducer = new KafkaProducer<>(props);
316 props = new Properties();
317 props.put("bootstrap.servers", properties.getBootstrapServer());
318 props.put("client.id", "OFFSET-CONSUMER");
319 props.put("group.id", properties.getGroupId());
320 props.put("key.deserializer", BytesDeserializer.class.getName());
321 props.put("value.deserializer", BytesDeserializer.class.getName());
322 offsetConsumer = new KafkaConsumer<>(props);
326 oldOffsets = new HashMap<>();
327 newOffsets = new HashMap<>();
328 receivedRecords = new HashSet<>();
330 doForCurrentOffsets((tp, offset) ->
332 oldOffsets.put(tp, offset - 1);
333 newOffsets.put(tp, offset - 1);
336 TestRecordHandler<K, V> captureOffsetAndExecuteTestHandler =
337 new TestRecordHandler<K, V>(recordHandler)
340 public void onNewRecord(ConsumerRecord<K, V> record)
343 new TopicPartition(record.topic(), record.partition()),
345 receivedRecords.add(record);
350 new EndlessConsumer<>(
352 properties.getClientId(),
353 properties.getTopic(),
356 captureOffsetAndExecuteTestHandler);
358 endlessConsumer.start();
366 endlessConsumer.stop();
367 testRecordProducer.close();
368 offsetConsumer.close();
372 log.info("Exception while stopping the consumer: {}", e.toString());
378 @Import(ApplicationConfiguration.class)
379 public static class Configuration