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 |
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.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
11 |
|
|
12 |
|
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile int x = 0; |
20 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { |
24 |
+ |
return AtomicIntegerFieldUpdater.newUpdater |
25 |
+ |
(AtomicIntegerFieldUpdaterTest.class, fieldName); |
26 |
+ |
} |
27 |
+ |
|
28 |
|
/** |
29 |
|
* Construction with non-existent field throws RuntimeException |
30 |
|
*/ |
31 |
|
public void testConstructor() { |
32 |
|
try { |
33 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
30 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
31 |
< |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
33 |
> |
updaterFor("y"); |
34 |
|
shouldThrow(); |
35 |
+ |
} catch (RuntimeException success) { |
36 |
+ |
assertTrue(success.getCause() != null); |
37 |
|
} |
34 |
– |
catch (RuntimeException rt) {} |
38 |
|
} |
39 |
|
|
40 |
|
/** |
41 |
< |
* construction with field not of given type throws RuntimeException |
41 |
> |
* construction with field not of given type throws IllegalArgumentException |
42 |
|
*/ |
43 |
|
public void testConstructor2() { |
44 |
|
try { |
45 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
43 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
44 |
< |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
45 |
> |
updaterFor("z"); |
46 |
|
shouldThrow(); |
47 |
< |
} |
47 |
< |
catch (RuntimeException rt) {} |
47 |
> |
} catch (IllegalArgumentException success) {} |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* construction with non-volatile field throws RuntimeException |
51 |
> |
* construction with non-volatile field throws IllegalArgumentException |
52 |
|
*/ |
53 |
|
public void testConstructor3() { |
54 |
|
try { |
55 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
56 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
57 |
< |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
55 |
> |
updaterFor("w"); |
56 |
|
shouldThrow(); |
57 |
< |
} |
60 |
< |
catch (RuntimeException rt) {} |
57 |
> |
} catch (IllegalArgumentException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* get returns the last value set or assigned |
61 |
> |
* get returns the last value set or assigned |
62 |
|
*/ |
63 |
|
public void testGetSet() { |
64 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
65 |
< |
try { |
69 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
70 |
< |
} catch (RuntimeException ok) { |
71 |
< |
return; |
72 |
< |
} |
65 |
> |
a = updaterFor("x"); |
66 |
|
x = 1; |
67 |
< |
assertEquals(1,a.get(this)); |
68 |
< |
a.set(this,2); |
69 |
< |
assertEquals(2,a.get(this)); |
70 |
< |
a.set(this,-3); |
71 |
< |
assertEquals(-3,a.get(this)); |
79 |
< |
|
67 |
> |
assertEquals(1, a.get(this)); |
68 |
> |
a.set(this, 2); |
69 |
> |
assertEquals(2, a.get(this)); |
70 |
> |
a.set(this, -3); |
71 |
> |
assertEquals(-3, a.get(this)); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* get returns the last value lazySet by same thread |
75 |
> |
* get returns the last value lazySet by same thread |
76 |
|
*/ |
77 |
|
public void testGetLazySet() { |
78 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
79 |
< |
try { |
88 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
89 |
< |
} catch (RuntimeException ok) { |
90 |
< |
return; |
91 |
< |
} |
79 |
> |
a = updaterFor("x"); |
80 |
|
x = 1; |
81 |
< |
assertEquals(1,a.get(this)); |
82 |
< |
a.lazySet(this,2); |
83 |
< |
assertEquals(2,a.get(this)); |
84 |
< |
a.lazySet(this,-3); |
85 |
< |
assertEquals(-3,a.get(this)); |
98 |
< |
|
81 |
> |
assertEquals(1, a.get(this)); |
82 |
> |
a.lazySet(this, 2); |
83 |
> |
assertEquals(2, a.get(this)); |
84 |
> |
a.lazySet(this, -3); |
85 |
> |
assertEquals(-3, a.get(this)); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
90 |
|
*/ |
91 |
|
public void testCompareAndSet() { |
92 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
93 |
< |
try { |
107 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
108 |
< |
} catch (RuntimeException ok) { |
109 |
< |
return; |
110 |
< |
} |
93 |
> |
a = updaterFor("x"); |
94 |
|
x = 1; |
95 |
< |
assertTrue(a.compareAndSet(this,1,2)); |
96 |
< |
assertTrue(a.compareAndSet(this,2,-4)); |
97 |
< |
assertEquals(-4,a.get(this)); |
98 |
< |
assertFalse(a.compareAndSet(this,-5,7)); |
99 |
< |
assertFalse((7 == a.get(this))); |
100 |
< |
assertTrue(a.compareAndSet(this,-4,7)); |
101 |
< |
assertEquals(7,a.get(this)); |
95 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
96 |
> |
assertTrue(a.compareAndSet(this, 2, -4)); |
97 |
> |
assertEquals(-4, a.get(this)); |
98 |
> |
assertFalse(a.compareAndSet(this, -5, 7)); |
99 |
> |
assertEquals(-4, a.get(this)); |
100 |
> |
assertTrue(a.compareAndSet(this, -4, 7)); |
101 |
> |
assertEquals(7, a.get(this)); |
102 |
|
} |
103 |
|
|
121 |
– |
|
104 |
|
/** |
105 |
|
* compareAndSet in one thread enables another waiting for value |
106 |
|
* to succeed |
107 |
|
*/ |
108 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
109 |
|
x = 1; |
110 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
111 |
< |
try { |
130 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
131 |
< |
} catch (RuntimeException ok) { |
132 |
< |
return; |
133 |
< |
} |
110 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
111 |
> |
a = updaterFor("x"); |
112 |
|
|
113 |
< |
Thread t = new Thread(new Runnable() { |
114 |
< |
public void run() { |
115 |
< |
while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
116 |
< |
}}); |
113 |
> |
Thread t = new Thread(new CheckedRunnable() { |
114 |
> |
public void realRun() { |
115 |
> |
while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) |
116 |
> |
Thread.yield(); |
117 |
> |
}}); |
118 |
|
|
119 |
|
t.start(); |
120 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
121 |
|
t.join(LONG_DELAY_MS); |
122 |
|
assertFalse(t.isAlive()); |
123 |
< |
assertEquals(a.get(this), 3); |
123 |
> |
assertEquals(3, a.get(this)); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
129 |
|
*/ |
130 |
|
public void testWeakCompareAndSet() { |
131 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
132 |
< |
try { |
154 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
155 |
< |
} catch (RuntimeException ok) { |
156 |
< |
return; |
157 |
< |
} |
132 |
> |
a = updaterFor("x"); |
133 |
|
x = 1; |
134 |
< |
while (!a.weakCompareAndSet(this,1,2)); |
135 |
< |
while (!a.weakCompareAndSet(this,2,-4)); |
136 |
< |
assertEquals(-4,a.get(this)); |
137 |
< |
while (!a.weakCompareAndSet(this,-4,7)); |
138 |
< |
assertEquals(7,a.get(this)); |
134 |
> |
while (!a.weakCompareAndSet(this, 1, 2)); |
135 |
> |
while (!a.weakCompareAndSet(this, 2, -4)); |
136 |
> |
assertEquals(-4, a.get(this)); |
137 |
> |
while (!a.weakCompareAndSet(this, -4, 7)); |
138 |
> |
assertEquals(7, a.get(this)); |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
< |
* getAndSet returns previous value and sets to given value |
142 |
> |
* getAndSet returns previous value and sets to given value |
143 |
|
*/ |
144 |
|
public void testGetAndSet() { |
145 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
146 |
< |
try { |
172 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
173 |
< |
} catch (RuntimeException ok) { |
174 |
< |
return; |
175 |
< |
} |
146 |
> |
a = updaterFor("x"); |
147 |
|
x = 1; |
148 |
< |
assertEquals(1,a.getAndSet(this, 0)); |
149 |
< |
assertEquals(0,a.getAndSet(this,-10)); |
150 |
< |
assertEquals(-10,a.getAndSet(this,1)); |
148 |
> |
assertEquals(1, a.getAndSet(this, 0)); |
149 |
> |
assertEquals(0, a.getAndSet(this, -10)); |
150 |
> |
assertEquals(-10, a.getAndSet(this, 1)); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
155 |
|
*/ |
156 |
|
public void testGetAndAdd() { |
157 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
158 |
< |
try { |
188 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
189 |
< |
} catch (RuntimeException ok) { |
190 |
< |
return; |
191 |
< |
} |
158 |
> |
a = updaterFor("x"); |
159 |
|
x = 1; |
160 |
< |
assertEquals(1,a.getAndAdd(this,2)); |
161 |
< |
assertEquals(3,a.get(this)); |
162 |
< |
assertEquals(3,a.getAndAdd(this,-4)); |
163 |
< |
assertEquals(-1,a.get(this)); |
160 |
> |
assertEquals(1, a.getAndAdd(this, 2)); |
161 |
> |
assertEquals(3, a.get(this)); |
162 |
> |
assertEquals(3, a.getAndAdd(this, -4)); |
163 |
> |
assertEquals(-1, a.get(this)); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
168 |
|
*/ |
169 |
|
public void testGetAndDecrement() { |
170 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
171 |
< |
try { |
205 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
206 |
< |
} catch (RuntimeException ok) { |
207 |
< |
return; |
208 |
< |
} |
171 |
> |
a = updaterFor("x"); |
172 |
|
x = 1; |
173 |
< |
assertEquals(1,a.getAndDecrement(this)); |
174 |
< |
assertEquals(0,a.getAndDecrement(this)); |
175 |
< |
assertEquals(-1,a.getAndDecrement(this)); |
173 |
> |
assertEquals(1, a.getAndDecrement(this)); |
174 |
> |
assertEquals(0, a.getAndDecrement(this)); |
175 |
> |
assertEquals(-1, a.getAndDecrement(this)); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
180 |
|
*/ |
181 |
|
public void testGetAndIncrement() { |
182 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
183 |
< |
try { |
221 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
222 |
< |
} catch (RuntimeException ok) { |
223 |
< |
return; |
224 |
< |
} |
183 |
> |
a = updaterFor("x"); |
184 |
|
x = 1; |
185 |
< |
assertEquals(1,a.getAndIncrement(this)); |
186 |
< |
assertEquals(2,a.get(this)); |
187 |
< |
a.set(this,-2); |
188 |
< |
assertEquals(-2,a.getAndIncrement(this)); |
189 |
< |
assertEquals(-1,a.getAndIncrement(this)); |
190 |
< |
assertEquals(0,a.getAndIncrement(this)); |
191 |
< |
assertEquals(1,a.get(this)); |
185 |
> |
assertEquals(1, a.getAndIncrement(this)); |
186 |
> |
assertEquals(2, a.get(this)); |
187 |
> |
a.set(this, -2); |
188 |
> |
assertEquals(-2, a.getAndIncrement(this)); |
189 |
> |
assertEquals(-1, a.getAndIncrement(this)); |
190 |
> |
assertEquals(0, a.getAndIncrement(this)); |
191 |
> |
assertEquals(1, a.get(this)); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
196 |
|
*/ |
197 |
|
public void testAddAndGet() { |
198 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
199 |
< |
try { |
241 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
242 |
< |
} catch (RuntimeException ok) { |
243 |
< |
return; |
244 |
< |
} |
199 |
> |
a = updaterFor("x"); |
200 |
|
x = 1; |
201 |
< |
assertEquals(3,a.addAndGet(this,2)); |
202 |
< |
assertEquals(3,a.get(this)); |
203 |
< |
assertEquals(-1,a.addAndGet(this,-4)); |
204 |
< |
assertEquals(-1,a.get(this)); |
201 |
> |
assertEquals(3, a.addAndGet(this, 2)); |
202 |
> |
assertEquals(3, a.get(this)); |
203 |
> |
assertEquals(-1, a.addAndGet(this, -4)); |
204 |
> |
assertEquals(-1, a.get(this)); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
209 |
|
*/ |
210 |
|
public void testDecrementAndGet() { |
211 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
212 |
< |
try { |
258 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
259 |
< |
} catch (RuntimeException ok) { |
260 |
< |
return; |
261 |
< |
} |
212 |
> |
a = updaterFor("x"); |
213 |
|
x = 1; |
214 |
< |
assertEquals(0,a.decrementAndGet(this)); |
215 |
< |
assertEquals(-1,a.decrementAndGet(this)); |
216 |
< |
assertEquals(-2,a.decrementAndGet(this)); |
217 |
< |
assertEquals(-2,a.get(this)); |
214 |
> |
assertEquals(0, a.decrementAndGet(this)); |
215 |
> |
assertEquals(-1, a.decrementAndGet(this)); |
216 |
> |
assertEquals(-2, a.decrementAndGet(this)); |
217 |
> |
assertEquals(-2, a.get(this)); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
222 |
|
*/ |
223 |
|
public void testIncrementAndGet() { |
224 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
225 |
< |
try { |
275 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
276 |
< |
} catch (RuntimeException ok) { |
277 |
< |
return; |
278 |
< |
} |
225 |
> |
a = updaterFor("x"); |
226 |
|
x = 1; |
227 |
< |
assertEquals(2,a.incrementAndGet(this)); |
228 |
< |
assertEquals(2,a.get(this)); |
229 |
< |
a.set(this,-2); |
230 |
< |
assertEquals(-1,a.incrementAndGet(this)); |
231 |
< |
assertEquals(0,a.incrementAndGet(this)); |
232 |
< |
assertEquals(1,a.incrementAndGet(this)); |
233 |
< |
assertEquals(1,a.get(this)); |
227 |
> |
assertEquals(2, a.incrementAndGet(this)); |
228 |
> |
assertEquals(2, a.get(this)); |
229 |
> |
a.set(this, -2); |
230 |
> |
assertEquals(-1, a.incrementAndGet(this)); |
231 |
> |
assertEquals(0, a.incrementAndGet(this)); |
232 |
> |
assertEquals(1, a.incrementAndGet(this)); |
233 |
> |
assertEquals(1, a.get(this)); |
234 |
|
} |
235 |
|
|
236 |
|
} |