Springify: Gemeinsame DLQ für Poison Pills und Fachlogik-Fehler konfiguriert
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index 52ecc0f..43a4f61 100644 (file)
@@ -6,7 +6,6 @@ import org.apache.kafka.clients.consumer.KafkaConsumer;
 import org.apache.kafka.clients.producer.KafkaProducer;
 import org.apache.kafka.clients.producer.ProducerRecord;
 import org.apache.kafka.common.TopicPartition;
-import org.apache.kafka.common.errors.RecordDeserializationException;
 import org.apache.kafka.common.serialization.*;
 import org.apache.kafka.common.utils.Bytes;
 import org.junit.jupiter.api.*;
@@ -18,7 +17,6 @@ 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.listener.MessageListenerContainer;
 import org.springframework.kafka.support.serializer.JsonSerializer;
 import org.springframework.kafka.test.context.EmbeddedKafka;
 import org.springframework.test.context.TestPropertySource;
@@ -45,7 +43,6 @@ import static org.awaitility.Awaitility.*;
                                EndlessConsumer.class,
                                KafkaAutoConfiguration.class,
                                ApplicationTests.Configuration.class })
-@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 @TestPropertySource(
                properties = {
                                "spring.kafka.consumer.bootstrap-servers=${spring.embedded.kafka.brokers}",
@@ -86,7 +83,6 @@ class ApplicationTests
        /** Tests methods */
 
        @Test
-       @Order(1) // << The poistion pill is not skipped. Hence, this test must run first
        void commitsCurrentOffsetsOnSuccess() throws ExecutionException, InterruptedException
        {
                send100Messages((key, counter) -> serialize(key, counter));
@@ -109,8 +105,7 @@ class ApplicationTests
        }
 
        @Test
-       @Order(2)
-       void commitsCurrentOffsetsOnError()
+       void commitsCurrentOffsetsOnDeserializationError()
        {
                send100Messages((key, counter) ->
                                counter == 77
@@ -141,6 +136,34 @@ class ApplicationTests
                                .isTrue();
        }
 
+       @Test
+       void commitsOffsetOnProgramLogicErrorFoo()
+       {
+               recordHandler.testHandler = (record) ->
+               {
+                       if (Integer.parseInt(record.value().message)%10 ==0)
+                               throw new RuntimeException("BOOM: " + record.value().message + "%10 == 0");
+               };
+
+               send100Messages((key, counter) -> serialize(key, counter));
+
+               await("80 records received")
+                               .atMost(Duration.ofSeconds(30))
+                               .until(() -> receivedRecords.size() == 100);
+
+               await("Offsets committed")
+                               .atMost(Duration.ofSeconds(10))
+                               .untilAsserted(() ->
+                               {
+                                       checkSeenOffsetsForProgress();
+                                       compareToCommitedOffsets(newOffsets);
+                               });
+
+               assertThat(endlessConsumer.isRunning())
+                               .describedAs("Consumer should still be running")
+                               .isTrue();
+       }
+
 
        /** Helper methods for the verification of expectations */
 
@@ -149,7 +172,7 @@ class ApplicationTests
                doForCurrentOffsets((tp, offset) ->
                {
                        Long expected = offsetsToCheck.get(tp) + 1;
-                       log.debug("Checking, if the offset for {} is {}", tp, expected);
+                       log.debug("TEST: Comparing the expected offset of {} for {} to {}", expected, tp, offset);
                        assertThat(offset)
                                        .describedAs("Committed offset corresponds to the offset of the consumer")
                                        .isEqualTo(expected);
@@ -166,10 +189,11 @@ class ApplicationTests
                        Long newOffset = newOffsets.get(tp);
                        if (!oldOffset.equals(newOffset))
                        {
-                               log.debug("Progress for {}: {} -> {}", tp, oldOffset, newOffset);
+                               log.debug("TEST: Progress for {}: {} -> {}", tp, oldOffset, newOffset);
                                withProgress.add(tp);
                        }
                });
+               log.debug("TEST: Offsets with progress: {}", withProgress);
                assertThat(withProgress)
                                .describedAs("Some offsets must have changed, compared to the old offset-positions")
                                .isNotEmpty();
@@ -218,7 +242,7 @@ class ApplicationTests
                                        if (metadata != null)
                                        {
                                                log.debug(
-                                                               "{}|{} - {}={}",
+                                                               "TEST: Sending partition={}, offset={} - {}={}",
                                                                metadata.partition(),
                                                                metadata.offset(),
                                                                record.key(),
@@ -227,7 +251,7 @@ class ApplicationTests
                                        else
                                        {
                                                log.warn(
-                                                               "Exception for {}={}: {}",
+                                                               "TEST: Exception for {}={}: {}",
                                                                record.key(),
                                                                record.value(),
                                                                e.toString());
@@ -265,6 +289,7 @@ class ApplicationTests
                                record ->
                                {
                                        receivedRecords.add(record);
+                                       log.debug("TEST: Processing record #{}: {}", receivedRecords.size(), record.value());
                                        newOffsets.put(
                                                        new TopicPartition(record.topic(), record.partition()),
                                                        record.offset());
@@ -282,7 +307,7 @@ class ApplicationTests
                }
                catch (Exception e)
                {
-                       log.info("Exception while stopping the consumer: {}", e.toString());
+                       log.info("TEST: Exception while stopping the consumer: {}", e.toString());
                }
        }