WIP:startup
authorKai Moritz <kai@juplo.de>
Fri, 23 Feb 2024 10:05:27 +0000 (11:05 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 23 Feb 2024 15:40:03 +0000 (16:40 +0100)
src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java

index 3d24daf..800d17e 100644 (file)
@@ -12,6 +12,7 @@ import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.containers.KafkaContainer;
 import org.testcontainers.containers.Network;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.Wait;
 import org.testcontainers.junit.jupiter.Container;
 import org.testcontainers.shaded.org.awaitility.Awaitility;
 import org.testcontainers.utility.DockerImageName;
@@ -71,16 +72,6 @@ class KafkaHandoverIT extends AbstractHandoverIT
   {
     Integer port = haproxy.getMappedPort(8400);
     webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build();
-
-    Awaitility
-        .await()
-        .atMost(Duration.ofSeconds(15))
-        .untilAsserted(() -> webTestClient
-              .get()
-              .uri("/actuator/health")
-              .exchange()
-              .expectStatus().isOk()
-              .expectBody().jsonPath("$.status").isEqualTo("UP"));
   }
 
 
@@ -88,22 +79,6 @@ class KafkaHandoverIT extends AbstractHandoverIT
 
   Network network = Network.newNetwork();
 
-  @Container
-  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, 5555)
-          .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("HAPROXY"));
-
   @Container
   KafkaContainer kafka =
       new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0"))
@@ -112,6 +87,7 @@ class KafkaHandoverIT extends AbstractHandoverIT
           .withListener(() -> "kafka:9999")
           .withEnv("KAFKA_NUM_PARTITIONS", "10")
           .withKraft()
+          .waitingFor(Wait.forLogMessage(".*Kafka Server started.*\\n", 1))
           .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("KAFKA"));
 
   @Container
@@ -130,6 +106,8 @@ class KafkaHandoverIT extends AbstractHandoverIT
               "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401",
               "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map"
           )
+          .dependsOn(kafka)
+          .waitingFor(Wait.forHttp("/actuator/health"))
           .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1"));
 
   @Container
@@ -148,6 +126,8 @@ class KafkaHandoverIT extends AbstractHandoverIT
               "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401",
               "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map"
           )
+          .dependsOn(kafka)
+          .waitingFor(Wait.forHttp("/actuator/health"))
           .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2"));
 
   @Container
@@ -166,5 +146,24 @@ class KafkaHandoverIT extends AbstractHandoverIT
               "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401",
               "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map"
           )
+          .dependsOn(kafka)
+          .waitingFor(Wait.forHttp("/actuator/health"))
           .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3"));
+
+  @Container
+  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, 5555)
+          .dependsOn(backend_1, backend_2, backend_3)
+          .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("HAPROXY"));
 }