From 80f616369c011db99eddf42c6ee91e66fd1dfd07 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 14 Aug 2022 11:32:10 +0200 Subject: [PATCH] =?utf8?q?Typisierung=20in=20`GenericApplicationTest`=20nu?= =?utf8?q?r=20noch,=20wo=20wirklich=20n=C3=B6tig?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Es wird nur noch dort mit Typisierung gearbeitet, wo dies unumgänglich ist, weil die typisierte Implementierung angesprochen wird. * Das Versenden der Test-Nachrichten erfolgt als `Bytes` für Schlüssel und Nachricht. * Dadurch muss der `RecordGenerator` nicht mehr typisiert werden. * Dafür muss die typisierte Implementierung des Testfalls dann Schlüssel und Nachricht mit einem passenden Serializer in eine `Bytes`-Payload umwandeln. --- .../java/de/juplo/kafka/ApplicationTest.java | 9 ++++----- .../juplo/kafka/GenericApplicationTest.java | 20 ++++++++----------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/test/java/de/juplo/kafka/ApplicationTest.java b/src/test/java/de/juplo/kafka/ApplicationTest.java index d3ff3b1..81165ab 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTest.java +++ b/src/test/java/de/juplo/kafka/ApplicationTest.java @@ -14,8 +14,7 @@ public class ApplicationTest extends GenericApplicationTest public ApplicationTest() { super( - new StringSerializer(), - new RecordGenerator<> () + new RecordGenerator() { final StringSerializer stringSerializer = new StringSerializer(); final LongSerializer longSerializer = new LongSerializer(); @@ -25,7 +24,7 @@ public class ApplicationTest extends GenericApplicationTest public void generate( int numberOfMessagesToGenerate, Set poisonPills, - Consumer> messageSender) + Consumer> messageSender) { int i = 0; @@ -41,11 +40,11 @@ public class ApplicationTest extends GenericApplicationTest ? new Bytes(stringSerializer.serialize(TOPIC, "BOOM!")) : new Bytes(longSerializer.serialize(TOPIC, (long)i)); - ProducerRecord record = + ProducerRecord record = new ProducerRecord<>( TOPIC, partition, - Integer.toString(partition*10+key%2), + new Bytes(stringSerializer.serialize(TOPIC,Integer.toString(partition*10+key%2))), value); messageSender.accept(record); diff --git a/src/test/java/de/juplo/kafka/GenericApplicationTest.java b/src/test/java/de/juplo/kafka/GenericApplicationTest.java index 6b2b635..68f150f 100644 --- a/src/test/java/de/juplo/kafka/GenericApplicationTest.java +++ b/src/test/java/de/juplo/kafka/GenericApplicationTest.java @@ -53,7 +53,7 @@ abstract class GenericApplicationTest @Autowired ExecutorService executor; - KafkaProducer testRecordProducer; + KafkaProducer testRecordProducer; KafkaConsumer offsetConsumer; Consumer> testHandler; EndlessConsumer endlessConsumer; @@ -62,15 +62,11 @@ abstract class GenericApplicationTest Set> receivedRecords; - final Serializer keySerializer; - final RecordGenerator recordGenerator; - final Consumer> messageSender; + final RecordGenerator recordGenerator; + final Consumer> messageSender; - public GenericApplicationTest( - Serializer keySerializer, - RecordGenerator recordGenerator) + public GenericApplicationTest(RecordGenerator recordGenerator) { - this.keySerializer = keySerializer; this.recordGenerator = recordGenerator; this.messageSender = (record) -> sendMessage(record); } @@ -204,15 +200,15 @@ abstract class GenericApplicationTest } - public interface RecordGenerator + public interface RecordGenerator { void generate( int numberOfMessagesToGenerate, Set poistionPills, - Consumer> messageSender); + Consumer> messageSender); } - void sendMessage(ProducerRecord record) + void sendMessage(ProducerRecord record) { testRecordProducer.send(record, (metadata, e) -> { @@ -244,7 +240,7 @@ abstract class GenericApplicationTest props = new Properties(); props.put("bootstrap.servers", properties.getBootstrapServer()); props.put("linger.ms", 100); - props.put("key.serializer", keySerializer.getClass().getName()); + props.put("key.serializer", BytesSerializer.class.getName()); props.put("value.serializer", BytesSerializer.class.getName()); testRecordProducer = new KafkaProducer<>(props); -- 2.20.1