Skip to content

Commit

Permalink
Use Hazelcast's Docker Image instead of an embedded Hazelcast instance
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Jan 10, 2023
1 parent 6f3eb79 commit 717200e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 67 deletions.
6 changes: 2 additions & 4 deletions tests/src/com.hazelcast/hazelcast/5.2.1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ dependencies {
testImplementation "com.hazelcast:hazelcast:$libraryVersion"
testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'javax.cache:cache-api:1.1.1'
}

test {
jvmArgs = Arrays.asList("-Xmx4g")
testImplementation 'org.awaitility:awaitility:4.2.0'
testImplementation 'ch.qos.logback:logback-classic:1.2.11'
}

graalvmNative {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import com.hazelcast.cache.ICache;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.GlobalSerializerConfig;
import com.hazelcast.config.SerializerConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.cp.IAtomicLong;
import com.hazelcast.map.IMap;
Expand All @@ -35,9 +33,13 @@
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.AccessedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.spi.CachingProvider;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand All @@ -51,18 +53,30 @@
import static com.hazelcast.query.Predicates.equal;
import static com.hazelcast.query.Predicates.sql;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

class HazelcastTest {
static HazelcastInstance hazelcastInstance;
private static Process process;

@BeforeAll
static void beforeAll() {
hazelcastInstance = Hazelcast.newHazelcastInstance(new Config());
static void beforeAll() throws IOException {
System.out.println("Starting Hazelcast ...");
process = new ProcessBuilder("docker", "run", "--rm", "-p", "5701:5701",
"-e", "JAVA_OPTS=-Xmx1024M", "hazelcast/hazelcast:5.2.1")
.redirectOutput(new File("hazelcast-stdout.txt")).redirectError(new File("hazelcast-stderr.txt")).start();
await().atMost(java.time.Duration.ofMinutes(5)).ignoreExceptions().until(() -> {
HazelcastClient.newHazelcastClient().shutdown();
return true;
});
System.out.println("Hazelcast started");
}

@AfterAll
static void afterAll() {
hazelcastInstance.shutdown();
if (process != null && process.isAlive()) {
System.out.println("Shutting down Hazelcast");
process.destroy();
}
}

@Test
Expand Down Expand Up @@ -256,4 +270,21 @@ void testTopic() {
IntStream.range(0, 3).mapToObj(i -> "Hello to distributed world").forEach(topic::publish);
client.shutdown();
}

@Test
void testJCacheOrigin() {
CachingProvider cachingProvider = Caching.getCachingProvider(HazelcastCachingProvider.class.getName());
CacheManager cacheManager = cachingProvider.getCacheManager();
CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>()
.setTypes(String.class, String.class)
.setStatisticsEnabled(true)
.setReadThrough(false)
.setManagementEnabled(true)
.setStoreByValue(false)
.setWriteThrough(false);
Cache<String, String> cache = cacheManager.createCache("example", config);
cache.put("world", "Hello World");
assertThat(cache.get("world")).isEqualTo("Hello World");
assertThat(cacheManager.getCache("example", String.class, String.class)).isNotNull();
}
}

This file was deleted.

0 comments on commit 717200e

Please sign in to comment.