test: HandoverIT-POC - GREEN: The `TestListener` automatically reconnects backend
authorKai Moritz <kai@juplo.de>
Mon, 11 Mar 2024 17:32:46 +0000 (18:32 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 22 Mar 2024 16:40:17 +0000 (17:40 +0100)
* If a rebalance happens, an instances, that is no longer responsible
  for a specific chat-room, closes the connections to listeners of these
  chat-rooms.
* Hence, the `TestListener`, like the real frontend, will have to
  reconnect, if that happens, to be able to see newly send messages.

src/test/java/de/juplo/kafka/chat/backend/TestListener.java

index 5bd3646..092cd43 100644 (file)
@@ -12,7 +12,9 @@ import org.springframework.http.codec.ServerSentEvent;
 import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
+import reactor.util.retry.Retry;
 
+import java.time.Duration;
 import java.util.*;
 
 
@@ -52,7 +54,8 @@ public class TestListener
         })
         .doOnNext(message -> list.add(message))
         .doOnComplete(() -> log.info("Listening to {} was completed!", chatRoom))
-        .doOnError(throwalbe -> log.error("Listening to {} failed!", chatRoom, throwalbe));
+        .doOnError(throwalbe -> log.error("Listening to {} failed!", chatRoom, throwalbe))
+        .thenMany(Flux.defer(() -> receiveMessages(chatRoom)));
   }
 
   Flux<ServerSentEvent<String>> receiveServerSentEvents(ChatRoomInfoTo chatRoom)
@@ -65,7 +68,8 @@ public class TestListener
         .accept(MediaType.TEXT_EVENT_STREAM)
         .header("X-Shard", chatRoom.getShard().toString())
         .retrieve()
-        .bodyToFlux(SSE_TYPE);
+        .bodyToFlux(SSE_TYPE)
+        .retryWhen(Retry.fixedDelay(15, Duration.ofSeconds(1)));
   }