From d09ba42bd23bce282091939966182a946faa8b91 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 1 Mar 2024 20:18:00 +0100 Subject: [PATCH] test: HandoverIT-POC - Listener/Writer remember received/sent messages --- .../kafka/chat/backend/TestListener.java | 48 +++++++++++-------- .../juplo/kafka/chat/backend/TestWriter.java | 18 +++++-- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/test/java/de/juplo/kafka/chat/backend/TestListener.java b/src/test/java/de/juplo/kafka/chat/backend/TestListener.java index c1daa49b..4a8ba0b0 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/TestListener.java +++ b/src/test/java/de/juplo/kafka/chat/backend/TestListener.java @@ -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 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)) - .limitRate(10)) + list.add(message); + log.info( + "Received a message from chat-room {}: {}", + chatRoom, + message); + }) + .limitRate(10); + }) .takeUntil(message -> !running) .then() .block(); @@ -71,6 +75,8 @@ public class TestListener implements Runnable private final ChatRoomInfoTo[] chatRooms; private final ObjectMapper objectMapper; + final Map> receivedMessages = new HashMap<>(); + volatile boolean running = true; diff --git a/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java b/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java index 267519e1..559cd360 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java +++ b/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java @@ -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 sentMessages = new LinkedList<>(); + volatile boolean running = true; -- 2.20.1