test: HandoverIT-POC - Refactored listening into class `TestListener`
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / AbstractHandoverIT.java
1 package de.juplo.kafka.chat.backend;
2
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;
15
16 import java.util.Arrays;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19
20
21 @Testcontainers
22 @Slf4j
23 public abstract class AbstractHandoverIT
24 {
25   static final ParameterizedTypeReference<ServerSentEvent<String>> SSE_TYPE = new ParameterizedTypeReference<>() {};
26   static final int NUM_CHATROOMS = 23;
27   static final int NUM_CLIENTS = 17;
28
29
30   private final AbstractHandoverITContainers containers;
31   private final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS + 1);
32
33
34   AbstractHandoverIT(AbstractHandoverITContainers containers)
35   {
36     this.containers = containers;
37   }
38
39
40   @Test
41   void test() throws InterruptedException
42   {
43     ChatRoomInfoTo[] chatRooms = Flux
44         .range(0, NUM_CHATROOMS)
45         .flatMap(i -> createChatRoom("room-" + i))
46         .toStream()
47         .toArray(size -> new ChatRoomInfoTo[size]);
48
49     int port = containers.haproxy.getMappedPort(8400);
50
51     TestWriter[] testWriters = Flux
52         .range(0, NUM_CLIENTS)
53         .map(i -> new TestWriter(
54             port,
55             chatRooms[i % NUM_CHATROOMS],
56             "user-" + Integer.toString(i)))
57         .doOnNext(testClient -> executorService.execute(testClient))
58         .toStream()
59         .toArray(size -> new TestWriter[size]);
60
61     TestListener testListener = new TestListener(port, chatRooms);
62     executorService.execute(testListener);
63
64     Thread.sleep(10000);
65
66     Arrays
67         .stream(testWriters)
68         .forEach(testClient -> testClient.running = false);
69     testListener.running = false;
70   }
71
72   Mono<ChatRoomInfoTo> createChatRoom(String name)
73   {
74     return webClient
75         .post()
76         .uri("/create")
77         .contentType(MediaType.TEXT_PLAIN)
78         .bodyValue(name)
79         .accept(MediaType.APPLICATION_JSON)
80         .exchangeToMono(response ->
81         {
82           if (response.statusCode().equals(HttpStatus.OK))
83           {
84             return response.bodyToMono(ChatRoomInfoTo.class);
85           }
86           else
87           {
88             return response.createError();
89           }
90         });
91   }
92
93
94   WebClient webClient;
95
96   @BeforeEach
97   void setUp() throws Exception
98   {
99     containers.setUp();
100
101     Integer port = containers.haproxy.getMappedPort(8400);
102     webClient = WebClient.create("http://localhost:" + port);
103   }
104 }