1 package de.juplo.kafka.chat.backend;
3 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
4 import lombok.extern.slf4j.Slf4j;
5 import org.junit.jupiter.api.BeforeEach;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.http.MediaType;
9 import org.springframework.web.reactive.function.client.WebClient;
10 import org.testcontainers.junit.jupiter.Testcontainers;
11 import reactor.core.publisher.Flux;
12 import reactor.core.publisher.Mono;
14 import java.util.concurrent.CompletableFuture;
19 public abstract class AbstractHandoverIT
21 static final int NUM_CHATROOMS = 23;
22 static final int NUM_CLIENTS = 17;
25 private final AbstractHandoverITContainers containers;
28 AbstractHandoverIT(AbstractHandoverITContainers containers)
30 this.containers = containers;
35 void test() throws InterruptedException
37 ChatRoomInfoTo[] chatRooms = Flux
38 .range(0, NUM_CHATROOMS)
39 .flatMap(i -> createChatRoom("room-" + i))
41 .toArray(size -> new ChatRoomInfoTo[size]);
43 int port = containers.haproxy.getMappedPort(8400);
45 CompletableFuture<Void>[] testWriterFutures = new CompletableFuture[NUM_CLIENTS];
46 TestWriter[] testWriters = new TestWriter[NUM_CLIENTS];
47 for (int i = 0; i < NUM_CLIENTS; i++)
49 TestWriter testWriter = new TestWriter(
51 chatRooms[i % NUM_CHATROOMS],
53 testWriters[i] = testWriter;
54 testWriterFutures[i] = testWriter
59 TestListener testListener = new TestListener(port, chatRooms);
62 .subscribe(message -> log.info(
63 "Received message: {}",
66 log.info("Sleeping for 3 seconds...");
69 for (int i = 0; i < NUM_CLIENTS; i++)
71 testWriters[i].running = false;
72 testWriterFutures[i].join();
73 log.info("Joined TestWriter {}", testWriters[i].user);
77 Mono<ChatRoomInfoTo> createChatRoom(String name)
82 .contentType(MediaType.TEXT_PLAIN)
84 .accept(MediaType.APPLICATION_JSON)
85 .exchangeToMono(response ->
87 if (response.statusCode().equals(HttpStatus.OK))
89 return response.bodyToMono(ChatRoomInfoTo.class);
93 return response.createError();
102 void setUp() throws Exception
106 Integer port = containers.haproxy.getMappedPort(8400);
107 webClient = WebClient.create("http://localhost:" + port);