WIP
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / TestWriter.java
1 package de.juplo.kafka.chat.backend;
2
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 org.springframework.web.reactive.function.client.WebClientResponseException;
10 import reactor.core.publisher.Flux;
11 import reactor.core.publisher.Mono;
12 import reactor.core.scheduler.Schedulers;
13 import reactor.util.retry.Retry;
14
15 import java.nio.charset.Charset;
16 import java.time.Duration;
17 import java.util.Iterator;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.concurrent.ThreadLocalRandom;
21
22
23 @Slf4j
24 public class TestWriter
25 {
26   public Mono<Void> run()
27   {
28     return Flux
29         .fromIterable((Iterable<Integer>) () -> new Iterator<>()
30         {
31           private int i = 0;
32
33           @Override
34           public boolean hasNext()
35           {
36             return running;
37           }
38
39           @Override
40           public Integer next()
41           {
42             return i++;
43           }
44         })
45         .map(i -> "Message #" + i)
46         .flatMap(message -> sendMessage(chatRoom, message)
47             .delayElement(Duration.ofMillis(ThreadLocalRandom.current().nextLong(500, 1500)))
48             .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1))))
49         .doOnNext(message ->
50         {
51           sentMessages.add(message);
52           log.info(
53               "{} sent a message to {}: {}",
54              user,
55              chatRoom,
56              message);
57         })
58         .doOnError(throwable ->
59         {
60           WebClientResponseException e = (WebClientResponseException)throwable.getCause();
61           log.error(
62               "{} failed sending a message: {}",
63               user,
64               e.getResponseBodyAsString(Charset.defaultCharset()));
65         })
66         .takeUntil(message -> !running)
67         .parallel()
68         .runOn(Schedulers.parallel())
69         .then();
70   }
71
72   private Mono<MessageTo> sendMessage(
73       ChatRoomInfoTo chatRoom,
74       String message)
75   {
76     return webClient
77         .put()
78         .uri(
79             "/{chatRoomId}/{username}/{serial}",
80             chatRoom.getId(),
81             user.getName(),
82             user.nextSerial())
83         .contentType(MediaType.TEXT_PLAIN)
84         .accept(MediaType.APPLICATION_JSON)
85         .bodyValue(message)
86         .exchangeToMono(response ->
87         {
88           if (response.statusCode().equals(HttpStatus.OK))
89           {
90             return response.bodyToMono(MessageTo.class);
91           }
92           else
93           {
94             return response.createError();
95           }
96         });
97   }
98
99
100   private final WebClient webClient;
101   private final ChatRoomInfoTo chatRoom;
102   private final User user;
103
104   final List<MessageTo> sentMessages = new LinkedList<>();
105
106   volatile boolean running = true;
107
108
109   TestWriter(Integer port, ChatRoomInfoTo chatRoom, String username)
110   {
111     webClient = WebClient.create("http://localhost:" + port);
112     this.chatRoom = chatRoom;
113     user = new User(username);
114   }
115 }