WIP:refactor: Refined channel-states, introduced `ChannelState` -- ALIGN
[demos/kafka/chat] / src / test / java / de / juplo / kafka / chat / backend / TestWriter.java
index 076376e..c21acda 100644 (file)
@@ -7,48 +7,68 @@ import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.web.reactive.function.client.WebClient;
 import org.springframework.web.reactive.function.client.WebClientResponseException;
+import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
 import reactor.util.retry.Retry;
 
 import java.nio.charset.Charset;
 import java.time.Duration;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.concurrent.ThreadLocalRandom;
 
 
 @Slf4j
-public class TestWriter implements Runnable
+public class TestWriter
 {
-  @Override
-  public void run()
+  public Mono<Void> run()
   {
-    for (int i = 0; running; i++)
-    {
-      String message = "Message #" + i;
-      for (ChatRoomInfoTo chatRoom : chatRooms)
-      {
-        sendMessage(chatRoom, message)
-            .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
-            .map(MessageTo::toString)
-            .onErrorResume(throwable ->
-            {
-              WebClientResponseException e = (WebClientResponseException)throwable.getCause();
-              return Mono.just(e.getResponseBodyAsString(Charset.defaultCharset()));
-            })
-            .subscribe(result -> log.info(
-                "{} sent a message to {}: {}",
-                user,
-                chatRoom,
-                result));
-      }
-      try
-      {
-        Thread.sleep(ThreadLocalRandom.current().nextLong(700, 1000));
-      }
-      catch (Exception e)
-      {
-        throw new RuntimeException(e);
-      }
-    }
+    return Flux
+        .fromIterable((Iterable<Integer>) () -> new Iterator<>()
+        {
+          private int i = 0;
+
+          @Override
+          public boolean hasNext()
+          {
+            return running;
+          }
+
+          @Override
+          public Integer next()
+          {
+            return i++;
+          }
+        })
+        .delayElements(Duration.ofMillis(ThreadLocalRandom.current().nextLong(500, 1500)))
+        .map(i -> "Message #" + i)
+        .flatMap(message -> sendMessage(chatRoom, message)
+            .retryWhen(Retry.backoff(10, Duration.ofSeconds(1))))
+        .doOnNext(message ->
+        {
+          sentMessages.add(message);
+          log.info(
+              "{} sent a message to {}: {}",
+             user,
+             chatRoom,
+             message);
+        })
+        .doOnError(throwable ->
+        {
+          WebClientResponseException e = (WebClientResponseException)throwable.getCause();
+          log.error(
+              "{} failed sending a message: {}",
+              user,
+              e.getResponseBodyAsString(Charset.defaultCharset()));
+        })
+        .limitRate(1)
+        .takeUntil(message -> !running)
+        .doOnComplete(() -> log.info("TestWriter {} is done", user))
+        .parallel(1)
+        .runOn(Schedulers.parallel())
+        .then();
   }
 
   private Mono<MessageTo> sendMessage(
@@ -80,16 +100,18 @@ public class TestWriter implements Runnable
 
 
   private final WebClient webClient;
-  private final ChatRoomInfoTo[] chatRooms;
-  private final User user;
+
+  final ChatRoomInfoTo chatRoom;
+  final User user;
+  final List<MessageTo> sentMessages = new LinkedList<>();
 
   volatile boolean running = true;
 
 
-  TestWriter(Integer port, ChatRoomInfoTo[] chatRooms, String username)
+  TestWriter(Integer port, ChatRoomInfoTo chatRoom, String username)
   {
     webClient = WebClient.create("http://localhost:" + port);
-    this.chatRooms = chatRooms;
+    this.chatRoom = chatRoom;
     user = new User(username);
   }
 }