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.core.ParameterizedTypeReference;
8 import org.springframework.http.HttpStatus;
9 import org.springframework.http.MediaType;
10 import org.springframework.http.codec.ServerSentEvent;
11 import org.springframework.web.reactive.function.client.WebClient;
12 import org.testcontainers.junit.jupiter.Testcontainers;
13 import reactor.core.publisher.Flux;
14 import reactor.core.publisher.Mono;
16 import java.util.Arrays;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
23 public abstract class AbstractHandoverIT
25 static final int NUM_CHATROOMS = 23;
26 static final int NUM_CLIENTS = 17;
29 private final AbstractHandoverITContainers containers;
30 private final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS + 1);
33 AbstractHandoverIT(AbstractHandoverITContainers containers)
35 this.containers = containers;
40 void test() throws InterruptedException
42 ChatRoomInfoTo[] chatRooms = Flux
43 .range(0, NUM_CHATROOMS)
44 .flatMap(i -> createChatRoom("room-" + i))
46 .toArray(size -> new ChatRoomInfoTo[size]);
48 int port = containers.haproxy.getMappedPort(8400);
50 TestWriter[] testWriters = Flux
51 .range(0, NUM_CLIENTS)
52 .map(i -> new TestWriter(
54 chatRooms[i % NUM_CHATROOMS],
55 "user-" + Integer.toString(i)))
56 .doOnNext(testClient -> executorService.execute(testClient))
58 .toArray(size -> new TestWriter[size]);
60 TestListener testListener = new TestListener(port, chatRooms);
61 executorService.execute(testListener);
67 .forEach(testClient -> testClient.running = false);
68 testListener.running = false;
71 Mono<ChatRoomInfoTo> createChatRoom(String name)
76 .contentType(MediaType.TEXT_PLAIN)
78 .accept(MediaType.APPLICATION_JSON)
79 .exchangeToMono(response ->
81 if (response.statusCode().equals(HttpStatus.OK))
83 return response.bodyToMono(ChatRoomInfoTo.class);
87 return response.createError();
96 void setUp() throws Exception
100 Integer port = containers.haproxy.getMappedPort(8400);
101 webClient = WebClient.create("http://localhost:" + port);