1 package de.juplo.kafka;
3 import com.mongodb.client.MongoClient;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerRecord;
6 import org.apache.kafka.clients.consumer.KafkaConsumer;
7 import org.apache.kafka.clients.producer.KafkaProducer;
8 import org.apache.kafka.clients.producer.ProducerRecord;
9 import org.apache.kafka.common.TopicPartition;
10 import org.apache.kafka.common.errors.RecordDeserializationException;
11 import org.apache.kafka.common.serialization.*;
12 import org.apache.kafka.common.utils.Bytes;
13 import org.junit.jupiter.api.*;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
16 import org.springframework.boot.autoconfigure.mongo.MongoProperties;
17 import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
18 import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
19 import org.springframework.boot.test.context.TestConfiguration;
20 import org.springframework.context.annotation.Import;
21 import org.springframework.kafka.test.context.EmbeddedKafka;
22 import org.springframework.test.context.TestPropertySource;
23 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
25 import java.time.Duration;
27 import java.util.concurrent.ExecutorService;
28 import java.util.function.BiConsumer;
29 import java.util.function.Consumer;
30 import java.util.stream.Collectors;
31 import java.util.stream.IntStream;
33 import static de.juplo.kafka.GenericApplicationTests.PARTITIONS;
34 import static de.juplo.kafka.GenericApplicationTests.TOPIC;
35 import static org.assertj.core.api.Assertions.*;
36 import static org.awaitility.Awaitility.*;
39 @SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
42 "sumup.adder.bootstrap-server=${spring.embedded.kafka.brokers}",
43 "sumup.adder.topic=" + TOPIC,
44 "sumup.adder.commit-interval=500ms",
45 "spring.mongodb.embedded.version=4.4.13" })
46 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
47 @EnableAutoConfiguration
48 @AutoConfigureDataMongo
50 abstract class GenericApplicationTests<K, V>
52 public static final String TOPIC = "FOO";
53 public static final int PARTITIONS = 10;
57 KafkaConsumer<K, V> kafkaConsumer;
59 Consumer<ConsumerRecord<K, V>> consumer;
61 ApplicationProperties properties;
63 ExecutorService executor;
65 StateRepository stateRepository;
67 MongoClient mongoClient;
69 MongoProperties mongoProperties;
71 RebalanceListener rebalanceListener;
73 RecordHandler<K, V> recordHandler;
75 KafkaProducer<Bytes, Bytes> testRecordProducer;
76 KafkaConsumer<Bytes, Bytes> offsetConsumer;
77 EndlessConsumer<K, V> endlessConsumer;
78 Map<TopicPartition, Long> oldOffsets;
79 Map<TopicPartition, Long> seenOffsets;
80 Set<ConsumerRecord<K, V>> receivedRecords;
83 final RecordGenerator recordGenerator;
84 final Consumer<ProducerRecord<Bytes, Bytes>> messageSender;
86 public GenericApplicationTests(RecordGenerator recordGenerator)
88 this.recordGenerator = recordGenerator;
89 this.messageSender = (record) -> sendMessage(record);
96 void commitsCurrentOffsetsOnSuccess() throws Exception
98 int numberOfGeneratedMessages =
99 recordGenerator.generate(false, false, messageSender);
101 await(numberOfGeneratedMessages + " records received")
102 .atMost(Duration.ofSeconds(30))
103 .pollInterval(Duration.ofSeconds(1))
104 .until(() -> receivedRecords.size() >= numberOfGeneratedMessages);
106 await("Offsets committed")
107 .atMost(Duration.ofSeconds(10))
108 .pollInterval(Duration.ofSeconds(1))
111 checkSeenOffsetsForProgress();
112 assertSeenOffsetsEqualCommittedOffsets(seenOffsets);
115 assertThatExceptionOfType(IllegalStateException.class)
116 .isThrownBy(() -> endlessConsumer.exitStatus())
117 .describedAs("Consumer should still be running");
119 endlessConsumer.stop();
120 recordGenerator.assertBusinessLogic();
124 @SkipWhenErrorCannotBeGenerated(poisonPill = true)
125 void commitsOffsetOfErrorForReprocessingOnDeserializationError()
127 int numberOfGeneratedMessages =
128 recordGenerator.generate(true, false, messageSender);
130 await("Consumer failed")
131 .atMost(Duration.ofSeconds(30))
132 .pollInterval(Duration.ofSeconds(1))
133 .until(() -> !endlessConsumer.running());
135 checkSeenOffsetsForProgress();
136 assertSeenOffsetsEqualCommittedOffsets(seenOffsets);
138 endlessConsumer.start();
139 await("Consumer failed")
140 .atMost(Duration.ofSeconds(30))
141 .pollInterval(Duration.ofSeconds(1))
142 .until(() -> !endlessConsumer.running());
144 checkSeenOffsetsForProgress();
145 assertSeenOffsetsEqualCommittedOffsets(seenOffsets);
146 assertThat(receivedRecords.size())
147 .describedAs("Received not all sent events")
148 .isLessThan(numberOfGeneratedMessages);
150 assertThatNoException()
151 .describedAs("Consumer should not be running")
152 .isThrownBy(() -> endlessConsumer.exitStatus());
153 assertThat(endlessConsumer.exitStatus())
154 .describedAs("Consumer should have exited abnormally")
155 .containsInstanceOf(RecordDeserializationException.class);
157 recordGenerator.assertBusinessLogic();
161 @SkipWhenErrorCannotBeGenerated(logicError = true)
162 void doesNotCommitOffsetsOnLogicError()
164 int numberOfGeneratedMessages =
165 recordGenerator.generate(false, true, messageSender);
167 await("Consumer failed")
168 .atMost(Duration.ofSeconds(30))
169 .pollInterval(Duration.ofSeconds(1))
170 .until(() -> !endlessConsumer.running());
172 checkSeenOffsetsForProgress();
173 assertSeenOffsetsAreBehindCommittedOffsets(seenOffsets);
175 endlessConsumer.start();
176 await("Consumer failed")
177 .atMost(Duration.ofSeconds(30))
178 .pollInterval(Duration.ofSeconds(1))
179 .until(() -> !endlessConsumer.running());
181 assertSeenOffsetsAreBehindCommittedOffsets(seenOffsets);
183 assertThatNoException()
184 .describedAs("Consumer should not be running")
185 .isThrownBy(() -> endlessConsumer.exitStatus());
186 assertThat(endlessConsumer.exitStatus())
187 .describedAs("Consumer should have exited abnormally")
188 .containsInstanceOf(RuntimeException.class);
190 recordGenerator.assertBusinessLogic();
194 /** Helper methods for the verification of expectations */
196 void assertSeenOffsetsEqualCommittedOffsets(Map<TopicPartition, Long> offsetsToCheck)
198 doForCurrentOffsets((tp, offset) ->
200 Long expected = offsetsToCheck.get(tp) + 1;
201 log.debug("Checking, if the offset {} for {} is exactly {}", offset, tp, expected);
203 .describedAs("Committed offset corresponds to the offset of the consumer")
204 .isEqualTo(expected);
208 void assertSeenOffsetsAreBehindCommittedOffsets(Map<TopicPartition, Long> offsetsToCheck)
210 List<Boolean> isOffsetBehindSeen = new LinkedList<>();
212 doForCurrentOffsets((tp, offset) ->
214 Long expected = offsetsToCheck.get(tp) + 1;
215 log.debug("Checking, if the offset {} for {} is at most {}", offset, tp, expected);
217 .describedAs("Committed offset must be at most equal to the offset of the consumer")
218 .isLessThanOrEqualTo(expected);
219 isOffsetBehindSeen.add(offset < expected);
222 assertThat(isOffsetBehindSeen.stream().reduce(false, (result, next) -> result | next))
223 .describedAs("Committed offsets are behind seen offsets")
227 void checkSeenOffsetsForProgress()
229 // Be sure, that some messages were consumed...!
230 Set<TopicPartition> withProgress = new HashSet<>();
231 partitions().forEach(tp ->
233 Long oldOffset = oldOffsets.get(tp) + 1;
234 Long newOffset = seenOffsets.get(tp) + 1;
235 if (!oldOffset.equals(newOffset))
237 log.debug("Progress for {}: {} -> {}", tp, oldOffset, newOffset);
238 withProgress.add(tp);
241 assertThat(withProgress)
242 .describedAs("Some offsets must have changed, compared to the old offset-positions")
247 /** Helper methods for setting up and running the tests */
251 offsetConsumer.assign(partitions());
252 partitions().forEach(tp ->
254 Long offset = offsetConsumer.position(tp);
255 log.info("New position for {}: {}", tp, offset);
256 Integer partition = tp.partition();
257 StateDocument document =
259 .findById(partition.toString())
260 .orElse(new StateDocument(partition));
261 document.offset = offset;
262 stateRepository.save(document);
264 offsetConsumer.unsubscribe();
267 void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
269 partitions().forEach(tp ->
271 String partition = Integer.toString(tp.partition());
272 Optional<Long> offset = stateRepository.findById(partition).map(document -> document.offset);
273 consumer.accept(tp, offset.orElse(0l));
277 List<TopicPartition> partitions()
281 .range(0, PARTITIONS)
282 .mapToObj(partition -> new TopicPartition(TOPIC, partition))
283 .collect(Collectors.toList());
287 public interface RecordGenerator
292 Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
294 default boolean canGeneratePoisonPill()
299 default boolean canGenerateLogicError()
304 default void assertBusinessLogic()
306 log.debug("No business-logic to assert");
310 void sendMessage(ProducerRecord<Bytes, Bytes> record)
312 testRecordProducer.send(record, (metadata, e) ->
314 if (metadata != null)
318 metadata.partition(),
326 "Exception for {}={}: {}",
339 props = new Properties();
340 props.put("bootstrap.servers", properties.getBootstrapServer());
341 props.put("linger.ms", 100);
342 props.put("key.serializer", BytesSerializer.class.getName());
343 props.put("value.serializer", BytesSerializer.class.getName());
344 testRecordProducer = new KafkaProducer<>(props);
346 props = new Properties();
347 props.put("bootstrap.servers", properties.getBootstrapServer());
348 props.put("client.id", "OFFSET-CONSUMER");
349 props.put("group.id", properties.getGroupId());
350 props.put("key.deserializer", BytesDeserializer.class.getName());
351 props.put("value.deserializer", BytesDeserializer.class.getName());
352 offsetConsumer = new KafkaConsumer<>(props);
354 mongoClient.getDatabase(mongoProperties.getDatabase()).drop();
357 oldOffsets = new HashMap<>();
358 seenOffsets = new HashMap<>();
359 receivedRecords = new HashSet<>();
361 doForCurrentOffsets((tp, offset) ->
363 oldOffsets.put(tp, offset - 1);
364 seenOffsets.put(tp, offset - 1);
367 TestRecordHandler<K, V> captureOffsetAndExecuteTestHandler =
368 new TestRecordHandler<K, V>(recordHandler)
371 public void onNewRecord(ConsumerRecord<K, V> record)
374 new TopicPartition(record.topic(), record.partition()),
376 receivedRecords.add(record);
381 new EndlessConsumer<>(
383 properties.getClientId(),
384 properties.getTopic(),
387 captureOffsetAndExecuteTestHandler);
389 endlessConsumer.start();
397 testRecordProducer.close();
398 offsetConsumer.close();
402 log.info("Exception while stopping the consumer: {}", e.toString());
408 @Import(ApplicationConfiguration.class)
409 public static class Configuration