Skip to content

Commit bc2425e

Browse files
committed
Add tests to run on Hazelcast 3 sample
1 parent e9a8009 commit bc2425e

File tree

8 files changed

+179
-10
lines changed

8 files changed

+179
-10
lines changed

samples/micrometer-samples-hazelcast/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'java'
3+
}
4+
15
dependencies {
26
implementation project(':micrometer-core')
37
implementation 'com.hazelcast:hazelcast:4.+'

samples/micrometer-samples-hazelcast/src/main/java/io/micrometer/core/samples/HazelcastCacheSample.java renamed to samples/micrometer-samples-hazelcast/src/main/java/io/micrometer/samples/hazelcast4/HazelcastCacheSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.micrometer.core.samples;
17+
package io.micrometer.samples.hazelcast4;
1818

1919
import com.hazelcast.core.Hazelcast;
2020
import com.hazelcast.map.IMap;

samples/micrometer-samples-hazelcast-3-compatibility/build.gradle renamed to samples/micrometer-samples-hazelcast3/build.gradle

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ repositories {
66
mavenCentral()
77
}
88

9-
license {
10-
ext.year = Calendar.getInstance().get(Calendar.YEAR)
11-
skipExistingHeaders = true
12-
}
13-
149
dependencies {
1510
implementation project(':micrometer-core')
1611
implementation('com.hazelcast:hazelcast') {
1712
version {
18-
strictly '3.12.6'
13+
strictly '3.+'
1914
}
2015
}
2116
implementation 'ch.qos.logback:logback-classic'
17+
18+
testImplementation project(':micrometer-test')
2219
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 VMware, Inc.
2+
* Copyright 2020 VMware, Inc.
33
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.micrometer.core.samples;
17+
package io.micrometer.samples.hazelcast3;
1818

1919
import com.hazelcast.core.Hazelcast;
2020
import com.hazelcast.core.IMap;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2020 VMware, Inc.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.samples.hazelcast3;
17+
18+
import com.hazelcast.core.Hazelcast;
19+
import com.hazelcast.core.IMap;
20+
import io.micrometer.core.instrument.binder.cache.CacheMeterBinder;
21+
import io.micrometer.core.instrument.binder.cache.CacheMeterBinderCompatibilityKit;
22+
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
23+
import org.junit.jupiter.api.AfterEach;
24+
25+
import static java.util.Collections.emptyList;
26+
27+
class Hazelcast3CacheMetricsCompatibilityTest extends CacheMeterBinderCompatibilityKit {
28+
private IMap<String, String> cache = Hazelcast.newHazelcastInstance().getMap("mycache");
29+
30+
@AfterEach
31+
void cleanup() {
32+
Hazelcast.shutdownAll();
33+
}
34+
35+
@Override
36+
public CacheMeterBinder binder() {
37+
return new HazelcastCacheMetrics(cache, emptyList());
38+
}
39+
40+
@Override
41+
public void put(String key, String value) {
42+
cache.put(key, value);
43+
}
44+
45+
@Override
46+
public String get(String key) {
47+
return cache.get(key);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Copyright 2020 VMware, Inc.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.samples.hazelcast3;
17+
18+
import com.hazelcast.core.Hazelcast;
19+
import com.hazelcast.core.HazelcastInstance;
20+
import com.hazelcast.core.IMap;
21+
import com.hazelcast.monitor.LocalMapStats;
22+
import com.hazelcast.monitor.NearCacheStats;
23+
import io.micrometer.core.instrument.*;
24+
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
25+
import io.micrometer.core.instrument.search.RequiredSearch;
26+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
27+
import org.junit.jupiter.api.AfterEach;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Disabled;
30+
import org.junit.jupiter.api.Test;
31+
32+
import java.util.concurrent.TimeUnit;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Copied from micrometer-core test HazelcastCacheMetricsTest here to ensure cache metrics work with Hazelcast 3.
38+
*/
39+
class Hazelcast3CacheMetricsTest {
40+
41+
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
42+
IMap<String, String> cache = hazelcastInstance.getMap("mycache");
43+
HazelcastCacheMetrics metrics = new HazelcastCacheMetrics(cache, Tags.empty());
44+
LocalMapStats localMapStats = cache.getLocalMapStats();
45+
MeterRegistry meterRegistry = new SimpleMeterRegistry();
46+
47+
@BeforeEach
48+
void setup() {
49+
metrics.bindTo(meterRegistry);
50+
cache.put("hello", "world");
51+
cache.get("hello");
52+
cache.get("hi");
53+
cache.get("hello");
54+
}
55+
56+
@AfterEach
57+
void cleanUp() {
58+
hazelcastInstance.shutdown();
59+
}
60+
61+
@Test
62+
void generalMetrics() {
63+
Gauge backupEntries = fetch(meterRegistry, "cache.entries", Tags.of("ownership", "backup")).gauge();
64+
assertThat(backupEntries.value()).isEqualTo(localMapStats.getBackupEntryCount());
65+
66+
Gauge ownedEntries = fetch(meterRegistry, "cache.entries", Tags.of("ownership", "owned")).gauge();
67+
assertThat(ownedEntries.value()).isEqualTo(localMapStats.getOwnedEntryCount());
68+
69+
Gauge backupEntryMemory = fetch(meterRegistry, "cache.entry.memory", Tags.of("ownership", "backup")).gauge();
70+
assertThat(backupEntryMemory.value()).isEqualTo(localMapStats.getBackupEntryMemoryCost());
71+
72+
Gauge ownedEntryMemory = fetch(meterRegistry, "cache.entry.memory", Tags.of("ownership", "owned")).gauge();
73+
assertThat(ownedEntryMemory.value()).isEqualTo(localMapStats.getOwnedEntryMemoryCost());
74+
75+
FunctionCounter partitionGets = fetch(meterRegistry, "cache.partition.gets").functionCounter();
76+
assertThat(partitionGets.count()).isEqualTo(localMapStats.getGetOperationCount());
77+
}
78+
79+
@Disabled("TODO figure out how to setup near cache properly")
80+
@Test
81+
void nearCacheMetrics() {
82+
NearCacheStats nearCacheStats = localMapStats.getNearCacheStats();
83+
Gauge hitCacheRequests = fetch(meterRegistry, "cache.near.requests", Tags.of("result", "hit")).gauge();
84+
assertThat(hitCacheRequests.value()).isEqualTo(nearCacheStats.getHits());
85+
86+
Gauge missCacheRequests = fetch(meterRegistry, "cache.near.requests", Tags.of("result", "miss")).gauge();
87+
assertThat(missCacheRequests.value()).isEqualTo(nearCacheStats.getMisses());
88+
89+
Gauge nearPersistance = fetch(meterRegistry, "cache.near.persistences").gauge();
90+
assertThat(nearPersistance.value()).isEqualTo(nearCacheStats.getPersistenceCount());
91+
92+
Gauge nearEvictions = fetch(meterRegistry, "cache.near.evictions").gauge();
93+
assertThat(nearEvictions.value()).isEqualTo(nearCacheStats.getEvictions());
94+
}
95+
96+
@Test
97+
void timingsMetrics() {
98+
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
99+
FunctionTimer getsLatency = fetch(meterRegistry, "cache.gets.latency").functionTimer();
100+
assertThat(getsLatency.count()).isEqualTo(localMapStats.getGetOperationCount());
101+
assertThat(getsLatency.totalTime(timeUnit)).isEqualTo(localMapStats.getTotalGetLatency());
102+
103+
FunctionTimer putsLatency = fetch(meterRegistry, "cache.puts.latency").functionTimer();
104+
assertThat(putsLatency.count()).isEqualTo(localMapStats.getPutOperationCount());
105+
assertThat(putsLatency.totalTime(timeUnit)).isEqualTo(localMapStats.getTotalPutLatency());
106+
107+
FunctionTimer removeLatency = fetch(meterRegistry, "cache.removals.latency").functionTimer();
108+
assertThat(removeLatency.count()).isEqualTo(localMapStats.getRemoveOperationCount());
109+
assertThat(removeLatency.totalTime(timeUnit)).isEqualTo(localMapStats.getTotalRemoveLatency());
110+
}
111+
112+
private RequiredSearch fetch(MeterRegistry meterRegistry, String name) {
113+
return fetch(meterRegistry, name, Tags.empty());
114+
}
115+
116+
private RequiredSearch fetch(MeterRegistry meterRegistry, String name, Iterable<Tag> tags) {
117+
return meterRegistry.get(name).tags(tags);
118+
}
119+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ rootProject.name = 'micrometer'
33
include 'micrometer-core'
44
include 'micrometer-jersey2'
55

6-
['core', 'boot2', 'spring-integration', 'hazelcast', 'hazelcast-3-compatibility'].each { sample ->
6+
['core', 'boot2', 'spring-integration', 'hazelcast', 'hazelcast3'].each { sample ->
77
include "micrometer-samples-$sample"
88
project(":micrometer-samples-$sample").projectDir = new File(rootProject.projectDir, "samples/micrometer-samples-$sample")
99
}

0 commit comments

Comments
 (0)