Skip to content

Commit

Permalink
Add Hazelcast Example (#6115)
Browse files Browse the repository at this point in the history
A simple Hazelcast example using both a single container and a cluster with two nodes.

Fixes #6115
  • Loading branch information
tomazfernandes committed Nov 3, 2022
1 parent 8fe5901 commit 8424cbc
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/hazelcast/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.testcontainers:testcontainers:1.17.5'
testImplementation 'com.hazelcast:hazelcast:5.2.0'
testImplementation 'ch.qos.logback:logback-classic:1.3.4'
testImplementation 'org.assertj:assertj-core:3.23.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.testcontainers.examples;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import org.junit.After;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.lifecycle.Startables;
import org.testcontainers.utility.DockerImageName;

import java.util.concurrent.BlockingQueue;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Examples with Hazelcast using both a single container and a cluster with two containers.
*/
public class HazelcastContainerTests {

// Hazelcast values
private static final String HZ_IMAGE_NAME = "hazelcast/hazelcast:5.2.0";

private static final String HZ_CLUSTERNAME_ENV_NAME = "HZ_CLUSTERNAME";

private static final int DEFAULT_EXPOSED_PORT = 5701;

// 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_VALUE = "Hello!";

@After
public void cleanUp() {
HazelcastClient.shutdownAll();
}

@Test
public void singleContainer() throws InterruptedException {
try (
GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME))
.withExposedPorts(DEFAULT_EXPOSED_PORT)
) {
container.start();
assertThat(container.isRunning()).isTrue();

ClientConfig clientConfig = new ClientConfig();
clientConfig
.getNetworkConfig()
.addAddress(container.getHost() + HOST_PORT_SEPARATOR + container.getFirstMappedPort());
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

BlockingQueue<String> queue = client.getQueue(TEST_QUEUE_NAME);
queue.put(TEST_VALUE);
assertThat(queue.take()).isEqualTo(TEST_VALUE);
}
}

@Test
public void hazelcastCluster() throws InterruptedException {
Network network = Network.newNetwork();
try (
GenericContainer<?> container1 = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME))
.withExposedPorts(DEFAULT_EXPOSED_PORT)
.withEnv(HZ_CLUSTERNAME_ENV_NAME, TEST_CLUSTER_NAME)
.withNetwork(network);
GenericContainer<?> container2 = new GenericContainer<>(DockerImageName.parse(HZ_IMAGE_NAME))
.withExposedPorts(DEFAULT_EXPOSED_PORT)
.withEnv(HZ_CLUSTERNAME_ENV_NAME, TEST_CLUSTER_NAME)
.withNetwork(network)
) {
Startables.deepStart(container1, container2).join();
assertThat(container1.isRunning()).isTrue();
assertThat(container2.isRunning()).isTrue();

ClientConfig clientConfig = new ClientConfig();
clientConfig
.setClusterName(TEST_CLUSTER_NAME)
.getNetworkConfig()
.addAddress(container1.getHost() + HOST_PORT_SEPARATOR + container1.getFirstMappedPort());

HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

BlockingQueue<String> queue = client.getQueue(TEST_QUEUE_NAME);
queue.put(TEST_VALUE);

assertThat(queue.take()).isEqualTo(TEST_VALUE);
}
}

}
1 change: 1 addition & 0 deletions examples/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include 'cucumber'
include 'spring-boot-kotlin-redis'
include 'immudb'
include 'zookeeper'
include 'hazelcast'

ext.isCI = System.getenv("CI") != null

Expand Down

0 comments on commit 8424cbc

Please sign in to comment.