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.test.context.ConfigDataApplicationContextInitializer;
15 import org.springframework.boot.test.context.TestConfiguration;
16 import org.springframework.context.annotation.Import;
17 import org.springframework.kafka.test.context.EmbeddedKafka;
18 import org.springframework.test.context.TestPropertySource;
19 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
21 import java.time.Duration;
23 import java.util.concurrent.ExecutorService;
24 import java.util.function.BiConsumer;
25 import java.util.function.Consumer;
26 import java.util.stream.Collectors;
27 import java.util.stream.IntStream;
29 import static de.juplo.kafka.GenericApplicationTests.PARTITIONS;
30 import static de.juplo.kafka.GenericApplicationTests.TOPIC;
31 import static org.assertj.core.api.Assertions.*;
32 import static org.awaitility.Awaitility.*;
35 @SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
38 "consumer.bootstrap-server=${spring.embedded.kafka.brokers}",
39 "consumer.topic=" + TOPIC,
40 "consumer.commit-interval=1s" })
41 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
43 abstract class GenericApplicationTests<K, V>
45 public static final String TOPIC = "FOO";
46 public static final int PARTITIONS = 10;
50 KafkaConsumer<K, V> kafkaConsumer;
52 Consumer<ConsumerRecord<K, V>> consumer;
54 ApplicationProperties properties;
56 ExecutorService executor;
58 KafkaProducer<Bytes, Bytes> testRecordProducer;
59 KafkaConsumer<Bytes, Bytes> offsetConsumer;
60 EndlessConsumer<K, V> endlessConsumer;
61 Map<TopicPartition, Long> oldOffsets;
62 Map<TopicPartition, Long> newOffsets;
63 Set<ConsumerRecord<K, V>> receivedRecords;
66 final RecordGenerator recordGenerator;
67 final Consumer<ProducerRecord<Bytes, Bytes>> messageSender;
69 public GenericApplicationTests(RecordGenerator recordGenerator)
71 this.recordGenerator = recordGenerator;
72 this.messageSender = (record) -> sendMessage(record);
79 void commitsCurrentOffsetsOnSuccess()
81 int numberOfGeneratedMessages =
82 recordGenerator.generate(false, false, messageSender);
84 await(numberOfGeneratedMessages + " records received")
85 .atMost(Duration.ofSeconds(30))
86 .pollInterval(Duration.ofSeconds(1))
87 .until(() -> receivedRecords.size() >= numberOfGeneratedMessages);
89 await("Offsets committed")
90 .atMost(Duration.ofSeconds(10))
91 .pollInterval(Duration.ofSeconds(1))
94 checkSeenOffsetsForProgress();
95 compareToCommitedOffsets(newOffsets);
98 assertThatExceptionOfType(IllegalStateException.class)
99 .isThrownBy(() -> endlessConsumer.exitStatus())
100 .describedAs("Consumer should still be running");
104 @SkipWhenErrorCannotBeGenerated(poisonPill = true)
105 void commitsOffsetOfErrorForReprocessingOnDeserializationError()
107 int numberOfGeneratedMessages =
108 recordGenerator.generate(true, false, messageSender);
110 await("Consumer failed")
111 .atMost(Duration.ofSeconds(30))
112 .pollInterval(Duration.ofSeconds(1))
113 .until(() -> !endlessConsumer.running());
115 checkSeenOffsetsForProgress();
116 compareToCommitedOffsets(newOffsets);
118 endlessConsumer.start();
119 await("Consumer failed")
120 .atMost(Duration.ofSeconds(30))
121 .pollInterval(Duration.ofSeconds(1))
122 .until(() -> !endlessConsumer.running());
124 checkSeenOffsetsForProgress();
125 compareToCommitedOffsets(newOffsets);
126 assertThat(receivedRecords.size())
127 .describedAs("Received not all sent events")
128 .isLessThan(numberOfGeneratedMessages);
130 assertThatNoException()
131 .describedAs("Consumer should not be running")
132 .isThrownBy(() -> endlessConsumer.exitStatus());
133 assertThat(endlessConsumer.exitStatus())
134 .describedAs("Consumer should have exited abnormally")
135 .containsInstanceOf(RecordDeserializationException.class);
139 @SkipWhenErrorCannotBeGenerated(logicError = true)
140 void doesNotCommitOffsetsOnLogicError()
142 int numberOfGeneratedMessages =
143 recordGenerator.generate(false, true, messageSender);
145 await("Consumer failed")
146 .atMost(Duration.ofSeconds(30))
147 .pollInterval(Duration.ofSeconds(1))
148 .until(() -> !endlessConsumer.running());
150 checkSeenOffsetsForProgress();
151 compareToCommitedOffsets(oldOffsets);
153 endlessConsumer.start();
154 await("Consumer failed")
155 .atMost(Duration.ofSeconds(30))
156 .pollInterval(Duration.ofSeconds(1))
157 .until(() -> !endlessConsumer.running());
159 checkSeenOffsetsForProgress();
160 compareToCommitedOffsets(oldOffsets);
161 assertThat(receivedRecords.size())
162 .describedAs("Received not all sent events")
163 .isLessThan(numberOfGeneratedMessages);
165 assertThatNoException()
166 .describedAs("Consumer should not be running")
167 .isThrownBy(() -> endlessConsumer.exitStatus());
168 assertThat(endlessConsumer.exitStatus())
169 .describedAs("Consumer should have exited abnormally")
170 .containsInstanceOf(RuntimeException.class);
174 /** Helper methods for the verification of expectations */
176 void compareToCommitedOffsets(Map<TopicPartition, Long> offsetsToCheck)
178 doForCurrentOffsets((tp, offset) ->
180 Long expected = offsetsToCheck.get(tp) + 1;
181 log.debug("Checking, if the offset for {} is {}", tp, expected);
183 .describedAs("Committed offset corresponds to the offset of the consumer")
184 .isEqualTo(expected);
188 void checkSeenOffsetsForProgress()
190 // Be sure, that some messages were consumed...!
191 Set<TopicPartition> withProgress = new HashSet<>();
192 partitions().forEach(tp ->
194 Long oldOffset = oldOffsets.get(tp) + 1;
195 Long newOffset = newOffsets.get(tp) + 1;
196 if (!oldOffset.equals(newOffset))
198 log.debug("Progress for {}: {} -> {}", tp, oldOffset, newOffset);
199 withProgress.add(tp);
202 assertThat(withProgress)
203 .describedAs("Some offsets must have changed, compared to the old offset-positions")
208 /** Helper methods for setting up and running the tests */
212 offsetConsumer.assign(partitions());
213 offsetConsumer.seekToEnd(partitions());
214 partitions().forEach(tp ->
216 // seekToEnd() works lazily: it only takes effect on poll()/position()
217 Long offset = offsetConsumer.position(tp);
218 log.info("New position for {}: {}", tp, offset);
220 // The new positions must be commited!
221 offsetConsumer.commitSync();
222 offsetConsumer.unsubscribe();
225 void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
227 offsetConsumer.assign(partitions());
228 partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
229 offsetConsumer.unsubscribe();
232 List<TopicPartition> partitions()
236 .range(0, PARTITIONS)
237 .mapToObj(partition -> new TopicPartition(TOPIC, partition))
238 .collect(Collectors.toList());
242 public interface RecordGenerator
247 Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
249 default boolean canGeneratePoisonPill()
254 default boolean canGenerateLogicError()
260 void sendMessage(ProducerRecord<Bytes, Bytes> record)
262 testRecordProducer.send(record, (metadata, e) ->
264 if (metadata != null)
268 metadata.partition(),
276 "Exception for {}={}: {}",
289 props = new Properties();
290 props.put("bootstrap.servers", properties.getBootstrapServer());
291 props.put("linger.ms", 100);
292 props.put("key.serializer", BytesSerializer.class.getName());
293 props.put("value.serializer", BytesSerializer.class.getName());
294 testRecordProducer = new KafkaProducer<>(props);
296 props = new Properties();
297 props.put("bootstrap.servers", properties.getBootstrapServer());
298 props.put("client.id", "OFFSET-CONSUMER");
299 props.put("group.id", properties.getGroupId());
300 props.put("key.deserializer", BytesDeserializer.class.getName());
301 props.put("value.deserializer", BytesDeserializer.class.getName());
302 offsetConsumer = new KafkaConsumer<>(props);
306 oldOffsets = new HashMap<>();
307 newOffsets = new HashMap<>();
308 receivedRecords = new HashSet<>();
310 doForCurrentOffsets((tp, offset) ->
312 oldOffsets.put(tp, offset - 1);
313 newOffsets.put(tp, offset - 1);
316 Consumer<ConsumerRecord<K, V>> captureOffsetAndExecuteTestHandler =
320 new TopicPartition(record.topic(), record.partition()),
322 receivedRecords.add(record);
323 consumer.accept(record);
327 new EndlessConsumer<>(
329 properties.getClientId(),
330 properties.getTopic(),
332 captureOffsetAndExecuteTestHandler);
334 endlessConsumer.start();
342 endlessConsumer.stop();
343 testRecordProducer.close();
344 offsetConsumer.close();
348 log.info("Exception while stopping the consumer: {}", e.toString());
354 @Import(ApplicationConfiguration.class)
355 public static class Configuration