Signatur und Handling des `RecordGenerator` vereinfacht/überarbeitet
authorKai Moritz <kai@juplo.de>
Sun, 14 Aug 2022 13:35:28 +0000 (15:35 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 14 Aug 2022 16:03:59 +0000 (18:03 +0200)
* Der `RecordGenerator` darf jetzt selbst bestimmen, wie viele Nachrichten
  er erzeugt und wo wieviele Poison-Pills oder Logik-Fehler erzeugt
  werden, wenn der Test dies anfordert.
* Dafür git der `RecordGenerator` jetzt die Anzahl der tatsächlich
  erzeugten Nachrichten zurück, damit die Tests richtig reagieren können.

src/test/java/de/juplo/kafka/ApplicationTests.java
src/test/java/de/juplo/kafka/ErrorCannotBeGeneratedCondition.java
src/test/java/de/juplo/kafka/GenericApplicationTests.java

index 1272124..8369a7b 100644 (file)
@@ -27,9 +27,9 @@ public class ApplicationTests extends GenericApplicationTests<String, Long>
 
 
           @Override
-          public void generate(
-              Set<Integer> poisonPills,
-              Set<Integer> logicErrors,
+          public int generate(
+              boolean poisonPills,
+              boolean logicErrors,
               Consumer<ProducerRecord<Bytes, Bytes>> messageSender)
           {
             int i = 0;
@@ -38,17 +38,19 @@ public class ApplicationTests extends GenericApplicationTests<String, Long>
             {
               for (int key = 0; key < 10; key++)
               {
-                if (++i > 100)
-                  return;
+                i++;
 
                 Bytes value = new Bytes(longSerializer.serialize(TOPIC, (long)i));
-                if (logicErrors.contains(i))
+                if (i == 77)
                 {
-                  value = new Bytes(longSerializer.serialize(TOPIC, Long.MIN_VALUE));
-                }
-                if (poisonPills.contains(i))
-                {
-                  value = new Bytes(stringSerializer.serialize(TOPIC, "BOOM (Poison-Pill)!"));
+                  if (logicErrors)
+                  {
+                    value = new Bytes(longSerializer.serialize(TOPIC, Long.MIN_VALUE));
+                  }
+                  if (poisonPills)
+                  {
+                    value = new Bytes(stringSerializer.serialize(TOPIC, "BOOM (Poison-Pill)!"));
+                  }
                 }
 
                 ProducerRecord<Bytes, Bytes> record =
@@ -61,6 +63,8 @@ public class ApplicationTests extends GenericApplicationTests<String, Long>
                 messageSender.accept(record);
               }
             }
+
+            return i;
           }
         });
   }
index 99af3b2..606218f 100644 (file)
@@ -30,7 +30,7 @@ public class ErrorCannotBeGeneratedCondition implements ExecutionCondition
       GenericApplicationTests instance = (GenericApplicationTests)context.getTestInstance().get();
       List<String> missingRequiredErrors = new LinkedList<>();
 
-      if (skipWhenErrorCannotBeGenerated.poisonPill() && !instance.recordGenerator.canGeneratePoisionPill())
+      if (skipWhenErrorCannotBeGenerated.poisonPill() && !instance.recordGenerator.canGeneratePoisonPill())
         missingRequiredErrors.add("Poison-Pill");
 
       if (skipWhenErrorCannotBeGenerated.logicError() && !instance.recordGenerator.canGenerateLogicError())
index 1aacb94..9175e52 100644 (file)
@@ -78,12 +78,13 @@ abstract class GenericApplicationTests<K, V>
        @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))
-                               .until(() -> receivedRecords.size() >= 100);
+                               .until(() -> receivedRecords.size() >= numberOfGeneratedMessages);
 
                await("Offsets committed")
                                .atMost(Duration.ofSeconds(10))
@@ -103,7 +104,8 @@ abstract class GenericApplicationTests<K, V>
        @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))
@@ -123,7 +125,7 @@ abstract class GenericApplicationTests<K, V>
                compareToCommitedOffsets(newOffsets);
                assertThat(receivedRecords.size())
                                .describedAs("Received not all sent events")
-                               .isLessThan(100);
+                               .isLessThan(numberOfGeneratedMessages);
 
                assertThatNoException()
                                .describedAs("Consumer should not be running")
@@ -137,7 +139,8 @@ abstract class GenericApplicationTests<K, V>
        @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))
@@ -157,7 +160,7 @@ abstract class GenericApplicationTests<K, V>
                compareToCommitedOffsets(oldOffsets);
                assertThat(receivedRecords.size())
                                .describedAs("Received not all sent events")
-                               .isLessThan(100);
+                               .isLessThan(numberOfGeneratedMessages);
 
                assertThatNoException()
                                .describedAs("Consumer should not be running")
@@ -238,12 +241,12 @@ abstract class GenericApplicationTests<K, V>
 
        public interface RecordGenerator
        {
-               void generate(
-                               Set<Integer> poisonPills,
-                               Set<Integer> logicErrors,
+               int generate(
+                               boolean poisonPills,
+                               boolean logicErrors,
                                Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
 
-               default boolean canGeneratePoisionPill()
+               default boolean canGeneratePoisonPill()
                {
                        return true;
                }