From 5075238a384c48d07bf8382c8904a105be83e7a9 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 3 Feb 2024 08:29:06 +0100 Subject: [PATCH] test: HandoverIT-POC - First working setup for the planned test * First working POC of the setup for the planned test. * This version blocks endlessly, because the SSE-flux never completes. --- pom.xml | 5 + .../chat/backend/AbstractHandoverIT.java | 147 ++++++++++++ .../kafka/chat/backend/KafkaHandoverIT.java | 209 ++++++++++++++++++ src/test/resources/haproxy.cfg | 39 ++++ src/test/resources/sharding.map | 10 + 5 files changed, 410 insertions(+) create mode 100644 src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java create mode 100644 src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java create mode 100644 src/test/resources/haproxy.cfg create mode 100644 src/test/resources/sharding.map diff --git a/pom.xml b/pom.xml index e98a75e4..13a0e7c6 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,11 @@ mongodb test + + org.testcontainers + kafka + test + org.testcontainers junit-jupiter diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java new file mode 100644 index 00000000..fc54c032 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java @@ -0,0 +1,147 @@ +package de.juplo.kafka.chat.backend; + +import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo; +import de.juplo.kafka.chat.backend.api.MessageTo; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import lombok.extern.slf4j.Slf4j; +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.images.ImagePullPolicy; +import org.testcontainers.junit.jupiter.Testcontainers; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.util.retry.Retry; + +import java.time.Duration; +import java.util.stream.IntStream; + + +@Testcontainers +@Slf4j +public abstract class AbstractHandoverIT +{ + static final ImagePullPolicy NEVER_PULL = imageName -> false; + static final ParameterizedTypeReference> SSE_TYPE = new ParameterizedTypeReference<>() {}; + + + @Test + void test() throws InterruptedException + { + ChatRoomInfoTo chatRoom = createChatRoom("bar").block(); + User user = new User("nerd"); + IntStream + .rangeClosed(1,100) + .mapToObj(i ->sendMessage(chatRoom, user, "Message #" + i)) + .map(result -> result + .map(MessageTo::toString) + .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1))) + .block()) + .forEach(result -> log.info("{}", result)); + + receiveMessages(chatRoom) + .take(100) + .doOnNext(message -> log.info("message: {}", message)) + .then() + .block(); + } + + Mono createChatRoom(String name) + { + return webClient + .post() + .uri("/create") + .contentType(MediaType.TEXT_PLAIN) + .bodyValue(name) + .accept(MediaType.APPLICATION_JSON) + .exchangeToMono(response -> + { + if (response.statusCode().equals(HttpStatus.OK)) + { + return response.bodyToMono(ChatRoomInfoTo.class); + } + else + { + return response.createError(); + } + }); + } + + Mono sendMessage( + ChatRoomInfoTo chatRoom, + User user, + String message) + { + return webClient + .put() + .uri( + "/{chatRoomId}/{username}/{serial}", + chatRoom.getId(), + user.getName(), + user.nextSerial()) + .contentType(MediaType.TEXT_PLAIN) + .accept(MediaType.APPLICATION_JSON) + .bodyValue(message) + .exchangeToMono(response -> + { + if (response.statusCode().equals(HttpStatus.OK)) + { + return response.bodyToMono(MessageTo.class); + } + else + { + return response.createError(); + } + }); + } + + Flux> receiveMessages(ChatRoomInfoTo chatRoom) + { + return webClient + .get() + .uri( + "/{chatRoomId}/listen", + chatRoom.getId()) + .accept(MediaType.TEXT_EVENT_STREAM) + .retrieve() + .bodyToFlux(SSE_TYPE); + } + + + WebClient webClient; + + + @EqualsAndHashCode + @ToString + class User + { + @Getter + private final String name; + private int serial = 0; + + + User (String name) + { + this.name = name; + } + + + int nextSerial() + { + return ++serial; + } + } + + @Getter + @Setter + static class StatusTo + { + String status; + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java new file mode 100644 index 00000000..9fbf5391 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java @@ -0,0 +1,209 @@ +package de.juplo.kafka.chat.backend; + +import lombok.extern.slf4j.Slf4j; +import org.awaitility.Awaitility; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.WebClient; +import org.testcontainers.containers.*; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; +import reactor.core.publisher.Mono; + +import java.io.IOException; +import java.time.Duration; + + +@Slf4j +class KafkaHandoverIT extends AbstractHandoverIT +{ + @BeforeEach + void setUpWebClient() + { + Integer port = HAPROXY.getMappedPort(8400); + webClient = WebClient.create("http://localhost:" + port); + + Awaitility + .await() + .atMost(Duration.ofMinutes(10)) + .until(() -> WebClient + .create("http://localhost:" + BACKEND_1.getMappedPort(8080)) + .get() + .uri("/actuator/health") + .exchangeToMono(response -> + { + if (response.statusCode().equals(HttpStatus.OK)) + { + return response + .bodyToMono(StatusTo.class) + .map(StatusTo::getStatus) + .map(status -> status.equalsIgnoreCase("UP")); + } + else + { + return Mono.just(false); + } + }) + .block()); + + HAPROXY + .getDockerClient() + .killContainerCmd(HAPROXY.getContainerId()) + .withSignal("HUP") + .exec(); + + + Awaitility + .await() + .atMost(Duration.ofMinutes(10)) + .until(() -> webClient + .get() + .uri("/actuator/health") + .exchangeToMono(response -> + { + if (response.statusCode().equals(HttpStatus.OK)) + { + return response + .bodyToMono(StatusTo.class) + .map(StatusTo::getStatus) + .map(status -> status.equalsIgnoreCase("UP")); + } + else + { + return Mono.just(false); + } + }) + .block()); + } + + + @BeforeAll + static void setUpDocker() throws IOException, InterruptedException + { + KAFKA.start(); + HAPROXY.start(); + + Container.ExecResult result; + result = KAFKA.execInContainer( + "kafka-topics", + "--bootstrap-server", + "kafka:9999", + "--create", + "--topic", + "info_channel", + "--partitions", + "3"); + log.info( + "EXIT-CODE={}, STDOUT={}, STDERR={}", + result.getExitCode(), + result.getStdout(), + result.getStdout()); + result = KAFKA.execInContainer( + "kafka-topics", + "--bootstrap-server", + "kafka:9999", + "--create", + "--topic", + "data_channel", + "--partitions", + "10"); + log.info( + "EXIT-CODE={}, STDOUT={}, STDERR={}", + result.getExitCode(), + result.getStdout(), + result.getStdout()); + + BACKEND_1.start(); + // BACKEND_2.start(); + // BACKEND_3.start(); + } + + static Network NETWORK = Network.newNetwork(); + + static KafkaContainer KAFKA = + new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) + .withNetwork(NETWORK) + .withNetworkAliases("kafka") + .withListener(() -> "kafka:9999") + .withKraft() + .waitingFor(Wait.forLogMessage(".*Kafka\\ Server\\ started.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("KAFKA")); + + static GenericContainer BACKEND_1 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(NETWORK) + .withNetworkAliases("backend-1") + .withCommand( + "--chat.backend.instance-id=backend_1", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-1:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=B1", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + ) + .withExposedPorts(8080) + .dependsOn(KAFKA) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); + + static GenericContainer BACKEND_2 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(NETWORK) + .withNetworkAliases("backend-2") + .withCommand( + "--chat.backend.instance-id=backend_2", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-2:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=B2", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + ) + .withExposedPorts(8080) + .dependsOn(KAFKA) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); + + + static GenericContainer BACKEND_3 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(NETWORK) + .withNetworkAliases("backend-3") + .withCommand( + "--chat.backend.instance-id=backend_3", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-3:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=B3", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + ) + .withExposedPorts(8080) + .dependsOn(KAFKA) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); + + static GenericContainer HAPROXY = + new GenericContainer(DockerImageName.parse("haproxytech/haproxy-debian:2.8")) + .withNetwork(NETWORK) + .withNetworkAliases("haproxy") + .withClasspathResourceMapping( + "haproxy.cfg", + "/usr/local/etc/haproxy/haproxy.cfg", + BindMode.READ_ONLY) + .withClasspathResourceMapping( + "sharding.map", + "/usr/local/etc/haproxy/sharding.map", + BindMode.READ_WRITE) + .withExposedPorts(8400, 8401, 8404) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("HAPROXY")); +} diff --git a/src/test/resources/haproxy.cfg b/src/test/resources/haproxy.cfg new file mode 100644 index 00000000..230b8f97 --- /dev/null +++ b/src/test/resources/haproxy.cfg @@ -0,0 +1,39 @@ +# Config from Blog-article "How to Run HAProxy with Docker" +defaults + mode http + timeout client 10s + timeout connect 5s + timeout server 10m + timeout http-request 10s + log global + default-server init-addr last,libc,none + +global + stats socket ipv4@:8401 level admin + stats socket /var/run/haproxy.sock mode 666 level admin + stats timeout 2m + +frontend stats + bind *:8404 + stats enable + stats uri / + stats refresh 10s + +frontend frontend + bind :8400 + default_backend random + use_backend %[req.hdr(X-Shard),map(/usr/local/etc/haproxy/sharding.map)] + +backend random + server b1 backend-1:8080 check + server b2 backend-2:8080 check + server b3 backend-3:8080 check + +backend backend_1 + server b1 backend-1:8080 check + +backend backend_2 + server b2 backend-2:8080 check + +backend backend_3 + server b3 backend-3:8080 check diff --git a/src/test/resources/sharding.map b/src/test/resources/sharding.map new file mode 100644 index 00000000..53346faa --- /dev/null +++ b/src/test/resources/sharding.map @@ -0,0 +1,10 @@ +0 backend_1 +1 backend_2 +2 backend_3 +3 backend_1 +4 backend_2 +5 backend_3 +6 backend_1 +7 backend_2 +8 backend_3 +9 backend_1 -- 2.20.1