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