From 7131724ef1486c13fc32bf44fe0df31287600581 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 1 Mar 2024 20:40:18 +0100 Subject: [PATCH] WIP --- .../chat/backend/AbstractHandoverIT.java | 43 +++++++++++-------- .../kafka/chat/backend/TestListener.java | 13 +++--- .../juplo/kafka/chat/backend/TestWriter.java | 13 +++--- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java index 6445755e..0047ff01 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java @@ -4,18 +4,14 @@ import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.reactive.function.client.WebClient; import org.testcontainers.junit.jupiter.Testcontainers; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.Arrays; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.CompletableFuture; @Testcontainers @@ -27,7 +23,6 @@ public abstract class AbstractHandoverIT private final AbstractHandoverITContainers containers; - private final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS + 1); AbstractHandoverIT(AbstractHandoverITContainers containers) @@ -47,25 +42,35 @@ public abstract class AbstractHandoverIT int port = containers.haproxy.getMappedPort(8400); - TestWriter[] testWriters = Flux - .range(0, NUM_CLIENTS) - .map(i -> new TestWriter( - port, - chatRooms[i % NUM_CHATROOMS], - "user-" + Integer.toString(i))) - .doOnNext(testClient -> executorService.execute(testClient)) - .toStream() - .toArray(size -> new TestWriter[size]); + CompletableFuture[] testWriterFutures = new CompletableFuture[NUM_CLIENTS]; + TestWriter[] testWriters = new TestWriter[NUM_CLIENTS]; + for (int i = 0; i < NUM_CLIENTS; i++) + { + TestWriter testWriter = new TestWriter( + port, + chatRooms[i % NUM_CHATROOMS], + "user-" + i); + testWriters[i] = testWriter; + testWriterFutures[i] = testWriter + .run() + .toFuture(); + } TestListener testListener = new TestListener(port, chatRooms); - executorService.execute(testListener); + CompletableFuture testListenerFuture = testListener + .run() + .toFuture(); Thread.sleep(10000); - Arrays - .stream(testWriters) - .forEach(testClient -> testClient.running = false); + for (int i = 0; i < NUM_CLIENTS; i++) + { + testWriters[i].running = false; + testWriterFutures[i].join(); + } + testListener.running = false; + testListenerFuture.join(); } Mono createChatRoom(String name) diff --git a/src/test/java/de/juplo/kafka/chat/backend/TestListener.java b/src/test/java/de/juplo/kafka/chat/backend/TestListener.java index b74b8e8c..e94dfcbc 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/TestListener.java +++ b/src/test/java/de/juplo/kafka/chat/backend/TestListener.java @@ -12,20 +12,20 @@ import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; import java.util.*; @Slf4j -public class TestListener implements Runnable +public class TestListener { static final ParameterizedTypeReference> SSE_TYPE = new ParameterizedTypeReference<>() {}; - @Override - public void run() + public Mono run() { - Flux + return Flux .fromArray(chatRooms) .flatMap(chatRoom -> { @@ -54,8 +54,9 @@ public class TestListener implements Runnable .take(30); }) .takeUntil(message -> !running) - .then() - .block(); + .parallel() + .runOn(Schedulers.parallel()) + .then(); } Flux> receiveMessages(ChatRoomInfoTo chatRoom) 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 559cd360..5c6f7abd 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java +++ b/src/test/java/de/juplo/kafka/chat/backend/TestWriter.java @@ -9,6 +9,7 @@ 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; @@ -20,12 +21,11 @@ import java.util.concurrent.ThreadLocalRandom; @Slf4j -public class TestWriter implements Runnable +public class TestWriter { - @Override - public void run() + public Mono run() { - Flux + return Flux .fromIterable((Iterable) () -> new Iterator<>() { private int i = 0; @@ -63,8 +63,9 @@ public class TestWriter implements Runnable user, e.getResponseBodyAsString(Charset.defaultCharset())); }) - .then() - .block(); + .parallel() + .runOn(Schedulers.parallel()) + .then(); } private Mono sendMessage( -- 2.20.1