/
ConcurrencyUtilTest.java
138 lines (108 loc) · 4.66 KB
/
ConcurrencyUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.util;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelTest.class})
public class ConcurrencyUtilTest extends HazelcastTestSupport {
private final Object mutex = new Object();
private final ContextMutexFactory mutexFactory = new ContextMutexFactory();
private final IntIntConstructorFunction constructorFunction = new IntIntConstructorFunction();
private ConcurrentMap<Integer, Integer> map = new ConcurrentHashMap<Integer, Integer>();
@Test
public void testConstructor() {
assertUtilityConstructor(ConcurrencyUtil.class);
}
@Test
public void setMax() {
setMax(8, 7);
setMax(9, 9);
setMax(10, 11);
}
private void setMax(long current, long update) {
LongValue longValue = new LongValue();
longValue.value = current;
ConcurrencyUtil.setMax(longValue, LongValue.UPDATER, update);
long max = Math.max(current, update);
assertEquals(max, longValue.value);
}
@Test
public void testGetOrPutSynchronized() {
int result = ConcurrencyUtil.getOrPutSynchronized(map, 5, mutex, constructorFunction);
assertEquals(1005, result);
assertEquals(1, constructorFunction.getConstructions());
}
@SuppressWarnings("ConstantConditions")
@Test(expected = NullPointerException.class)
public void testGetOrPutSynchronized_whenMutexIsNull_thenThrowException() {
ConcurrencyUtil.getOrPutSynchronized(map, 5, (Object) null, constructorFunction);
}
@Test
public void testGetOrPutSynchronized_withMutexFactory() {
int result = ConcurrencyUtil.getOrPutSynchronized(map, 5, mutexFactory, constructorFunction);
assertEquals(1005, result);
assertEquals(1, constructorFunction.getConstructions());
}
@SuppressWarnings("ConstantConditions")
@Test(expected = NullPointerException.class)
public void testGetOrPutSynchronized_whenMutexFactoryIsNull_thenThrowException() {
ContextMutexFactory factory = null;
ConcurrencyUtil.getOrPutSynchronized(map, 5, factory, constructorFunction);
}
@Test
public void testSetIfEqualOrGreaterThan() {
assertTrue(ConcurrencyUtil.setIfEqualOrGreaterThan(new AtomicLong(1), 1));
assertTrue(ConcurrencyUtil.setIfEqualOrGreaterThan(new AtomicLong(1), 2));
assertFalse(ConcurrencyUtil.setIfEqualOrGreaterThan(new AtomicLong(2), 1));
}
@Test
public void testGetOrPutIfAbsent() {
int result = ConcurrencyUtil.getOrPutIfAbsent(map, 5, constructorFunction);
assertEquals(1005, result);
result = ConcurrencyUtil.getOrPutIfAbsent(map, 5, constructorFunction);
assertEquals(1005, result);
assertEquals(1, constructorFunction.getConstructions());
}
private static final class LongValue {
static final AtomicLongFieldUpdater UPDATER = AtomicLongFieldUpdater.newUpdater(LongValue.class, "value");
volatile long value;
}
private static class IntIntConstructorFunction implements ConstructorFunction<Integer, Integer> {
private AtomicInteger constructions = new AtomicInteger();
@Override
public Integer createNew(Integer key) {
constructions.incrementAndGet();
return key + 1000;
}
int getConstructions() {
return constructions.get();
}
}
}