1 package de.juplo.kafka.chat.backend;
3 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
4 import de.juplo.kafka.chat.backend.api.MessageTo;
5 import lombok.extern.slf4j.Slf4j;
6 import org.springframework.http.HttpStatus;
7 import org.springframework.http.MediaType;
8 import org.springframework.web.reactive.function.client.WebClient;
9 import reactor.core.publisher.Mono;
10 import reactor.util.retry.Retry;
12 import java.time.Duration;
16 public class TestClient
20 for (int i = 0; i < 100; i++)
22 String message = "Message #" + i;
23 for (ChatRoomInfoTo chatRoom : chatRooms)
25 sendMessage(chatRoom, message)
26 .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
27 .map(MessageTo::toString)
28 .subscribe(result -> log.info(
29 "{} sent message \"{}\" to {}",
37 private Mono<MessageTo> sendMessage(
38 ChatRoomInfoTo chatRoom,
44 "/{chatRoomId}/{username}/{serial}",
48 .contentType(MediaType.TEXT_PLAIN)
49 .accept(MediaType.APPLICATION_JSON)
51 .exchangeToMono(response ->
53 if (response.statusCode().equals(HttpStatus.OK))
55 return response.bodyToMono(MessageTo.class);
59 return response.createError();
65 private final WebClient webClient;
66 private final ChatRoomInfoTo[] chatRooms;
67 private final User user;
70 TestClient(Integer port, ChatRoomInfoTo[] chatRooms, String username)
72 webClient = WebClient.create("http://localhost:" + port);
73 this.chatRooms = chatRooms;
74 user = new User(username);