test: HandoverIT-POC - Listener/Writer remember received/sent messages
authorKai Moritz <kai@juplo.de>
Fri, 1 Mar 2024 19:18:00 +0000 (20:18 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 3 Mar 2024 10:41:01 +0000 (11:41 +0100)
src/test/java/de/juplo/kafka/chat/backend/TestListener.java
src/test/java/de/juplo/kafka/chat/backend/TestWriter.java

index 1c993c1..b74b8e8 100644 (file)
@@ -7,18 +7,13 @@ import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
 import de.juplo.kafka.chat.backend.api.MessageTo;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.core.ParameterizedTypeReference;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.codec.ServerSentEvent;
 import org.springframework.web.reactive.function.client.WebClient;
-import org.springframework.web.reactive.function.client.WebClientResponseException;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
-import reactor.util.retry.Retry;
 
-import java.nio.charset.Charset;
-import java.time.Duration;
-import java.util.concurrent.ThreadLocalRandom;
+import java.util.*;
 
 
 @Slf4j
@@ -32,23 +27,32 @@ public class TestListener implements Runnable
   {
     Flux
         .fromArray(chatRooms)
-        .flatMap(chatRoom -> receiveMessages(chatRoom)
-            .flatMap(sse ->
-            {
-              try
+        .flatMap(chatRoom ->
+        {
+          List<MessageTo> list = new LinkedList<>();
+          receivedMessages.put(chatRoom.getId(), list);
+          return receiveMessages(chatRoom)
+              .flatMap(sse ->
               {
-                return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
-              }
-              catch (Exception e)
+                try
+                {
+                  return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
+                }
+                catch (Exception e)
+                {
+                  return Mono.error(e);
+                }
+              })
+              .doOnNext(message ->
               {
-                return Mono.error(e);
-              }
-            })
-            .doOnNext(message -> log.info(
-                "Received a message from chat-room {}: {}",
-                chatRoom,
-                message))
-            .take(30))
+                list.add(message);
+                log.info(
+                    "Received a message from chat-room {}: {}",
+                    chatRoom,
+                    message);
+              })
+              .take(30);
+        })
         .takeUntil(message -> !running)
         .then()
         .block();
@@ -71,6 +75,8 @@ public class TestListener implements Runnable
   private final ChatRoomInfoTo[] chatRooms;
   private final ObjectMapper objectMapper;
 
+  final Map<UUID, List<MessageTo>> receivedMessages = new HashMap<>();
+
   volatile boolean running = true;
 
 
index 267519e..559cd36 100644 (file)
@@ -14,6 +14,8 @@ import reactor.util.retry.Retry;
 import java.nio.charset.Charset;
 import java.time.Duration;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.concurrent.ThreadLocalRandom;
 
 
@@ -44,11 +46,15 @@ public class TestWriter implements Runnable
         .flatMap(message -> sendMessage(chatRoom, message)
             .delayElement(Duration.ofMillis(ThreadLocalRandom.current().nextLong(500, 1500)))
             .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1))))
-        .doOnNext(message -> log.info(
-            "{} sent a message to {}: {}",
-            user,
-            chatRoom,
-            message))
+        .doOnNext(message ->
+        {
+          sentMessages.add(message);
+          log.info(
+              "{} sent a message to {}: {}",
+             user,
+             chatRoom,
+             message);
+        })
         .doOnError(throwable ->
         {
           WebClientResponseException e = (WebClientResponseException)throwable.getCause();
@@ -93,6 +99,8 @@ public class TestWriter implements Runnable
   private final ChatRoomInfoTo chatRoom;
   private final User user;
 
+  final List<MessageTo> sentMessages = new LinkedList<>();
+
   volatile boolean running = true;