From f5809dc0c06f7d58da39634ad5ed72f0200cae4e Mon Sep 17 00:00:00 2001 From: Tomaz Fernandes Date: Thu, 3 Nov 2022 01:27:15 -0300 Subject: [PATCH] Fix race condition for cluster startup Sometimes the client would connect to the first container before the second one registers in the cluster. Add a condition to wait for both nodes to be registered when starting the containers. --- .../examples/HazelcastContainerTest.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/hazelcast/src/test/java/org/testcontainers/examples/HazelcastContainerTest.java b/examples/hazelcast/src/test/java/org/testcontainers/examples/HazelcastContainerTest.java index f70abd75765..6321c8398f3 100644 --- a/examples/hazelcast/src/test/java/org/testcontainers/examples/HazelcastContainerTest.java +++ b/examples/hazelcast/src/test/java/org/testcontainers/examples/HazelcastContainerTest.java @@ -3,10 +3,11 @@ import com.hazelcast.client.HazelcastClient; import com.hazelcast.client.config.ClientConfig; import com.hazelcast.core.HazelcastInstance; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Test; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.Network; +import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.DockerImageName; @@ -26,22 +27,24 @@ public class HazelcastContainerTest { private static final int DEFAULT_EXPOSED_PORT = 5701; + private static final String CLUSTER_STARTUP_LOG_MESSAGE_REGEX = ".*Members \\{size:2, ver:2\\}.*"; + // Test values private static final String HOST_PORT_SEPARATOR = ":"; private static final String TEST_QUEUE_NAME = "test-queue"; - private static final String TEST_CLUSTER_NAME = "myClusterName"; + private static final String TEST_CLUSTER_NAME = "test-cluster"; private static final String TEST_VALUE = "Hello!"; - @After - public void cleanUp() { + @AfterClass + public static void cleanUp() { HazelcastClient.shutdownAll(); } @Test - public void singleContainer() throws InterruptedException { + public void singleHazelcastContainer() throws InterruptedException { try ( GenericContainer container = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME)) .withExposedPorts(DEFAULT_EXPOSED_PORT) @@ -68,10 +71,12 @@ public void hazelcastCluster() throws InterruptedException { GenericContainer container1 = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME)) .withExposedPorts(DEFAULT_EXPOSED_PORT) .withEnv(HZ_CLUSTERNAME_ENV_NAME, TEST_CLUSTER_NAME) + .waitingFor(Wait.forLogMessage(CLUSTER_STARTUP_LOG_MESSAGE_REGEX, 1)) .withNetwork(network); GenericContainer container2 = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME)) .withExposedPorts(DEFAULT_EXPOSED_PORT) .withEnv(HZ_CLUSTERNAME_ENV_NAME, TEST_CLUSTER_NAME) + .waitingFor(Wait.forLogMessage(CLUSTER_STARTUP_LOG_MESSAGE_REGEX, 1)) .withNetwork(network) ) { Startables.deepStart(container1, container2).join();