X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2FTestWriter.java;h=267519e1490f3652fcd7afe8ed85ba88ba991239;hb=de74f365261455ddb83034faa8f6a4290c556e41;hp=0d54600d17094e49bc966e143992bdd7017c5683;hpb=d01255379f60b7be221690a1ad89349bf477d50a;p=demos%2Fkafka%2Fchat diff --git a/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java b/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java index 0d54600d..267519e1 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java +++ b/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java @@ -7,48 +7,58 @@ 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.util.retry.Retry; import java.nio.charset.Charset; import java.time.Duration; +import java.util.Iterator; import java.util.concurrent.ThreadLocalRandom; @Slf4j -public class TestClient implements Runnable +public class TestWriter implements Runnable { @Override public 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); - } - } + Flux + .fromIterable((Iterable) () -> new Iterator<>() + { + private int i = 0; + + @Override + public boolean hasNext() + { + return running; + } + + @Override + public Integer next() + { + return i++; + } + }) + .map(i -> "Message #" + i) + .flatMap(message -> sendMessage(chatRoom, message) + .delayElement(Duration.ofMillis(ThreadLocalRandom.current().nextLong(500, 1500))) + .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))) + .doOnNext(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())); + }) + .then() + .block(); } private Mono sendMessage( @@ -80,16 +90,16 @@ public class TestClient implements Runnable private final WebClient webClient; - private final ChatRoomInfoTo[] chatRooms; + private final ChatRoomInfoTo chatRoom; private final User user; volatile boolean running = true; - TestClient(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); } }