WIP rebase--2023-09-13--21-01
authorKai Moritz <kai@juplo.de>
Wed, 13 Sep 2023 19:01:34 +0000 (21:01 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 13 Sep 2023 19:01:34 +0000 (21:01 +0200)
src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java

index a07ffd1..d97327d 100644 (file)
@@ -25,7 +25,8 @@ import java.util.List;
 import java.util.concurrent.CompletableFuture;
 
 import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS;
-import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.TOPIC;
+import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.DATA_TOPIC;
+import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.INFO_TOPIC;
 
 
 @SpringBootTest(
@@ -40,16 +41,20 @@ import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServ
     "chat.backend.kafka.client-id-PREFIX=TEST",
     "chat.backend.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
     "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
-    "chat.backend.kafka.chatroom-channel-topic=" + TOPIC,
+    "chat.backend.kafka.chatroom-channel-topic=" + DATA_TOPIC,
     "chat.backend.kafka.num-partitions=" + NUM_SHARDS,
 })
-@EmbeddedKafka(topics = { TOPIC }, partitions = 10)
+@EmbeddedKafka(
+    topics = { INFO_TOPIC, DATA_TOPIC },
+    partitions = 10)
 @Slf4j
 public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
 {
-  final static String TOPIC = "KAFKA_CHAT_HOME_TEST";
+  final static String INFO_TOPIC = "KAFKA_CHAT_HOME_TEST_INFO";
+  final static String DATA_TOPIC = "KAFKA_CHAT_HOME_TEST_DATA";
 
-  static CompletableFuture<Void> CONSUMER_JOB;
+  static CompletableFuture<Void> INFO_CHANNEL_CONSUMER_JOB;
+  static CompletableFuture<Void> DATA_CHANNEL_CONSUMER_JOB;
 
 
   @TestConfiguration
@@ -67,20 +72,21 @@ public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
   @BeforeAll
   public static void sendAndLoadStoredData(
       @Autowired KafkaTemplate<String, String> messageTemplate,
-      @Autowired Consumer chatRoomChannelConsumer,
+      @Autowired Consumer infoChannelConsumer,
+      @Autowired Consumer dataChannelConsumer,
       @Autowired ThreadPoolTaskExecutor taskExecutor,
       @Autowired DataChannel dataChannel)
   {
-    send(messageTemplate, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\": \"5c73531c-6fc4-426c-adcb-afc5c140a0f7\", \"shard\": 2, \"name\": \"FOO\" }", "command_create_chatroom");
+    send(messageTemplate, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\": \"5c73531c-6fc4-426c-adcb-afc5c140a0f7\", \"name\": \"FOO\" }", "event_chatroom_created");
     send(messageTemplate,"5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"peter\", \"text\" : \"Hallo, ich heiße Peter!\" }", "event_chatmessage_received");
     send(messageTemplate,"5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"ute\", \"text\" : \"Ich bin Ute...\" }", "event_chatmessage_received");
     send(messageTemplate,"5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 2, \"user\" : \"peter\", \"text\" : \"Willst du mit mir gehen?\" }", "event_chatmessage_received");
     send(messageTemplate,"5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"klaus\", \"text\" : \"Ja? Nein? Vielleicht??\" }", "event_chatmessage_received");
 
-    List<TopicPartition> assignedPartitions = List.of(new TopicPartition(TOPIC, 2));
-    chatRoomChannelConsumer.assign(assignedPartitions);
+    List<TopicPartition> assignedPartitions = List.of(new TopicPartition(DATA_TOPIC, 2));
+    dataChannelConsumer.assign(assignedPartitions);
     dataChannel.onPartitionsAssigned(assignedPartitions);
-    CONSUMER_JOB = taskExecutor
+    DATA_CHANNEL_CONSUMER_JOB = taskExecutor
         .submitCompletable(dataChannel)
         .exceptionally(e ->
         {
@@ -91,7 +97,7 @@ public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
 
   static void send(KafkaTemplate<String, String> kafkaTemplate, String key, String value, String typeId)
   {
-    ProducerRecord<String, String> record = new ProducerRecord<>(TOPIC, key, value);
+    ProducerRecord<String, String> record = new ProducerRecord<>(DATA_TOPIC, key, value);
     record.headers().add("__TypeId__", typeId.getBytes());
     SendResult<String, String> result = kafkaTemplate.send(record).join();
     log.info(
@@ -107,7 +113,7 @@ public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest
     log.info("Signaling the consumer of the CahtRoomChannel to quit its work");
     chatRoomChannelConsumer.wakeup();
     log.info("Waiting for the consumer of the ChatRoomChannel to finish its work");
-    CONSUMER_JOB.join();
+    DATA_CHANNEL_CONSUMER_JOB.join();
     log.info("Joined the consumer of the ChatRoomChannel");
   }
 }