1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import java.util.concurrent.atomic.*; |
10 |
< |
import junit.framework.*; |
11 |
< |
import java.util.*; |
9 |
> |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
10 |
> |
|
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
|
|
14 |
|
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
15 |
|
volatile int x = 0; |
16 |
+ |
protected volatile int protectedField; |
17 |
+ |
private volatile int privateField; |
18 |
|
int w; |
19 |
< |
long z; |
20 |
< |
public static void main(String[] args){ |
21 |
< |
junit.textui.TestRunner.run(suite()); |
19 |
> |
float z; |
20 |
> |
public static void main(String[] args) { |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
25 |
|
} |
26 |
|
|
27 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { |
28 |
+ |
return AtomicIntegerFieldUpdater.newUpdater |
29 |
+ |
(AtomicIntegerFieldUpdaterTest.class, fieldName); |
30 |
+ |
} |
31 |
+ |
|
32 |
|
/** |
33 |
|
* Construction with non-existent field throws RuntimeException |
34 |
|
*/ |
35 |
|
public void testConstructor() { |
36 |
< |
try{ |
37 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
30 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
31 |
< |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
36 |
> |
try { |
37 |
> |
updaterFor("y"); |
38 |
|
shouldThrow(); |
39 |
+ |
} catch (RuntimeException success) { |
40 |
+ |
assertNotNull(success.getCause()); |
41 |
|
} |
34 |
– |
catch (RuntimeException rt) {} |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* construction with field not of given type throws RuntimeException |
45 |
> |
* construction with field not of given type throws IllegalArgumentException |
46 |
|
*/ |
47 |
|
public void testConstructor2() { |
48 |
< |
try{ |
49 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
43 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
44 |
< |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
48 |
> |
try { |
49 |
> |
updaterFor("z"); |
50 |
|
shouldThrow(); |
51 |
< |
} |
47 |
< |
catch (RuntimeException rt) {} |
51 |
> |
} catch (IllegalArgumentException success) {} |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* construction with non-volatile field throws RuntimeException |
55 |
> |
* construction with non-volatile field throws IllegalArgumentException |
56 |
|
*/ |
57 |
|
public void testConstructor3() { |
58 |
< |
try{ |
59 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
56 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
57 |
< |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
58 |
> |
try { |
59 |
> |
updaterFor("w"); |
60 |
|
shouldThrow(); |
61 |
< |
} |
62 |
< |
catch (RuntimeException rt) {} |
61 |
> |
} catch (IllegalArgumentException success) {} |
62 |
> |
} |
63 |
> |
|
64 |
> |
/** |
65 |
> |
* construction using private field from subclass throws RuntimeException |
66 |
> |
*/ |
67 |
> |
public void testPrivateFieldInSubclass() { |
68 |
> |
new NonNestmates.AtomicIntegerFieldUpdaterTestSubclass() |
69 |
> |
.checkPrivateAccess(); |
70 |
> |
} |
71 |
> |
|
72 |
> |
/** |
73 |
> |
* construction from unrelated class; package access is allowed, |
74 |
> |
* private access is not |
75 |
> |
*/ |
76 |
> |
public void testUnrelatedClassAccess() { |
77 |
> |
new NonNestmates().checkPackageAccess(this); |
78 |
> |
new NonNestmates().checkPrivateAccess(this); |
79 |
|
} |
80 |
|
|
81 |
|
/** |
82 |
< |
* get returns the last value set or assigned |
82 |
> |
* get returns the last value set or assigned |
83 |
|
*/ |
84 |
|
public void testGetSet() { |
85 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
86 |
< |
try { |
69 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
70 |
< |
} catch (RuntimeException ok) { |
71 |
< |
return; |
72 |
< |
} |
86 |
> |
a = updaterFor("x"); |
87 |
|
x = 1; |
88 |
< |
assertEquals(1,a.get(this)); |
89 |
< |
a.set(this,2); |
90 |
< |
assertEquals(2,a.get(this)); |
91 |
< |
a.set(this,-3); |
92 |
< |
assertEquals(-3,a.get(this)); |
79 |
< |
|
88 |
> |
assertEquals(1, a.get(this)); |
89 |
> |
a.set(this, 2); |
90 |
> |
assertEquals(2, a.get(this)); |
91 |
> |
a.set(this, -3); |
92 |
> |
assertEquals(-3, a.get(this)); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* get returns the last value lazySet by same thread |
96 |
> |
* get returns the last value lazySet by same thread |
97 |
|
*/ |
98 |
|
public void testGetLazySet() { |
99 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
100 |
< |
try { |
88 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
89 |
< |
} catch (RuntimeException ok) { |
90 |
< |
return; |
91 |
< |
} |
100 |
> |
a = updaterFor("x"); |
101 |
|
x = 1; |
102 |
< |
assertEquals(1,a.get(this)); |
103 |
< |
a.lazySet(this,2); |
104 |
< |
assertEquals(2,a.get(this)); |
105 |
< |
a.lazySet(this,-3); |
106 |
< |
assertEquals(-3,a.get(this)); |
98 |
< |
|
102 |
> |
assertEquals(1, a.get(this)); |
103 |
> |
a.lazySet(this, 2); |
104 |
> |
assertEquals(2, a.get(this)); |
105 |
> |
a.lazySet(this, -3); |
106 |
> |
assertEquals(-3, a.get(this)); |
107 |
|
} |
108 |
|
|
109 |
|
/** |
111 |
|
*/ |
112 |
|
public void testCompareAndSet() { |
113 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
114 |
< |
try { |
107 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
108 |
< |
} catch (RuntimeException ok) { |
109 |
< |
return; |
110 |
< |
} |
114 |
> |
a = updaterFor("x"); |
115 |
|
x = 1; |
116 |
< |
assertTrue(a.compareAndSet(this,1,2)); |
117 |
< |
assertTrue(a.compareAndSet(this,2,-4)); |
118 |
< |
assertEquals(-4,a.get(this)); |
119 |
< |
assertFalse(a.compareAndSet(this,-5,7)); |
120 |
< |
assertFalse((7 == a.get(this))); |
121 |
< |
assertTrue(a.compareAndSet(this,-4,7)); |
122 |
< |
assertEquals(7,a.get(this)); |
116 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
117 |
> |
assertTrue(a.compareAndSet(this, 2, -4)); |
118 |
> |
assertEquals(-4, a.get(this)); |
119 |
> |
assertFalse(a.compareAndSet(this, -5, 7)); |
120 |
> |
assertEquals(-4, a.get(this)); |
121 |
> |
assertTrue(a.compareAndSet(this, -4, 7)); |
122 |
> |
assertEquals(7, a.get(this)); |
123 |
|
} |
124 |
|
|
125 |
+ |
/** |
126 |
+ |
* compareAndSet succeeds in changing protected field value if |
127 |
+ |
* equal to expected else fails |
128 |
+ |
*/ |
129 |
+ |
public void testCompareAndSetProtected() { |
130 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
131 |
+ |
a = updaterFor("protectedField"); |
132 |
+ |
protectedField = 1; |
133 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
134 |
+ |
assertTrue(a.compareAndSet(this, 2, -4)); |
135 |
+ |
assertEquals(-4, a.get(this)); |
136 |
+ |
assertFalse(a.compareAndSet(this, -5, 7)); |
137 |
+ |
assertEquals(-4, a.get(this)); |
138 |
+ |
assertTrue(a.compareAndSet(this, -4, 7)); |
139 |
+ |
assertEquals(7, a.get(this)); |
140 |
+ |
} |
141 |
+ |
|
142 |
+ |
/** |
143 |
+ |
* compareAndSet succeeds in changing protected field value if |
144 |
+ |
* equal to expected else fails |
145 |
+ |
*/ |
146 |
+ |
public void testCompareAndSetProtectedInSubclass() { |
147 |
+ |
NonNestmates.AtomicIntegerFieldUpdaterTestSubclass s = |
148 |
+ |
new NonNestmates.AtomicIntegerFieldUpdaterTestSubclass(); |
149 |
+ |
s.checkCompareAndSetProtectedSub(); |
150 |
+ |
} |
151 |
|
|
152 |
|
/** |
153 |
|
* compareAndSet in one thread enables another waiting for value |
154 |
|
* to succeed |
155 |
|
*/ |
156 |
< |
public void testCompareAndSetInMultipleThreads() { |
156 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
157 |
|
x = 1; |
158 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
159 |
< |
try { |
130 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
131 |
< |
} catch (RuntimeException ok) { |
132 |
< |
return; |
133 |
< |
} |
158 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
159 |
> |
a = updaterFor("x"); |
160 |
|
|
161 |
< |
Thread t = new Thread(new Runnable() { |
162 |
< |
public void run() { |
163 |
< |
while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
164 |
< |
}}); |
165 |
< |
try { |
166 |
< |
t.start(); |
167 |
< |
assertTrue(a.compareAndSet(this, 1, 2)); |
168 |
< |
t.join(LONG_DELAY_MS); |
169 |
< |
assertFalse(t.isAlive()); |
170 |
< |
assertEquals(a.get(this), 3); |
171 |
< |
} |
146 |
< |
catch(Exception e) { |
147 |
< |
unexpectedException(); |
148 |
< |
} |
161 |
> |
Thread t = new Thread(new CheckedRunnable() { |
162 |
> |
public void realRun() { |
163 |
> |
while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) |
164 |
> |
Thread.yield(); |
165 |
> |
}}); |
166 |
> |
|
167 |
> |
t.start(); |
168 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
169 |
> |
t.join(LONG_DELAY_MS); |
170 |
> |
assertFalse(t.isAlive()); |
171 |
> |
assertEquals(3, a.get(this)); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
176 |
< |
* to expected |
176 |
> |
* to expected |
177 |
|
*/ |
178 |
|
public void testWeakCompareAndSet() { |
179 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
180 |
< |
try { |
158 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
159 |
< |
} catch (RuntimeException ok) { |
160 |
< |
return; |
161 |
< |
} |
180 |
> |
a = updaterFor("x"); |
181 |
|
x = 1; |
182 |
< |
while(!a.weakCompareAndSet(this,1,2)); |
183 |
< |
while(!a.weakCompareAndSet(this,2,-4)); |
184 |
< |
assertEquals(-4,a.get(this)); |
185 |
< |
while(!a.weakCompareAndSet(this,-4,7)); |
186 |
< |
assertEquals(7,a.get(this)); |
182 |
> |
do {} while (!a.weakCompareAndSet(this, 1, 2)); |
183 |
> |
do {} while (!a.weakCompareAndSet(this, 2, -4)); |
184 |
> |
assertEquals(-4, a.get(this)); |
185 |
> |
do {} while (!a.weakCompareAndSet(this, -4, 7)); |
186 |
> |
assertEquals(7, a.get(this)); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* getAndSet returns previous value and sets to given value |
190 |
> |
* getAndSet returns previous value and sets to given value |
191 |
|
*/ |
192 |
|
public void testGetAndSet() { |
193 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
194 |
< |
try { |
176 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
177 |
< |
} catch (RuntimeException ok) { |
178 |
< |
return; |
179 |
< |
} |
194 |
> |
a = updaterFor("x"); |
195 |
|
x = 1; |
196 |
< |
assertEquals(1,a.getAndSet(this, 0)); |
197 |
< |
assertEquals(0,a.getAndSet(this,-10)); |
198 |
< |
assertEquals(-10,a.getAndSet(this,1)); |
196 |
> |
assertEquals(1, a.getAndSet(this, 0)); |
197 |
> |
assertEquals(0, a.getAndSet(this, -10)); |
198 |
> |
assertEquals(-10, a.getAndSet(this, 1)); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
203 |
|
*/ |
204 |
|
public void testGetAndAdd() { |
205 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
206 |
< |
try { |
192 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
193 |
< |
} catch (RuntimeException ok) { |
194 |
< |
return; |
195 |
< |
} |
206 |
> |
a = updaterFor("x"); |
207 |
|
x = 1; |
208 |
< |
assertEquals(1,a.getAndAdd(this,2)); |
209 |
< |
assertEquals(3,a.get(this)); |
210 |
< |
assertEquals(3,a.getAndAdd(this,-4)); |
211 |
< |
assertEquals(-1,a.get(this)); |
208 |
> |
assertEquals(1, a.getAndAdd(this, 2)); |
209 |
> |
assertEquals(3, a.get(this)); |
210 |
> |
assertEquals(3, a.getAndAdd(this, -4)); |
211 |
> |
assertEquals(-1, a.get(this)); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
216 |
|
*/ |
217 |
|
public void testGetAndDecrement() { |
218 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
219 |
< |
try { |
209 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
210 |
< |
} catch (RuntimeException ok) { |
211 |
< |
return; |
212 |
< |
} |
219 |
> |
a = updaterFor("x"); |
220 |
|
x = 1; |
221 |
< |
assertEquals(1,a.getAndDecrement(this)); |
222 |
< |
assertEquals(0,a.getAndDecrement(this)); |
223 |
< |
assertEquals(-1,a.getAndDecrement(this)); |
221 |
> |
assertEquals(1, a.getAndDecrement(this)); |
222 |
> |
assertEquals(0, a.getAndDecrement(this)); |
223 |
> |
assertEquals(-1, a.getAndDecrement(this)); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
228 |
|
*/ |
229 |
|
public void testGetAndIncrement() { |
230 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
231 |
< |
try { |
225 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
226 |
< |
} catch (RuntimeException ok) { |
227 |
< |
return; |
228 |
< |
} |
231 |
> |
a = updaterFor("x"); |
232 |
|
x = 1; |
233 |
< |
assertEquals(1,a.getAndIncrement(this)); |
234 |
< |
assertEquals(2,a.get(this)); |
235 |
< |
a.set(this,-2); |
236 |
< |
assertEquals(-2,a.getAndIncrement(this)); |
237 |
< |
assertEquals(-1,a.getAndIncrement(this)); |
238 |
< |
assertEquals(0,a.getAndIncrement(this)); |
239 |
< |
assertEquals(1,a.get(this)); |
233 |
> |
assertEquals(1, a.getAndIncrement(this)); |
234 |
> |
assertEquals(2, a.get(this)); |
235 |
> |
a.set(this, -2); |
236 |
> |
assertEquals(-2, a.getAndIncrement(this)); |
237 |
> |
assertEquals(-1, a.getAndIncrement(this)); |
238 |
> |
assertEquals(0, a.getAndIncrement(this)); |
239 |
> |
assertEquals(1, a.get(this)); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
244 |
|
*/ |
245 |
|
public void testAddAndGet() { |
246 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
247 |
< |
try { |
245 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
246 |
< |
} catch (RuntimeException ok) { |
247 |
< |
return; |
248 |
< |
} |
247 |
> |
a = updaterFor("x"); |
248 |
|
x = 1; |
249 |
< |
assertEquals(3,a.addAndGet(this,2)); |
250 |
< |
assertEquals(3,a.get(this)); |
251 |
< |
assertEquals(-1,a.addAndGet(this,-4)); |
252 |
< |
assertEquals(-1,a.get(this)); |
249 |
> |
assertEquals(3, a.addAndGet(this, 2)); |
250 |
> |
assertEquals(3, a.get(this)); |
251 |
> |
assertEquals(-1, a.addAndGet(this, -4)); |
252 |
> |
assertEquals(-1, a.get(this)); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
257 |
|
*/ |
258 |
|
public void testDecrementAndGet() { |
259 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
260 |
< |
try { |
262 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
263 |
< |
} catch (RuntimeException ok) { |
264 |
< |
return; |
265 |
< |
} |
260 |
> |
a = updaterFor("x"); |
261 |
|
x = 1; |
262 |
< |
assertEquals(0,a.decrementAndGet(this)); |
263 |
< |
assertEquals(-1,a.decrementAndGet(this)); |
264 |
< |
assertEquals(-2,a.decrementAndGet(this)); |
265 |
< |
assertEquals(-2,a.get(this)); |
262 |
> |
assertEquals(0, a.decrementAndGet(this)); |
263 |
> |
assertEquals(-1, a.decrementAndGet(this)); |
264 |
> |
assertEquals(-2, a.decrementAndGet(this)); |
265 |
> |
assertEquals(-2, a.get(this)); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
270 |
|
*/ |
271 |
|
public void testIncrementAndGet() { |
272 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
273 |
< |
try { |
279 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
280 |
< |
} catch (RuntimeException ok) { |
281 |
< |
return; |
282 |
< |
} |
273 |
> |
a = updaterFor("x"); |
274 |
|
x = 1; |
275 |
< |
assertEquals(2,a.incrementAndGet(this)); |
276 |
< |
assertEquals(2,a.get(this)); |
277 |
< |
a.set(this,-2); |
278 |
< |
assertEquals(-1,a.incrementAndGet(this)); |
279 |
< |
assertEquals(0,a.incrementAndGet(this)); |
280 |
< |
assertEquals(1,a.incrementAndGet(this)); |
281 |
< |
assertEquals(1,a.get(this)); |
275 |
> |
assertEquals(2, a.incrementAndGet(this)); |
276 |
> |
assertEquals(2, a.get(this)); |
277 |
> |
a.set(this, -2); |
278 |
> |
assertEquals(-1, a.incrementAndGet(this)); |
279 |
> |
assertEquals(0, a.incrementAndGet(this)); |
280 |
> |
assertEquals(1, a.incrementAndGet(this)); |
281 |
> |
assertEquals(1, a.get(this)); |
282 |
|
} |
283 |
|
|
284 |
|
} |