Anzahl der Fehler für die Test-Logik verfügbar gemacht
authorKai Moritz <kai@juplo.de>
Sun, 11 Sep 2022 11:42:57 +0000 (13:42 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 11 Sep 2022 14:43:07 +0000 (16:43 +0200)
src/test/java/de/juplo/kafka/ApplicationTests.java
src/test/java/de/juplo/kafka/GenericApplicationTests.java

index bd9f449..e01fdd1 100644 (file)
@@ -41,17 +41,22 @@ public class ApplicationTests extends GenericApplicationTests<String, Message>
     final StringSerializer stringSerializer = new StringSerializer();
     final Bytes calculateMessage = new Bytes(stringSerializer.serialize(TOPIC, "{}"));
 
-    int counter = 0;
+    int counterMessages;
+    int counterPoisonPills;
+    int counterLogicErrors;
 
     Map<String, List<AdderResult>> state;
 
     @Override
-    public int generate(
+    public void generate(
         boolean poisonPills,
         boolean logicErrors,
         Consumer<ProducerRecord<Bytes, Bytes>> messageSender)
     {
-      counter = 0;
+      counterMessages = 0;
+      counterPoisonPills = 0;
+      counterLogicErrors = 0;
+
       state =
           Arrays
               .stream(dieWilden13)
@@ -76,8 +81,8 @@ public class ApplicationTests extends GenericApplicationTests<String, Message>
               key,
               calculateMessage,
               Message.Type.CALC,
-              poisonPill(poisonPills, pass, counter),
-              logicError(logicErrors, pass, counter),
+              poisonPill(poisonPills, pass, counterMessages),
+              logicError(logicErrors, pass, counterMessages),
               messageSender);
             state.get(seeräuber).add(new AdderResult(number[i], (number[i] + 1) * number[i] / 2));
             // Pick next number to calculate
@@ -90,13 +95,29 @@ public class ApplicationTests extends GenericApplicationTests<String, Message>
             key,
             new Bytes(stringSerializer.serialize(TOPIC, "{\"next\":" + message[i]++ + "}")),
             Message.Type.ADD,
-            poisonPill(poisonPills, pass, counter),
-            logicError(logicErrors, pass, counter),
+            poisonPill(poisonPills, pass, counterMessages),
+            logicError(logicErrors, pass, counterMessages),
             messageSender);
         }
       }
+    }
+
+    @Override
+    public int getNumberOfMessages()
+    {
+      return counterMessages;
+    }
 
-      return counter;
+    @Override
+    public int getNumberOfPoisonPills()
+    {
+      return counterPoisonPills;
+    }
+
+    @Override
+    public int getNumberOfLogicErrors()
+    {
+      return counterLogicErrors;
     }
 
     boolean poisonPill (boolean poisonPills, int pass, int counter)
@@ -117,15 +138,17 @@ public class ApplicationTests extends GenericApplicationTests<String, Message>
         boolean logicError,
         Consumer<ProducerRecord<Bytes, Bytes>> messageSender)
     {
-      counter++;
+      counterMessages++;
 
       if (logicError)
       {
         value = new Bytes(stringSerializer.serialize(TOPIC, "{\"next\":-1}"));
+        counterLogicErrors++;
       }
       if (poisonPill)
       {
         value = new Bytes("BOOM!".getBytes());
+        counterPoisonPills++;
       }
 
       ProducerRecord<Bytes, Bytes> record = new ProducerRecord<>(TOPIC, key, value);
index 66a80ad..4793d96 100644 (file)
@@ -92,8 +92,9 @@ abstract class GenericApplicationTests<K, V>
        @Test
        void commitsCurrentOffsetsOnSuccess() throws Exception
        {
-               int numberOfGeneratedMessages =
-                               recordGenerator.generate(false, false, messageSender);
+               recordGenerator.generate(false, false, messageSender);
+
+               int numberOfGeneratedMessages = recordGenerator.getNumberOfMessages();
 
                await(numberOfGeneratedMessages + " records received")
                                .atMost(Duration.ofSeconds(30))
@@ -121,8 +122,9 @@ abstract class GenericApplicationTests<K, V>
        @SkipWhenErrorCannotBeGenerated(poisonPill = true)
        void commitsOffsetOfErrorForReprocessingOnDeserializationError()
        {
-               int numberOfGeneratedMessages =
-                               recordGenerator.generate(true, false, messageSender);
+               recordGenerator.generate(true, false, messageSender);
+
+               int numberOfGeneratedMessages = recordGenerator.getNumberOfMessages();
 
                await("Consumer failed")
                                .atMost(Duration.ofSeconds(30))
@@ -155,8 +157,9 @@ abstract class GenericApplicationTests<K, V>
        @SkipWhenErrorCannotBeGenerated(logicError = true)
        void commitsOffsetsOfUnseenRecordsOnLogicError()
        {
-               int numberOfGeneratedMessages =
-                               recordGenerator.generate(false, true, messageSender);
+               recordGenerator.generate(false, true, messageSender);
+
+               int numberOfGeneratedMessages = recordGenerator.getNumberOfMessages();
 
                await("Consumer failed")
                                .atMost(Duration.ofSeconds(30))
@@ -271,11 +274,15 @@ abstract class GenericApplicationTests<K, V>
 
        public interface RecordGenerator
        {
-               int generate(
+               void generate(
                                boolean poisonPills,
                                boolean logicErrors,
                                Consumer<ProducerRecord<Bytes, Bytes>> messageSender);
 
+               int getNumberOfMessages();
+               int getNumberOfPoisonPills();
+               int getNumberOfLogicErrors();
+
                default boolean canGeneratePoisonPill()
                {
                        return true;