DIRTYFIX:subscribe
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / AbstractHandoverIT.java
index 2e4faa1..b55a4c0 100644 (file)
@@ -4,21 +4,22 @@ import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-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.testcontainers.junit.jupiter.Testcontainers;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
+import java.util.concurrent.CompletableFuture;
+
 
 @Testcontainers
 @Slf4j
 public abstract class AbstractHandoverIT
 {
-  static final ParameterizedTypeReference<ServerSentEvent<String>> SSE_TYPE = new ParameterizedTypeReference<>() {};
+  static final int NUM_CHATROOMS = 23;
+  static final int NUM_CLIENTS = 17;
 
 
   private final AbstractHandoverITContainers containers;
@@ -33,18 +34,44 @@ public abstract class AbstractHandoverIT
   @Test
   void test() throws InterruptedException
   {
-    ChatRoomInfoTo chatRoom = createChatRoom("bar").block();
-    TestClient testClient = new TestClient(
-        containers.haproxy.getMappedPort(8400),
-        chatRoom,
-        "nerd");
-    testClient.run();
-
-    receiveMessages(chatRoom)
-        .take(100)
-        .doOnNext(message -> log.info("message: {}", message))
-        .then()
-        .block();
+    ChatRoomInfoTo[] chatRooms = Flux
+        .range(0, NUM_CHATROOMS)
+        .flatMap(i -> createChatRoom("room-" + i))
+        .toStream()
+        .toArray(size -> new ChatRoomInfoTo[size]);
+
+    int port = containers.haproxy.getMappedPort(8400);
+
+    CompletableFuture<Void>[] testWriterFutures = new CompletableFuture[NUM_CLIENTS];
+    TestWriter[] testWriters = new TestWriter[NUM_CLIENTS];
+    for (int i = 0; i < NUM_CLIENTS; i++)
+    {
+      TestWriter testWriter = new TestWriter(
+          port,
+          chatRooms[i % NUM_CHATROOMS],
+          "user-" + i);
+      testWriters[i] = testWriter;
+      testWriterFutures[i] = testWriter
+          .run()
+          .toFuture();
+    }
+
+    TestListener testListener = new TestListener(port, chatRooms);
+    testListener
+        .run()
+        .subscribe(message -> log.info(
+            "Received message: {}",
+            message));
+
+    log.info("Sleeping for 3 seconds...");
+    Thread.sleep(3000);
+
+    for (int i = 0; i < NUM_CLIENTS; i++)
+    {
+      testWriters[i].running = false;
+      testWriterFutures[i].join();
+      log.info("Joined TestWriter {}", testWriters[i].user);
+    }
   }
 
   Mono<ChatRoomInfoTo> createChatRoom(String name)
@@ -68,18 +95,6 @@ public abstract class AbstractHandoverIT
         });
   }
 
-  Flux<ServerSentEvent<String>> receiveMessages(ChatRoomInfoTo chatRoom)
-  {
-    return webClient
-        .get()
-        .uri(
-            "/{chatRoomId}/listen",
-            chatRoom.getId())
-        .accept(MediaType.TEXT_EVENT_STREAM)
-        .retrieve()
-        .bodyToFlux(SSE_TYPE);
-  }
-
 
   WebClient webClient;