Springify: ROT - Merge des verschärften Tests aus der Vanilla-Version
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
index cbf215e..3ded0d2 100644 (file)
@@ -67,10 +67,13 @@ class ApplicationTests
        @Autowired
        ApplicationProperties properties;
        @Autowired
+       EndlessConsumer endlessConsumer;
+       @Autowired
        RecordHandler recordHandler;
 
        Map<TopicPartition, Long> oldOffsets;
        Map<TopicPartition, Long> newOffsets;
+       Set<ConsumerRecord<String, Long>> receivedRecords;
 
 
        /** Tests methods */
@@ -81,12 +84,9 @@ class ApplicationTests
        {
                send100Messages(i ->  new Bytes(longSerializer.serialize(TOPIC, i)));
 
-               Set<ConsumerRecord<String, Long>> received = new HashSet<>();
-               recordHandler.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))
@@ -111,6 +111,9 @@ class ApplicationTests
                                .untilAsserted(() -> checkSeenOffsetsForProgress());
 
                compareToCommitedOffsets(newOffsets);
+               assertThat(receivedRecords.size())
+                               .describedAs("Received not all sent events")
+                               .isLessThan(100);
        }
 
 
@@ -122,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);
                });
        }
 
@@ -140,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();
        }
 
 
@@ -212,6 +219,7 @@ class ApplicationTests
 
                oldOffsets = new HashMap<>();
                newOffsets = new HashMap<>();
+               receivedRecords = new HashSet<>();
 
                doForCurrentOffsets((tp, offset) ->
                {
@@ -221,11 +229,28 @@ class ApplicationTests
 
                recordHandler.captureOffsets =
                                record ->
+                               {
+                                       receivedRecords.add(record);
                                        newOffsets.put(
                                                        new TopicPartition(record.topic(), record.partition()),
                                                        record.offset());
+                               };
+
+               endlessConsumer.start();
        }
 
+       @AfterEach
+       public void deinit()
+       {
+               try
+               {
+                       endlessConsumer.stop();
+               }
+               catch (Exception e)
+               {
+                       log.info("Exception while stopping the consumer: {}", e.toString());
+               }
+       }
 
        public static class RecordHandler implements Consumer<ConsumerRecord<String, Long>>
        {