Skip to content

Commit

Permalink
Fix race condition for cluster startup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomazfernandes committed Nov 3, 2022
1 parent 10ff2c2 commit f5809dc
Showing 1 changed file with 10 additions and 5 deletions.
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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();
Expand Down

0 comments on commit f5809dc

Please sign in to comment.