projects
/
demos
/
kafka
/
training
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Funktionsunabhängiger Name für das erweiterte Interface RebalanceListener
[demos/kafka/training]
/
src
/
test
/
java
/
de
/
juplo
/
kafka
/
GenericApplicationTests.java
diff --git
a/src/test/java/de/juplo/kafka/GenericApplicationTests.java
b/src/test/java/de/juplo/kafka/GenericApplicationTests.java
index
649cdba
..
a8fa7ea
100644
(file)
--- a/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
+++ b/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
@@
-1,5
+1,6
@@
package de.juplo.kafka;
package de.juplo.kafka;
+import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
@@
-11,6
+12,9
@@
import org.apache.kafka.common.serialization.*;
import org.apache.kafka.common.utils.Bytes;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.kafka.common.utils.Bytes;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.mongo.MongoProperties;
+import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Import;
@@
-35,10
+39,13
@@
import static org.awaitility.Awaitility.*;
@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
@TestPropertySource(
properties = {
@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class)
@TestPropertySource(
properties = {
- "consumer.bootstrap-server=${spring.embedded.kafka.brokers}",
- "consumer.topic=" + TOPIC,
- "consumer.commit-interval=500ms" })
+ "sumup.adder.bootstrap-server=${spring.embedded.kafka.brokers}",
+ "sumup.adder.topic=" + TOPIC,
+ "sumup.adder.commit-interval=500ms",
+ "spring.mongodb.embedded.version=4.4.13" })
@EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
@EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
+@EnableAutoConfiguration
+@AutoConfigureDataMongo
@Slf4j
abstract class GenericApplicationTests<K, V>
{
@Slf4j
abstract class GenericApplicationTests<K, V>
{
@@
-54,6
+61,16
@@
abstract class GenericApplicationTests<K, V>
ApplicationProperties properties;
@Autowired
ExecutorService executor;
ApplicationProperties properties;
@Autowired
ExecutorService executor;
+ @Autowired
+ StateRepository stateRepository;
+ @Autowired
+ MongoClient mongoClient;
+ @Autowired
+ MongoProperties mongoProperties;
+ @Autowired
+ RebalanceListener rebalanceListener;
+ @Autowired
+ RecordHandler<K, V> recordHandler;
KafkaProducer<Bytes, Bytes> testRecordProducer;
KafkaConsumer<Bytes, Bytes> offsetConsumer;
KafkaProducer<Bytes, Bytes> testRecordProducer;
KafkaConsumer<Bytes, Bytes> offsetConsumer;
@@
-76,7
+93,7
@@
abstract class GenericApplicationTests<K, V>
/** Tests methods */
@Test
/** Tests methods */
@Test
- void commitsCurrentOffsetsOnSuccess()
+ void commitsCurrentOffsetsOnSuccess()
throws Exception
{
int numberOfGeneratedMessages =
recordGenerator.generate(false, false, messageSender);
{
int numberOfGeneratedMessages =
recordGenerator.generate(false, false, messageSender);
@@
-99,6
+116,7
@@
abstract class GenericApplicationTests<K, V>
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
+ endlessConsumer.stop();
recordGenerator.assertBusinessLogic();
}
recordGenerator.assertBusinessLogic();
}
@@
-196,7
+214,7
@@
abstract class GenericApplicationTests<K, V>
Long expected = offsetsToCheck.get(tp) + 1;
log.debug("Checking, if the offset {} for {} is at most {}", offset, tp, expected);
assertThat(offset)
Long expected = offsetsToCheck.get(tp) + 1;
log.debug("Checking, if the offset {} for {} is at most {}", offset, tp, expected);
assertThat(offset)
- .describedAs("Committed offset
corresponds
to the offset of the consumer")
+ .describedAs("Committed offset
must be at most equal
to the offset of the consumer")
.isLessThanOrEqualTo(expected);
isOffsetBehindSeen.add(offset < expected);
});
.isLessThanOrEqualTo(expected);
isOffsetBehindSeen.add(offset < expected);
});
@@
-231,23
+249,29
@@
abstract class GenericApplicationTests<K, V>
void seekToEnd()
{
offsetConsumer.assign(partitions());
void seekToEnd()
{
offsetConsumer.assign(partitions());
- offsetConsumer.seekToEnd(partitions());
partitions().forEach(tp ->
{
partitions().forEach(tp ->
{
- // seekToEnd() works lazily: it only takes effect on poll()/position()
Long offset = offsetConsumer.position(tp);
log.info("New position for {}: {}", tp, offset);
Long offset = offsetConsumer.position(tp);
log.info("New position for {}: {}", tp, offset);
+ Integer partition = tp.partition();
+ StateDocument document =
+ stateRepository
+ .findById(partition.toString())
+ .orElse(new StateDocument(partition));
+ document.offset = offset;
+ stateRepository.save(document);
});
});
- // The new positions must be commited!
- offsetConsumer.commitSync();
offsetConsumer.unsubscribe();
}
void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
{
offsetConsumer.unsubscribe();
}
void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
{
- offsetConsumer.assign(partitions());
- partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
- offsetConsumer.unsubscribe();
+ partitions().forEach(tp ->
+ {
+ String partition = Integer.toString(tp.partition());
+ Optional<Long> offset = stateRepository.findById(partition).map(document -> document.offset);
+ consumer.accept(tp, offset.orElse(0l));
+ });
}
List<TopicPartition> partitions()
}
List<TopicPartition> partitions()
@@
-327,6
+351,7
@@
abstract class GenericApplicationTests<K, V>
props.put("value.deserializer", BytesDeserializer.class.getName());
offsetConsumer = new KafkaConsumer<>(props);
props.put("value.deserializer", BytesDeserializer.class.getName());
offsetConsumer = new KafkaConsumer<>(props);
+ mongoClient.getDatabase(mongoProperties.getDatabase()).drop();
seekToEnd();
oldOffsets = new HashMap<>();
seekToEnd();
oldOffsets = new HashMap<>();
@@
-339,14
+364,17
@@
abstract class GenericApplicationTests<K, V>
seenOffsets.put(tp, offset - 1);
});
seenOffsets.put(tp, offset - 1);
});
-
Consumer<ConsumerRecord<K, V>
> captureOffsetAndExecuteTestHandler =
- record ->
+
TestRecordHandler<K, V
> captureOffsetAndExecuteTestHandler =
+ new TestRecordHandler<K, V>(recordHandler)
{
{
- seenOffsets.put(
- new TopicPartition(record.topic(), record.partition()),
- record.offset());
- receivedRecords.add(record);
- consumer.accept(record);
+ @Override
+ public void onNewRecord(ConsumerRecord<K, V> record)
+ {
+ seenOffsets.put(
+ new TopicPartition(record.topic(), record.partition()),
+ record.offset());
+ receivedRecords.add(record);
+ }
};
endlessConsumer =
};
endlessConsumer =
@@
-355,6
+383,7
@@
abstract class GenericApplicationTests<K, V>
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
+ rebalanceListener,
captureOffsetAndExecuteTestHandler);
endlessConsumer.start();
captureOffsetAndExecuteTestHandler);
endlessConsumer.start();
@@
-365,7
+394,6
@@
abstract class GenericApplicationTests<K, V>
{
try
{
{
try
{
- endlessConsumer.stop();
testRecordProducer.close();
offsetConsumer.close();
}
testRecordProducer.close();
offsetConsumer.close();
}