From: Kai Moritz Date: Tue, 27 Feb 2024 10:54:01 +0000 (+0100) Subject: WIP:test: HandoverIT-POC - splitted up code into smaller classes -- MOVE X-Git-Tag: rebase--2024-02-27--11-55~12 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=0fbb2cc385499af17876c99146fec8be03c4a0f6;p=demos%2Fkafka%2Fchat WIP:test: HandoverIT-POC - splitted up code into smaller classes -- MOVE --- 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..f384f7e8 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java @@ -0,0 +1,274 @@ +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.awaitility.Awaitility; +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.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.images.ImagePullPolicy; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.util.retry.Retry; + +import java.io.IOException; +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; + + + abstract void setUpExtra() throws IOException, InterruptedException; + + @BeforeEach + void setUp() throws Exception + { + setUpExtra(); + haproxy.start(); + backend1.start(); + // backend2.start(); + // backend3.start(); + + Integer port = haproxy.getMappedPort(8400); + webClient = WebClient.create("http://localhost:" + port); + + Awaitility + .await() + .atMost(Duration.ofMinutes(10)) + .until(() -> WebClient + .create("http://localhost:" + backend1.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()); + } + + Network network = Network.newNetwork(); + + 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")); + + abstract String[] getCommandBackend1(); + GenericContainer backend1 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-1") + .withCommand(getCommandBackend1()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); + + abstract String[] getCommandBackend2(); + GenericContainer backend2 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-2") + .withCommand(getCommandBackend2()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); + + abstract String[] getCommandBackend3(); + GenericContainer backend3 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-3") + .withCommand(getCommandBackend3()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); + + + @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/AbstractHandoverITContainers.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java new file mode 100644 index 00000000..b22166fa --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverITContainers.java @@ -0,0 +1,207 @@ +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.awaitility.Awaitility; +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.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.images.ImagePullPolicy; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.util.retry.Retry; + +import java.io.IOException; +import java.time.Duration; +import java.util.stream.IntStream; + + +@Testcontainers +@Slf4j +public abstract class AbstractHandoverIT +{ + static final ImagePullPolicy NEVER_PULL = imageName -> false; + + + @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(); + } + + + abstract void setUpExtra() throws IOException, InterruptedException; + + @BeforeEach + void setUp() throws Exception + { + setUpExtra(); + haproxy.start(); + backend1.start(); + // backend2.start(); + // backend3.start(); + + Awaitility + .await() + .atMost(Duration.ofMinutes(10)) + .until(() -> WebClient + .create("http://localhost:" + backend1.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 + .create("http://localhost:" + haproxy.getMappedPort(8400)) + .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()); + } + + Network network = Network.newNetwork(); + + 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")); + + abstract String[] getCommandBackend1(); + GenericContainer backend1 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-1") + .withCommand(getCommandBackend1()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); + + abstract String[] getCommandBackend2(); + GenericContainer backend2 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-2") + .withCommand(getCommandBackend2()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); + + abstract String[] getCommandBackend3(); + GenericContainer backend3 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-3") + .withCommand(getCommandBackend3()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); + + + @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/HandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/HandoverIT.java deleted file mode 100644 index f384f7e8..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/HandoverIT.java +++ /dev/null @@ -1,274 +0,0 @@ -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.awaitility.Awaitility; -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.containers.BindMode; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.Network; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.images.ImagePullPolicy; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.util.retry.Retry; - -import java.io.IOException; -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; - - - abstract void setUpExtra() throws IOException, InterruptedException; - - @BeforeEach - void setUp() throws Exception - { - setUpExtra(); - haproxy.start(); - backend1.start(); - // backend2.start(); - // backend3.start(); - - Integer port = haproxy.getMappedPort(8400); - webClient = WebClient.create("http://localhost:" + port); - - Awaitility - .await() - .atMost(Duration.ofMinutes(10)) - .until(() -> WebClient - .create("http://localhost:" + backend1.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()); - } - - Network network = Network.newNetwork(); - - 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")); - - abstract String[] getCommandBackend1(); - GenericContainer backend1 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-1") - .withCommand(getCommandBackend1()) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); - - abstract String[] getCommandBackend2(); - GenericContainer backend2 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-2") - .withCommand(getCommandBackend2()) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); - - abstract String[] getCommandBackend3(); - GenericContainer backend3 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-3") - .withCommand(getCommandBackend3()) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); - - - @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/HandoverITContainers.java b/src/test/java/de/juplo/kafka/chat/backend/HandoverITContainers.java deleted file mode 100644 index b22166fa..00000000 --- a/src/test/java/de/juplo/kafka/chat/backend/HandoverITContainers.java +++ /dev/null @@ -1,207 +0,0 @@ -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.awaitility.Awaitility; -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.containers.BindMode; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.Network; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.images.ImagePullPolicy; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.util.retry.Retry; - -import java.io.IOException; -import java.time.Duration; -import java.util.stream.IntStream; - - -@Testcontainers -@Slf4j -public abstract class AbstractHandoverIT -{ - static final ImagePullPolicy NEVER_PULL = imageName -> false; - - - @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(); - } - - - abstract void setUpExtra() throws IOException, InterruptedException; - - @BeforeEach - void setUp() throws Exception - { - setUpExtra(); - haproxy.start(); - backend1.start(); - // backend2.start(); - // backend3.start(); - - Awaitility - .await() - .atMost(Duration.ofMinutes(10)) - .until(() -> WebClient - .create("http://localhost:" + backend1.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 - .create("http://localhost:" + haproxy.getMappedPort(8400)) - .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()); - } - - Network network = Network.newNetwork(); - - 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")); - - abstract String[] getCommandBackend1(); - GenericContainer backend1 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-1") - .withCommand(getCommandBackend1()) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); - - abstract String[] getCommandBackend2(); - GenericContainer backend2 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-2") - .withCommand(getCommandBackend2()) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); - - abstract String[] getCommandBackend3(); - GenericContainer backend3 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-3") - .withCommand(getCommandBackend3()) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); - - - @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; - } -}