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;
13 import java.util.concurrent.ThreadLocalRandom;
17 public class TestClient
21 for (int i = 0; i < 100; i++)
23 String message = "Message #" + i;
24 for (ChatRoomInfoTo chatRoom : chatRooms)
26 sendMessage(chatRoom, message)
27 .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
28 .map(MessageTo::toString)
29 .subscribe(result -> log.info(
30 "{} sent message \"{}\" to {}",
37 Thread.sleep(ThreadLocalRandom.current().nextLong(700, 1000));
41 throw new RuntimeException(e);
46 private Mono<MessageTo> sendMessage(
47 ChatRoomInfoTo chatRoom,
53 "/{chatRoomId}/{username}/{serial}",
57 .contentType(MediaType.TEXT_PLAIN)
58 .accept(MediaType.APPLICATION_JSON)
60 .exchangeToMono(response ->
62 if (response.statusCode().equals(HttpStatus.OK))
64 return response.bodyToMono(MessageTo.class);
68 return response.createError();
74 private final WebClient webClient;
75 private final ChatRoomInfoTo[] chatRooms;
76 private final User user;
79 TestClient(Integer port, ChatRoomInfoTo[] chatRooms, String username)
81 webClient = WebClient.create("http://localhost:" + port);
82 this.chatRooms = chatRooms;
83 user = new User(username);