Tests: Erste Version eines synchronen Integration-Test implementiert
[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.errors.WakeupException;
10 import org.apache.kafka.common.serialization.BytesSerializer;
11 import org.apache.kafka.common.serialization.LongSerializer;
12 import org.apache.kafka.common.serialization.StringSerializer;
13 import org.apache.kafka.common.utils.Bytes;
14 import org.junit.jupiter.api.Test;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
17 import org.springframework.boot.test.context.TestConfiguration;
18 import org.springframework.context.annotation.Bean;
19 import org.springframework.context.annotation.Import;
20 import org.springframework.kafka.test.context.EmbeddedKafka;
21 import org.springframework.test.context.TestPropertySource;
22 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
23
24 import java.util.*;
25 import java.util.concurrent.ExecutorService;
26 import java.util.function.BiConsumer;
27 import java.util.function.Consumer;
28 import java.util.function.Function;
29 import java.util.stream.Collectors;
30 import java.util.stream.IntStream;
31
32 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
33 import static de.juplo.kafka.ApplicationTests.TOPIC;
34 import static org.assertj.core.api.Assertions.assertThat;
35
36
37 @SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
38 @TestPropertySource(
39                 properties = {
40                                 "consumer.bootstrap-server=${spring.embedded.kafka.brokers}",
41                                 "consumer.topic=" + TOPIC })
42 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
43 @Slf4j
44 class ApplicationTests
45 {
46         public static final String TOPIC = "FOO";
47         public static final int PARTITIONS = 10;
48
49
50         StringSerializer stringSerializer = new StringSerializer();
51         LongSerializer longSerializer = new LongSerializer();
52
53         @Autowired
54         KafkaProducer<String, Bytes> kafkaProducer;
55         @Autowired
56         KafkaConsumer<String, Long> kafkaConsumer;
57         @Autowired
58         ApplicationProperties properties;
59         @Autowired
60         ExecutorService executor;
61
62
63         @Test
64         void commitsCurrentOffsetsOnSuccess()
65         {
66                 send100Messages(i ->  new Bytes(longSerializer.serialize(TOPIC, i)));
67
68                 Set<ConsumerRecord<String, Long>> received = new HashSet<>();
69                 Map<Integer, Long> offsets = runEndlessConsumer(record ->
70                 {
71                         received.add(record);
72                         if (received.size() == 100)
73                                 throw new WakeupException();
74                 });
75
76                 check(offsets);
77         }
78
79         @Test
80         void commitsNoOffsetsOnError()
81         {
82                 send100Messages(counter ->
83                                 counter == 77
84                                                 ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!"))
85                                                 : new Bytes(longSerializer.serialize(TOPIC, counter)));
86
87                 Map<Integer, Long> oldOffsets = new HashMap<>();
88                 doForCurrentOffsets((tp, offset) -> oldOffsets.put(tp.partition(), offset -1));
89                 Map<Integer, Long> newOffsets = runEndlessConsumer((record) -> {});
90
91                 check(oldOffsets);
92         }
93
94
95         void send100Messages(Function<Long, Bytes> messageGenerator)
96         {
97                 long i = 0;
98
99                 for (int partition = 0; partition < 10; partition++)
100                 {
101                         for (int key = 0; key < 10; key++)
102                         {
103                                 Bytes value = messageGenerator.apply(++i);
104
105                                 ProducerRecord<String, Bytes> record =
106                                                 new ProducerRecord<>(
107                                                                 TOPIC,
108                                                                 partition,
109                                                                 Integer.toString(key%2),
110                                                                 value);
111
112                                 kafkaProducer.send(record, (metadata, e) ->
113                                 {
114                                         if (metadata != null)
115                                         {
116                                                 log.debug(
117                                                                 "{}|{} - {}={}",
118                                                                 metadata.partition(),
119                                                                 metadata.offset(),
120                                                                 record.key(),
121                                                                 record.value());
122                                         }
123                                         else
124                                         {
125                                                 log.warn(
126                                                                 "Exception for {}={}: {}",
127                                                                 record.key(),
128                                                                 record.value(),
129                                                                 e.toString());
130                                         }
131                                 });
132                         }
133                 }
134         }
135
136         Map<Integer, Long> runEndlessConsumer(Consumer<ConsumerRecord<String, Long>> consumer)
137         {
138                 Map<Integer, Long> offsets = new HashMap<>();
139                 doForCurrentOffsets((tp, offset) -> offsets.put(tp.partition(), offset -1));
140                 Consumer<ConsumerRecord<String, Long>> captureOffset = record -> offsets.put(record.partition(), record.offset());
141                 EndlessConsumer<String, Long> endlessConsumer =
142                                 new EndlessConsumer<>(
143                                                 executor,
144                                                 properties.getClientId(),
145                                                 properties.getTopic(),
146                                                 kafkaConsumer,
147                                                 captureOffset.andThen(consumer));
148
149                 endlessConsumer.run();
150
151                 return offsets;
152         }
153
154         List<TopicPartition> partitions()
155         {
156                 return
157                                 IntStream
158                                                 .range(0, PARTITIONS)
159                                                 .mapToObj(partition -> new TopicPartition(TOPIC, partition))
160                                                 .collect(Collectors.toList());
161         }
162
163         void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
164         {
165                 kafkaConsumer.assign(partitions());
166                 partitions().forEach(tp -> consumer.accept(tp, kafkaConsumer.position(tp)));
167                 kafkaConsumer.unsubscribe();
168         }
169
170         void check(Map<Integer, Long> offsets)
171         {
172                 doForCurrentOffsets((tp, offset) ->
173                 {
174                         Long expected = offsets.get(tp.partition()) + 1;
175                         log.debug("Checking, if the offset for {} is {}", tp, expected);
176                         assertThat(offset).isEqualTo(expected);
177                 });
178         }
179
180
181         @TestConfiguration
182         @Import(ApplicationConfiguration.class)
183         public static class Configuration
184         {
185                 @Bean
186                 KafkaProducer<String, Bytes> kafkaProducer(ApplicationProperties properties)
187                 {
188                         Properties props = new Properties();
189                         props.put("bootstrap.servers", properties.getBootstrapServer());
190                         props.put("linger.ms", 100);
191                         props.put("key.serializer", StringSerializer.class.getName());
192                         props.put("value.serializer", BytesSerializer.class.getName());
193
194                         return new KafkaProducer<>(props);
195                 }
196         }
197 }