X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplicationTests.java;h=3ded0d267c1e61968c17df21bd01e57706d27013;hb=b8bdc40c0441591c1dc5ab5a3922db1c1dca2ed9;hp=e35b223666e6b1519292f513f5cd9280b1f5278c;hpb=2939f4b7bae23df34968ce3d87be1b83cf0fba90;p=demos%2Fkafka%2Ftraining diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index e35b223..3ded0d2 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTests.java +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -13,10 +13,12 @@ import org.apache.kafka.common.serialization.StringSerializer; import org.apache.kafka.common.utils.Bytes; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Primary; import org.springframework.kafka.test.context.EmbeddedKafka; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -24,7 +26,6 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import java.time.Duration; import java.util.*; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -37,7 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.*; -@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class) +@SpringJUnitConfig( + initializers = ConfigDataApplicationContextInitializer.class, + classes = { + EndlessConsumer.class, + KafkaAutoConfiguration.class, + ApplicationTests.Configuration.class }) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestPropertySource( properties = { @@ -57,18 +63,17 @@ class ApplicationTests @Autowired KafkaProducer kafkaProducer; @Autowired - KafkaConsumer kafkaConsumer; - @Autowired KafkaConsumer offsetConsumer; @Autowired ApplicationProperties properties; @Autowired - ExecutorService executor; + EndlessConsumer endlessConsumer; + @Autowired + RecordHandler recordHandler; - Consumer> testHandler; - EndlessConsumer endlessConsumer; Map oldOffsets; Map newOffsets; + Set> receivedRecords; /** Tests methods */ @@ -79,12 +84,9 @@ class ApplicationTests { send100Messages(i -> new Bytes(longSerializer.serialize(TOPIC, i))); - Set> received = new HashSet<>(); - testHandler = record -> received.add(record); - await("100 records received") .atMost(Duration.ofSeconds(30)) - .until(() -> received.size() >= 100); + .until(() -> receivedRecords.size() >= 100); await("Offsets committed") .atMost(Duration.ofSeconds(10)) @@ -97,7 +99,7 @@ class ApplicationTests @Test @Order(2) - void commitsNoOffsetsOnError() + void commitsOffsetOfErrorForReprocessingOnError() { send100Messages(counter -> counter == 77 @@ -106,10 +108,12 @@ class ApplicationTests await("Consumer failed") .atMost(Duration.ofSeconds(30)) - .until(() -> !endlessConsumer.running()); + .untilAsserted(() -> checkSeenOffsetsForProgress()); - checkSeenOffsetsForProgress(); - compareToCommitedOffsets(oldOffsets); + compareToCommitedOffsets(newOffsets); + assertThat(receivedRecords.size()) + .describedAs("Received not all sent events") + .isLessThan(100); } @@ -121,7 +125,9 @@ class ApplicationTests { Long expected = offsetsToCheck.get(tp) + 1; log.debug("Checking, if the offset for {} is {}", tp, expected); - assertThat(offset).isEqualTo(expected); + assertThat(offset) + .describedAs("Committed offset corresponds to the offset of the consumer") + .isEqualTo(expected); }); } @@ -139,7 +145,9 @@ class ApplicationTests withProgress.add(tp); } }); - assertThat(withProgress).isNotEmpty().describedAs("Found no partitions with any offset-progress"); + assertThat(withProgress) + .describedAs("Some offsets must have changed, compared to the old offset-positions") + .isNotEmpty(); } @@ -207,10 +215,11 @@ class ApplicationTests @BeforeEach public void init() { - testHandler = record -> {} ; + recordHandler.testHandler = (record) -> {}; oldOffsets = new HashMap<>(); newOffsets = new HashMap<>(); + receivedRecords = new HashSet<>(); doForCurrentOffsets((tp, offset) -> { @@ -218,23 +227,15 @@ class ApplicationTests newOffsets.put(tp, offset - 1); }); - Consumer> captureOffsetAndExecuteTestHandler = + recordHandler.captureOffsets = record -> { + receivedRecords.add(record); newOffsets.put( new TopicPartition(record.topic(), record.partition()), record.offset()); - testHandler.accept(record); }; - endlessConsumer = - new EndlessConsumer<>( - executor, - properties.getClientId(), - properties.getTopic(), - kafkaConsumer, - captureOffsetAndExecuteTestHandler); - endlessConsumer.start(); } @@ -251,11 +252,32 @@ class ApplicationTests } } + public static class RecordHandler implements Consumer> + { + Consumer> captureOffsets; + Consumer> testHandler; + + + @Override + public void accept(ConsumerRecord record) + { + captureOffsets + .andThen(testHandler) + .accept(record); + } + } @TestConfiguration @Import(ApplicationConfiguration.class) public static class Configuration { + @Primary + @Bean + public Consumer> testHandler() + { + return new RecordHandler(); + } + @Bean KafkaProducer kafkaProducer(ApplicationProperties properties) {