projects
/
demos
/
kafka
/
training
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Benennung vereinheitlicht und projektunabhängig gemacht
[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
1aacb94
..
9a6f812
100644
(file)
--- a/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
+++ b/
src/test/java/de/juplo/kafka/GenericApplicationTests.java
@@
-11,6
+11,8
@@
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.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
+37,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=1s" })
+ "sumup.adder.bootstrap-server=${spring.embedded.kafka.brokers}",
+ "sumup.adder.topic=" + TOPIC,
+ "sumup.adder.commit-interval=1s",
+ "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
+59,12
@@
abstract class GenericApplicationTests<K, V>
ApplicationProperties properties;
@Autowired
ExecutorService executor;
ApplicationProperties properties;
@Autowired
ExecutorService executor;
+ @Autowired
+ StateRepository stateRepository;
+ @Autowired
+ PollIntervalAwareConsumerRebalanceListener rebalanceListener;
+ @Autowired
+ RecordHandler<K, V> recordHandler;
KafkaProducer<Bytes, Bytes> testRecordProducer;
KafkaConsumer<Bytes, Bytes> offsetConsumer;
KafkaProducer<Bytes, Bytes> testRecordProducer;
KafkaConsumer<Bytes, Bytes> offsetConsumer;
@@
-78,12
+89,13
@@
abstract class GenericApplicationTests<K, V>
@Test
void commitsCurrentOffsetsOnSuccess()
{
@Test
void commitsCurrentOffsetsOnSuccess()
{
- recordGenerator.generate(100, Set.of(), Set.of(), messageSender);
+ int numberOfGeneratedMessages =
+ recordGenerator.generate(false, false, messageSender);
- await(
"100
records received")
+ await(
numberOfGeneratedMessages + "
records received")
.atMost(Duration.ofSeconds(30))
.pollInterval(Duration.ofSeconds(1))
.atMost(Duration.ofSeconds(30))
.pollInterval(Duration.ofSeconds(1))
- .until(() -> receivedRecords.size() >=
100
);
+ .until(() -> receivedRecords.size() >=
numberOfGeneratedMessages
);
await("Offsets committed")
.atMost(Duration.ofSeconds(10))
await("Offsets committed")
.atMost(Duration.ofSeconds(10))
@@
-97,13
+109,16
@@
abstract class GenericApplicationTests<K, V>
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
+
+ recordGenerator.assertBusinessLogic();
}
@Test
@SkipWhenErrorCannotBeGenerated(poisonPill = true)
void commitsOffsetOfErrorForReprocessingOnDeserializationError()
{
}
@Test
@SkipWhenErrorCannotBeGenerated(poisonPill = true)
void commitsOffsetOfErrorForReprocessingOnDeserializationError()
{
- recordGenerator.generate(100, Set.of(77), Set.of(), messageSender);
+ int numberOfGeneratedMessages =
+ recordGenerator.generate(true, false, messageSender);
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
@@
-123,7
+138,7
@@
abstract class GenericApplicationTests<K, V>
compareToCommitedOffsets(newOffsets);
assertThat(receivedRecords.size())
.describedAs("Received not all sent events")
compareToCommitedOffsets(newOffsets);
assertThat(receivedRecords.size())
.describedAs("Received not all sent events")
- .isLessThan(
100
);
+ .isLessThan(
numberOfGeneratedMessages
);
assertThatNoException()
.describedAs("Consumer should not be running")
assertThatNoException()
.describedAs("Consumer should not be running")
@@
-131,13
+146,16
@@
abstract class GenericApplicationTests<K, V>
assertThat(endlessConsumer.exitStatus())
.describedAs("Consumer should have exited abnormally")
.containsInstanceOf(RecordDeserializationException.class);
assertThat(endlessConsumer.exitStatus())
.describedAs("Consumer should have exited abnormally")
.containsInstanceOf(RecordDeserializationException.class);
+
+ recordGenerator.assertBusinessLogic();
}
@Test
@SkipWhenErrorCannotBeGenerated(logicError = true)
void doesNotCommitOffsetsOnLogicError()
{
}
@Test
@SkipWhenErrorCannotBeGenerated(logicError = true)
void doesNotCommitOffsetsOnLogicError()
{
- recordGenerator.generate(100, Set.of(), Set.of(77), messageSender);
+ int numberOfGeneratedMessages =
+ recordGenerator.generate(false, true, messageSender);
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
await("Consumer failed")
.atMost(Duration.ofSeconds(30))
@@
-157,7
+175,7
@@
abstract class GenericApplicationTests<K, V>
compareToCommitedOffsets(oldOffsets);
assertThat(receivedRecords.size())
.describedAs("Received not all sent events")
compareToCommitedOffsets(oldOffsets);
assertThat(receivedRecords.size())
.describedAs("Received not all sent events")
- .isLessThan(
100
);
+ .isLessThan(
numberOfGeneratedMessages
);
assertThatNoException()
.describedAs("Consumer should not be running")
assertThatNoException()
.describedAs("Consumer should not be running")
@@
-165,6
+183,8
@@
abstract class GenericApplicationTests<K, V>
assertThat(endlessConsumer.exitStatus())
.describedAs("Consumer should have exited abnormally")
.containsInstanceOf(RuntimeException.class);
assertThat(endlessConsumer.exitStatus())
.describedAs("Consumer should have exited abnormally")
.containsInstanceOf(RuntimeException.class);
+
+ recordGenerator.assertBusinessLogic();
}
}
@@
-207,23
+227,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()
@@
-238,12
+264,12
@@
abstract class GenericApplicationTests<K, V>
public interface RecordGenerator
{
public interface RecordGenerator
{
-
void
generate(
-
Set<Integer>
poisonPills,
-
Set<Integer>
logicErrors,
+
int
generate(
+
boolean
poisonPills,
+
boolean
logicErrors,
Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
- default boolean canGeneratePois
i
onPill()
+ default boolean canGeneratePoisonPill()
{
return true;
}
{
return true;
}
@@
-252,6
+278,11
@@
abstract class GenericApplicationTests<K, V>
{
return true;
}
{
return true;
}
+
+ default void assertBusinessLogic()
+ {
+ log.debug("No business-logic to assert");
+ }
}
void sendMessage(ProducerRecord<Bytes, Bytes> record)
}
void sendMessage(ProducerRecord<Bytes, Bytes> record)
@@
-310,14
+341,17
@@
abstract class GenericApplicationTests<K, V>
newOffsets.put(tp, offset - 1);
});
newOffsets.put(tp, offset - 1);
});
-
Consumer<ConsumerRecord<K, V>
> captureOffsetAndExecuteTestHandler =
- record ->
+
TestRecordHandler<K, V
> captureOffsetAndExecuteTestHandler =
+ new TestRecordHandler<K, V>(recordHandler)
{
{
- newOffsets.put(
- new TopicPartition(record.topic(), record.partition()),
- record.offset());
- receivedRecords.add(record);
- consumer.accept(record);
+ @Override
+ public void onNewRecord(ConsumerRecord<K, V> record)
+ {
+ newOffsets.put(
+ new TopicPartition(record.topic(), record.partition()),
+ record.offset());
+ receivedRecords.add(record);
+ }
};
endlessConsumer =
};
endlessConsumer =
@@
-326,6
+360,7
@@
abstract class GenericApplicationTests<K, V>
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
+ rebalanceListener,
captureOffsetAndExecuteTestHandler);
endlessConsumer.start();
captureOffsetAndExecuteTestHandler);
endlessConsumer.start();