This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSCompareAndSetTest.java
224 lines (170 loc) · 7.84 KB
/
SCompareAndSetTest.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package none.cvg.variables;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import none.cvg.HandlesKataDisplayNames;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import sun.misc.Unsafe;
import static none.cvg.ErrorMessages.TEST_FAILURE;
import static none.cvg.ErrorMessages.UNSAFE_FAILURE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/*
* DONE:
* This test aims at using VarHandles to compare and then set a value of a given variable.
* Each solved test shows how this can be achieved with the traditional reflection calls.
* Each unsolved test provides a few hints that will allow the kata-taker to manually solve
* the exercise to achieve the same goal with MethodHandles.
*/
@DisplayName("Compare and Set field values")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DisplayNameGeneration(HandlesKataDisplayNames.class)
public class SCompareAndSetTest {
private Integer currentValue = 2;
private volatile Integer privateVolatile = 2;
private Integer newValue = 7;
@Test
@Tag("PASSING")
@Order(1)
public void compareAndSetUsingAtomicReference() {
AtomicReference<Integer> atomicReference = new AtomicReference<>(privateVolatile);
boolean exchanged = atomicReference.compareAndSet(privateVolatile, newValue);
assertTrue(exchanged,
"The value should have been changed to 7, " +
"hence exchanged should be true"
);
assertEquals(newValue,
atomicReference.get(),
"The value of the privateVolatile should now be 7");
exchanged = atomicReference.compareAndSet(privateVolatile, newValue);
assertFalse(exchanged,
"The value should not have changed again, " +
"hence exchanged should be false"
);
assertEquals(newValue,
atomicReference.get(),
"The value of the privateVolatile should still be 7");
}
@Test
@Tag("PASSING")
@Order(2)
public void compareAndSetUsingAtomicReferenceFieldUpdater() {
final AtomicReferenceFieldUpdater<SCompareAndSetTest, Integer> valueUpdater =
AtomicReferenceFieldUpdater.newUpdater(SCompareAndSetTest.class,
Integer.class,
"privateVolatile");
boolean exchanged = valueUpdater.compareAndSet(this, currentValue, newValue);
assertTrue(exchanged,
"The value should have been changed to 7, " +
"hence exchanged should be true"
);
assertEquals(newValue,
valueUpdater.get(this),
"The value of the privateVolatile should now be 7");
exchanged = valueUpdater.compareAndSet(this, 2, 33);
assertFalse(exchanged,
"The value should not have changed since the expected value " +
"did not match, hence exchanged should be false"
);
assertEquals(newValue,
valueUpdater.get(this),
"The value of the privateVolatile should still be 7");
}
@Test
@Tag("PASSING")
@Order(3)
public void compareAndSetUsingUnsafe() {
try {
Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
final Unsafe unsafe = (Unsafe) theUnsafeInstance.get(null);
final long offset;
offset = unsafe.objectFieldOffset(
SCompareAndSetTest.class.getDeclaredField("privateVolatile"));
boolean exchanged = unsafe.compareAndSwapObject(this,
offset, currentValue, newValue);
assertTrue(exchanged,
"The value should have been changed to 7, " +
"hence exchanged should be true"
);
assertEquals(newValue,
unsafe.getObject(this, offset),
"The value of the privateVolatile should now be 7");
exchanged = unsafe.compareAndSwapObject(this, offset, 2, 33);
assertFalse(exchanged,
"The value should not have changed since the expected value " +
"did not match, hence exchanged should be false"
);
assertEquals(newValue,
unsafe.getObject(this, offset),
"The value of the privateVolatile should still be 7");
} catch (NoSuchFieldException | IllegalAccessException e) {
fail(UNSAFE_FAILURE.getValue() + e.getMessage());
}
}
@Test
@Tag("PASSING")
@Order(4)
public void compareAndSetUsingVarHandles() {
VarHandle varHandle = null;
try {
/*
* DONE:
* Replace the "null"s with valid values to get a VarHandle.
* Check API: java.lang.invoke.MethodHandles.privateLookupIn(?, ?)
* HINT: params are Target class, Lookup type
* Check API: java.lang.invoke.MethodHandles.Lookup.findVarHandle(?, ?, ?)
* HINT: params are Declaring class, Variable name, Variable type
*/
varHandle = MethodHandles
.privateLookupIn(SCompareAndSetTest.class, MethodHandles.lookup())
.findVarHandle(SCompareAndSetTest.class, "privateVolatile", Integer.class);
/*
* DONE:
* Replace the "false" to a compareAndSet call from 'currentValue' to 'newValue'.
* Check API: java.lang.invoke.VarHandle.compareAndSet(...)
* Three parameters are needed here:
* 1. Instance of the class where the variable is being manipulated.
* 2. The current value to compare
* 3. The new value to set
*/
boolean exchanged = varHandle.compareAndSet(this, currentValue, newValue);
assertTrue(exchanged,
"The value should have been changed to 7, " +
"hence exchanged should be true"
);
assertEquals(newValue,
varHandle.get(this),
"The value of the privateVolatile should now be 7");
/*
* TODO:
* Replace the "false" to a compareAndSet call from 2 to 33.
* Check API: java.lang.invoke.VarHandle.compareAndSet(...)
* Three parameters are needed here:
* 1. Instance of the class where the variable is being manipulated.
* 2. The current value to compare
* 3. The new value to set
*/
exchanged = varHandle.compareAndSet(this, 2, 33);
assertFalse(exchanged,
"The value should not have changed since the expected value " +
"did not match, hence exchanged should be false"
);
assertEquals(newValue,
varHandle.get(this),
"The value of the privateVolatile should still be 7");
} catch (NoSuchFieldException | IllegalAccessException e) {
fail(TEST_FAILURE.getValue() + e.getMessage());
}
}
}