test: RED - Added test for multiple parallel Listeners to `ChatRoomDataTest`
authorKai Moritz <kai@juplo.de>
Fri, 8 Mar 2024 17:10:43 +0000 (18:10 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 22 Mar 2024 16:39:20 +0000 (17:39 +0100)
* The test formulates the expectation, that late listeners should see all
  messages (for a reasonable long period before their subscriptions)
* The test _fails_, because the implementation only buffers messages for
  backpressure -- _not for replay!_

src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java

index 96122d9..4a7f2d6 100644 (file)
@@ -272,6 +272,59 @@ public class ChatRoomDataTest
         .untilAsserted(() -> assertThat(receivedMessages).contains(sentMessages));
   }
 
+  @Test
+  @DisplayName("Assert, that multiple listeners can receive an added message")
+  void testMultipleListeners()
+  {
+    // Given
+    Message message1 = new Message(key, 1l, timestamp, "#1");
+    Message message2 = new Message(key, 2l, timestamp, "#2");
+    Message message3 = new Message(key, 3l, timestamp, "#3");
+    Message message4 = new Message(key, 4l, timestamp, "#4");
+    when(chatMessageService.getMessage(any(Message.MessageKey.class)))
+        .thenReturn(Mono.empty());
+    when(chatMessageService.persistMessage(any(Message.MessageKey.class), any(LocalDateTime.class), any(String.class)))
+        .thenReturn(Mono.just(message1))
+        .thenReturn(Mono.just(message2))
+        .thenReturn(Mono.just(message3))
+        .thenReturn(Mono.just(message4));
+
+    // When
+    Message[] sentMessages = new Message[4];
+    List<Message> messagesReceivedByListener1 = new LinkedList<>();
+    chatRoomData
+        .listen()
+        .subscribe(receivedMessage -> messagesReceivedByListener1.add(receivedMessage));
+    sentMessages[0] = chatRoomData.addMessage(messageId, user, "Some Text").block();
+    sentMessages[1] = chatRoomData.addMessage(messageId, user, "Some Text").block();
+    List<Message> messagesReceivedByListener2 = new LinkedList<>();
+    chatRoomData
+        .listen()
+        .subscribe(receivedMessage -> messagesReceivedByListener2.add(receivedMessage));
+    sentMessages[2] = chatRoomData.addMessage(messageId, user, "Some Text").block();
+    List<Message> messagesReceivedByListener3 = new LinkedList<>();
+    chatRoomData
+        .listen()
+        .subscribe(receivedMessage -> messagesReceivedByListener3.add(receivedMessage));
+    sentMessages[3] = chatRoomData.addMessage(messageId, user, "Some Text").block();
+    List<Message> messagesReceivedByListener4 = new LinkedList<>();
+    chatRoomData
+        .listen()
+        .subscribe(receivedMessage -> messagesReceivedByListener4.add(receivedMessage));
+
+    // Then
+    Awaitility
+        .await()
+        .atMost(Duration.ofSeconds(1))
+        .untilAsserted(() ->
+        {
+          assertThat(messagesReceivedByListener1).contains(sentMessages);
+          assertThat(messagesReceivedByListener2).contains(sentMessages);
+          assertThat(messagesReceivedByListener3).contains(sentMessages);
+          assertThat(messagesReceivedByListener4).contains(sentMessages);
+        });
+  }
+
 
   /**
    * This message is used, when methods of {@link ChatMessageService} are mocked,