Skip to content

Commit 5d2abc9

Browse files
Tian Baoqiangpivovarit
authored andcommitted
add concurrentmap tests (eugenp#1051)
1 parent fb61d1b commit 5d2abc9

4 files changed

Lines changed: 416 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.java.concurrentmap;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
12+
import java.util.concurrent.TimeUnit;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotEquals;
16+
17+
public class ConcurrentMapAggregateStatusTest {
18+
19+
private ExecutorService executorService;
20+
private Map<String, Integer> concurrentMap;
21+
private List<Integer> mapSizes;
22+
private int MAX_SIZE = 100000;
23+
24+
@Before
25+
public void init() {
26+
executorService = Executors.newFixedThreadPool(2);
27+
concurrentMap = new ConcurrentHashMap<>();
28+
mapSizes = new ArrayList<>(MAX_SIZE);
29+
}
30+
31+
@Test
32+
public void givenConcurrentMap_whenSizeWithoutConcurrentUpdates_thenCorrect() throws InterruptedException {
33+
Runnable collectMapSizes = () -> {
34+
for (int i = 0; i < MAX_SIZE; i++) {
35+
concurrentMap.put(String.valueOf(i), i);
36+
mapSizes.add(concurrentMap.size());
37+
}
38+
};
39+
Runnable retrieveMapData = () -> {
40+
for (int i = 0; i < MAX_SIZE; i++) {
41+
concurrentMap.get(String.valueOf(i));
42+
}
43+
};
44+
executorService.execute(retrieveMapData);
45+
executorService.execute(collectMapSizes);
46+
executorService.shutdown();
47+
executorService.awaitTermination(1, TimeUnit.MINUTES);
48+
49+
for (int i = 1; i <= MAX_SIZE; i++) {
50+
assertEquals("map size should be consistently reliable", i, mapSizes
51+
.get(i - 1)
52+
.intValue());
53+
}
54+
assertEquals(MAX_SIZE, concurrentMap.size());
55+
}
56+
57+
@Test
58+
public void givenConcurrentMap_whenUpdatingAndGetSize_thenError() throws InterruptedException {
59+
Runnable collectMapSizes = () -> {
60+
for (int i = 0; i < MAX_SIZE; i++) {
61+
mapSizes.add(concurrentMap.size());
62+
}
63+
};
64+
Runnable updateMapData = () -> {
65+
for (int i = 0; i < MAX_SIZE; i++) {
66+
concurrentMap.put(String.valueOf(i), i);
67+
}
68+
};
69+
executorService.execute(updateMapData);
70+
executorService.execute(collectMapSizes);
71+
executorService.shutdown();
72+
executorService.awaitTermination(1, TimeUnit.MINUTES);
73+
74+
assertNotEquals("map size collected with concurrent updates not reliable", MAX_SIZE, mapSizes
75+
.get(MAX_SIZE - 1)
76+
.intValue());
77+
assertEquals(MAX_SIZE, concurrentMap.size());
78+
}
79+
80+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.baeldung.java.concurrentmap;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.ConcurrentMap;
10+
11+
import static org.junit.Assert.assertNull;
12+
13+
public class ConcurrentMapNullKeyValueTest {
14+
15+
ConcurrentMap<String, Object> concurrentMap;
16+
17+
@Before
18+
public void setup() {
19+
concurrentMap = new ConcurrentHashMap<>();
20+
}
21+
22+
@Test(expected = NullPointerException.class)
23+
public void givenConcurrentHashMap_whenGetWithNullKey_thenThrowsNPE() {
24+
concurrentMap.get(null);
25+
}
26+
27+
@Test(expected = NullPointerException.class)
28+
public void givenConcurrentHashMap_whenGetOrDefaultWithNullKey_thenThrowsNPE() {
29+
concurrentMap.getOrDefault(null, new Object());
30+
}
31+
32+
@Test(expected = NullPointerException.class)
33+
public void givenConcurrentHashMap_whenPutWithNullKey_thenThrowsNPE() {
34+
concurrentMap.put(null, new Object());
35+
}
36+
37+
@Test(expected = NullPointerException.class)
38+
public void givenConcurrentHashMap_whenPutNullValue_thenThrowsNPE() {
39+
concurrentMap.put("test", null);
40+
}
41+
42+
@Test(expected = NullPointerException.class)
43+
public void givenConcurrentHashMapAndKeyAbsent_whenPutWithNullKey_thenThrowsNPE() {
44+
concurrentMap.putIfAbsent(null, new Object());
45+
}
46+
47+
@Test(expected = NullPointerException.class)
48+
public void givenConcurrentHashMapAndMapWithNullKey_whenPutNullKeyMap_thenThrowsNPE() {
49+
Map<String, Object> nullKeyMap = new HashMap<>();
50+
nullKeyMap.put(null, new Object());
51+
concurrentMap.putAll(nullKeyMap);
52+
}
53+
54+
@Test(expected = NullPointerException.class)
55+
public void givenConcurrentHashMapAndMapWithNullValue_whenPutNullValueMap_thenThrowsNPE() {
56+
Map<String, Object> nullValueMap = new HashMap<>();
57+
nullValueMap.put("test", null);
58+
concurrentMap.putAll(nullValueMap);
59+
}
60+
61+
@Test(expected = NullPointerException.class)
62+
public void givenConcurrentHashMap_whenReplaceNullKeyWithValues_thenThrowsNPE() {
63+
concurrentMap.replace(null, new Object(), new Object());
64+
}
65+
66+
@Test(expected = NullPointerException.class)
67+
public void givenConcurrentHashMap_whenReplaceWithNullNewValue_thenThrowsNPE() {
68+
Object o = new Object();
69+
concurrentMap.replace("test", o, null);
70+
}
71+
72+
@Test(expected = NullPointerException.class)
73+
public void givenConcurrentHashMap_whenReplaceOldNullValue_thenThrowsNPE() {
74+
Object o = new Object();
75+
concurrentMap.replace("test", null, o);
76+
}
77+
78+
@Test(expected = NullPointerException.class)
79+
public void givenConcurrentHashMap_whenReplaceWithNullValue_thenThrowsNPE() {
80+
concurrentMap.replace("test", null);
81+
}
82+
83+
@Test(expected = NullPointerException.class)
84+
public void givenConcurrentHashMap_whenReplaceNullKey_thenThrowsNPE() {
85+
concurrentMap.replace(null, "test");
86+
}
87+
88+
@Test(expected = NullPointerException.class)
89+
public void givenConcurrentHashMap_whenReplaceAllMappingNull_thenThrowsNPE() {
90+
concurrentMap.put("test", new Object());
91+
concurrentMap.replaceAll((s, o) -> null);
92+
}
93+
94+
@Test(expected = NullPointerException.class)
95+
public void givenConcurrentHashMap_whenRemoveNullKey_thenThrowsNPE() {
96+
concurrentMap.remove(null);
97+
}
98+
99+
@Test(expected = NullPointerException.class)
100+
public void givenConcurrentHashMap_whenRemoveNullKeyWithValue_thenThrowsNPE() {
101+
concurrentMap.remove(null, new Object());
102+
}
103+
104+
@Test(expected = NullPointerException.class)
105+
public void givenConcurrentHashMap_whenMergeNullKeyWithValue_thenThrowsNPE() {
106+
concurrentMap.merge(null, new Object(), (o, o2) -> o2);
107+
}
108+
109+
@Test(expected = NullPointerException.class)
110+
public void givenConcurrentHashMap_whenMergeKeyWithNullValue_thenThrowsNPE() {
111+
concurrentMap.put("test", new Object());
112+
concurrentMap.merge("test", null, (o, o2) -> o2);
113+
}
114+
115+
@Test(expected = NullPointerException.class)
116+
public void givenConcurrentHashMapAndAssumeKeyAbsent_whenComputeWithNullKey_thenThrowsNPE() {
117+
concurrentMap.computeIfAbsent(null, s -> s);
118+
}
119+
120+
@Test(expected = NullPointerException.class)
121+
public void givenConcurrentHashMapAndAssumeKeyPresent_whenComputeWithNullKey_thenThrowsNPE() {
122+
concurrentMap.computeIfPresent(null, (s, o) -> o);
123+
}
124+
125+
@Test(expected = NullPointerException.class)
126+
public void givenConcurrentHashMap_whenComputeWithNullKey_thenThrowsNPE() {
127+
concurrentMap.compute(null, (s, o) -> o);
128+
}
129+
130+
@Test
131+
public void givenConcurrentHashMap_whenMergeKeyRemappingNull_thenRemovesMapping() {
132+
Object oldValue = new Object();
133+
concurrentMap.put("test", oldValue);
134+
concurrentMap.merge("test", new Object(), (o, o2) -> null);
135+
assertNull(concurrentMap.get("test"));
136+
}
137+
138+
@Test
139+
public void givenConcurrentHashMapAndKeyAbsent_whenComputeWithKeyRemappingNull_thenRemainsAbsent() {
140+
concurrentMap.computeIfPresent("test", (s, o) -> null);
141+
assertNull(concurrentMap.get("test"));
142+
}
143+
144+
@Test
145+
public void givenKeyPresent_whenComputeIfPresentRemappingNull_thenMappingRemoved() {
146+
Object oldValue = new Object();
147+
concurrentMap.put("test", oldValue);
148+
concurrentMap.computeIfPresent("test", (s, o) -> null);
149+
assertNull(concurrentMap.get("test"));
150+
}
151+
152+
@Test
153+
public void givenKeyPresent_whenComputeRemappingNull_thenMappingRemoved() {
154+
Object oldValue = new Object();
155+
concurrentMap.put("test", oldValue);
156+
concurrentMap.compute("test", (s, o) -> null);
157+
assertNull(concurrentMap.get("test"));
158+
}
159+
160+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.java.concurrentmap;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.Hashtable;
8+
import java.util.Map;
9+
import java.util.concurrent.*;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNotEquals;
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class ConcurrentMapPerformanceTest {
16+
17+
@Test
18+
public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception {
19+
Map<String, Object> hashtable = new Hashtable<>();
20+
Map<String, Object> synchronizedHashMap = Collections.synchronizedMap(new HashMap<>());
21+
Map<String, Object> concurrentHashMap = new ConcurrentHashMap<>();
22+
23+
long hashtableAvgRuntime = timeElapseForGetPut(hashtable);
24+
long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap);
25+
long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap);
26+
27+
assertTrue(hashtableAvgRuntime > concurrentHashMapAvgRuntime);
28+
assertTrue(syncHashMapAvgRuntime > concurrentHashMapAvgRuntime);
29+
30+
System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime));
31+
32+
}
33+
34+
private long timeElapseForGetPut(Map<String, Object> map) throws InterruptedException {
35+
ExecutorService executorService = Executors.newFixedThreadPool(4);
36+
long startTime = System.nanoTime();
37+
for (int i = 0; i < 4; i++) {
38+
executorService.execute(() -> {
39+
for (int j = 0; j < 500_000; j++) {
40+
int value = ThreadLocalRandom
41+
.current()
42+
.nextInt(10000);
43+
String key = String.valueOf(value);
44+
map.put(key, value);
45+
map.get(key);
46+
}
47+
});
48+
}
49+
executorService.shutdown();
50+
executorService.awaitTermination(1, TimeUnit.MINUTES);
51+
return (System.nanoTime() - startTime) / 500_000;
52+
}
53+
54+
@Test
55+
public void givenConcurrentMap_whenKeyWithSameHashCode_thenPerformanceDegrades() throws InterruptedException {
56+
class SameHash {
57+
@Override
58+
public int hashCode() {
59+
return 1;
60+
}
61+
}
62+
int executeTimes = 5000;
63+
64+
Map<SameHash, Integer> mapOfSameHash = new ConcurrentHashMap<>();
65+
ExecutorService executorService = Executors.newFixedThreadPool(2);
66+
long sameHashStartTime = System.currentTimeMillis();
67+
for (int i = 0; i < 2; i++) {
68+
executorService.execute(() -> {
69+
for (int j = 0; j < executeTimes; j++) {
70+
mapOfSameHash.put(new SameHash(), 1);
71+
}
72+
});
73+
}
74+
executorService.shutdown();
75+
executorService.awaitTermination(5, TimeUnit.SECONDS);
76+
77+
long mapOfSameHashDuration = System.currentTimeMillis() - sameHashStartTime;
78+
Map<Object, Integer> mapOfDefaultHash = new ConcurrentHashMap<>();
79+
executorService = Executors.newFixedThreadPool(2);
80+
long defaultHashStartTime = System.currentTimeMillis();
81+
for (int i = 0; i < 2; i++) {
82+
executorService.execute(() -> {
83+
for (int j = 0; j < executeTimes; j++) {
84+
mapOfDefaultHash.put(new Object(), 1);
85+
}
86+
});
87+
}
88+
executorService.shutdown();
89+
executorService.awaitTermination(5, TimeUnit.SECONDS);
90+
91+
long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime;
92+
assertEquals(executeTimes * 2, mapOfDefaultHash.size());
93+
assertNotEquals(executeTimes * 2, mapOfSameHash.size());
94+
System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration));
95+
assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > mapOfDefaultHashDuration * 10);
96+
}
97+
98+
}

0 commit comments

Comments
 (0)