WIP:test: HandoverIT-POC - Clients are stopped after some time
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / TestClient.java
index 964a06b..0d54600 100644 (file)
@@ -6,9 +6,11 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
 import reactor.core.publisher.Mono;
 import reactor.util.retry.Retry;
 
+import java.nio.charset.Charset;
 import java.time.Duration;
 import java.util.concurrent.ThreadLocalRandom;
 
@@ -19,7 +21,7 @@ public class TestClient implements Runnable
   @Override
   public void run()
   {
-    for (int i = 0; i < 100; i++)
+    for (int i = 0; running; i++)
     {
       String message = "Message #" + i;
       for (ChatRoomInfoTo chatRoom : chatRooms)
@@ -27,11 +29,16 @@ public class TestClient implements Runnable
         sendMessage(chatRoom, message)
             .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
             .map(MessageTo::toString)
+            .onErrorResume(throwable ->
+            {
+              WebClientResponseException e = (WebClientResponseException)throwable.getCause();
+              return Mono.just(e.getResponseBodyAsString(Charset.defaultCharset()));
+            })
             .subscribe(result -> log.info(
-                "{} sent message \"{}\" to {}",
+                "{} sent a message to {}: {}",
                 user,
-                message,
-                chatRoom));
+                chatRoom,
+                result));
       }
       try
       {
@@ -76,6 +83,8 @@ public class TestClient implements Runnable
   private final ChatRoomInfoTo[] chatRooms;
   private final User user;
 
+  volatile boolean running = true;
+
 
   TestClient(Integer port, ChatRoomInfoTo[] chatRooms, String username)
   {