From 6e9b68d07e0f48a6f7b4f97697ce461e51eb48a6 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 8 Mar 2024 18:10:43 +0100 Subject: [PATCH] test: RED - Added test for multiple parallel Listeners to `ChatRoomDataTest` * 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!_ --- .../chat/backend/domain/ChatRoomDataTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java index 96122d94..4a7f2d64 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/domain/ChatRoomDataTest.java @@ -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 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 messagesReceivedByListener2 = new LinkedList<>(); + chatRoomData + .listen() + .subscribe(receivedMessage -> messagesReceivedByListener2.add(receivedMessage)); + sentMessages[2] = chatRoomData.addMessage(messageId, user, "Some Text").block(); + List messagesReceivedByListener3 = new LinkedList<>(); + chatRoomData + .listen() + .subscribe(receivedMessage -> messagesReceivedByListener3.add(receivedMessage)); + sentMessages[3] = chatRoomData.addMessage(messageId, user, "Some Text").block(); + List 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, -- 2.20.1