-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A simple Hazelcast example using both a single container and a cluster with two nodes. Fixes #6115
- Loading branch information
1 parent
8fe5901
commit 8424cbc
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
96 changes: 96 additions & 0 deletions
96
examples/hazelcast/src/test/java/org/testcontainers/examples/HazelcastContainerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters