6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
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 |
|
int w; |
22 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
23 |
|
} |
24 |
|
|
25 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { |
26 |
+ |
return AtomicIntegerFieldUpdater.newUpdater |
27 |
+ |
(AtomicIntegerFieldUpdaterTest.class, fieldName); |
28 |
+ |
} |
29 |
+ |
|
30 |
|
/** |
31 |
|
* Construction with non-existent field throws RuntimeException |
32 |
|
*/ |
33 |
|
public void testConstructor() { |
34 |
|
try { |
35 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
29 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
30 |
< |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
35 |
> |
updaterFor("y"); |
36 |
|
shouldThrow(); |
37 |
< |
} catch (RuntimeException success) {} |
37 |
> |
} catch (RuntimeException success) { |
38 |
> |
assertNotNull(success.getCause()); |
39 |
> |
} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* construction with field not of given type throws RuntimeException |
43 |
> |
* construction with field not of given type throws IllegalArgumentException |
44 |
|
*/ |
45 |
|
public void testConstructor2() { |
46 |
|
try { |
47 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
41 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
42 |
< |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
47 |
> |
updaterFor("z"); |
48 |
|
shouldThrow(); |
49 |
< |
} catch (RuntimeException success) {} |
49 |
> |
} catch (IllegalArgumentException success) {} |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* construction with non-volatile field throws RuntimeException |
53 |
> |
* construction with non-volatile field throws IllegalArgumentException |
54 |
|
*/ |
55 |
|
public void testConstructor3() { |
56 |
|
try { |
57 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
53 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
54 |
< |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
57 |
> |
updaterFor("w"); |
58 |
|
shouldThrow(); |
59 |
< |
} catch (RuntimeException success) {} |
59 |
> |
} catch (IllegalArgumentException success) {} |
60 |
|
} |
61 |
|
|
62 |
|
/** |
64 |
|
*/ |
65 |
|
public void testGetSet() { |
66 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
67 |
< |
try { |
65 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
66 |
< |
} catch (RuntimeException ok) { |
67 |
< |
return; |
68 |
< |
} |
67 |
> |
a = updaterFor("x"); |
68 |
|
x = 1; |
69 |
|
assertEquals(1, a.get(this)); |
70 |
|
a.set(this, 2); |
78 |
|
*/ |
79 |
|
public void testGetLazySet() { |
80 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
81 |
< |
try { |
83 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
84 |
< |
} catch (RuntimeException ok) { |
85 |
< |
return; |
86 |
< |
} |
81 |
> |
a = updaterFor("x"); |
82 |
|
x = 1; |
83 |
|
assertEquals(1, a.get(this)); |
84 |
|
a.lazySet(this, 2); |
92 |
|
*/ |
93 |
|
public void testCompareAndSet() { |
94 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
95 |
< |
try { |
101 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
102 |
< |
} catch (RuntimeException ok) { |
103 |
< |
return; |
104 |
< |
} |
95 |
> |
a = updaterFor("x"); |
96 |
|
x = 1; |
97 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
98 |
|
assertTrue(a.compareAndSet(this, 2, -4)); |
109 |
|
*/ |
110 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
111 |
|
x = 1; |
112 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
113 |
< |
try { |
123 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
124 |
< |
} catch (RuntimeException ok) { |
125 |
< |
return; |
126 |
< |
} |
112 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
113 |
> |
a = updaterFor("x"); |
114 |
|
|
115 |
|
Thread t = new Thread(new CheckedRunnable() { |
116 |
|
public void realRun() { |
122 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
123 |
|
t.join(LONG_DELAY_MS); |
124 |
|
assertFalse(t.isAlive()); |
125 |
< |
assertEquals(a.get(this), 3); |
125 |
> |
assertEquals(3, a.get(this)); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
131 |
|
*/ |
132 |
|
public void testWeakCompareAndSet() { |
133 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
134 |
< |
try { |
148 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
149 |
< |
} catch (RuntimeException ok) { |
150 |
< |
return; |
151 |
< |
} |
134 |
> |
a = updaterFor("x"); |
135 |
|
x = 1; |
136 |
< |
while (!a.weakCompareAndSet(this, 1, 2)); |
137 |
< |
while (!a.weakCompareAndSet(this, 2, -4)); |
136 |
> |
do {} while (!a.weakCompareAndSet(this, 1, 2)); |
137 |
> |
do {} while (!a.weakCompareAndSet(this, 2, -4)); |
138 |
|
assertEquals(-4, a.get(this)); |
139 |
< |
while (!a.weakCompareAndSet(this, -4, 7)); |
139 |
> |
do {} while (!a.weakCompareAndSet(this, -4, 7)); |
140 |
|
assertEquals(7, a.get(this)); |
141 |
|
} |
142 |
|
|
145 |
|
*/ |
146 |
|
public void testGetAndSet() { |
147 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
148 |
< |
try { |
166 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
167 |
< |
} catch (RuntimeException ok) { |
168 |
< |
return; |
169 |
< |
} |
148 |
> |
a = updaterFor("x"); |
149 |
|
x = 1; |
150 |
|
assertEquals(1, a.getAndSet(this, 0)); |
151 |
|
assertEquals(0, a.getAndSet(this, -10)); |
157 |
|
*/ |
158 |
|
public void testGetAndAdd() { |
159 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
160 |
< |
try { |
182 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
183 |
< |
} catch (RuntimeException ok) { |
184 |
< |
return; |
185 |
< |
} |
160 |
> |
a = updaterFor("x"); |
161 |
|
x = 1; |
162 |
|
assertEquals(1, a.getAndAdd(this, 2)); |
163 |
|
assertEquals(3, a.get(this)); |
170 |
|
*/ |
171 |
|
public void testGetAndDecrement() { |
172 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
173 |
< |
try { |
199 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
200 |
< |
} catch (RuntimeException ok) { |
201 |
< |
return; |
202 |
< |
} |
173 |
> |
a = updaterFor("x"); |
174 |
|
x = 1; |
175 |
|
assertEquals(1, a.getAndDecrement(this)); |
176 |
|
assertEquals(0, a.getAndDecrement(this)); |
182 |
|
*/ |
183 |
|
public void testGetAndIncrement() { |
184 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
185 |
< |
try { |
215 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
216 |
< |
} catch (RuntimeException ok) { |
217 |
< |
return; |
218 |
< |
} |
185 |
> |
a = updaterFor("x"); |
186 |
|
x = 1; |
187 |
|
assertEquals(1, a.getAndIncrement(this)); |
188 |
|
assertEquals(2, a.get(this)); |
198 |
|
*/ |
199 |
|
public void testAddAndGet() { |
200 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
201 |
< |
try { |
235 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
236 |
< |
} catch (RuntimeException ok) { |
237 |
< |
return; |
238 |
< |
} |
201 |
> |
a = updaterFor("x"); |
202 |
|
x = 1; |
203 |
|
assertEquals(3, a.addAndGet(this, 2)); |
204 |
|
assertEquals(3, a.get(this)); |
211 |
|
*/ |
212 |
|
public void testDecrementAndGet() { |
213 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
214 |
< |
try { |
252 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
253 |
< |
} catch (RuntimeException ok) { |
254 |
< |
return; |
255 |
< |
} |
214 |
> |
a = updaterFor("x"); |
215 |
|
x = 1; |
216 |
|
assertEquals(0, a.decrementAndGet(this)); |
217 |
|
assertEquals(-1, a.decrementAndGet(this)); |
224 |
|
*/ |
225 |
|
public void testIncrementAndGet() { |
226 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
227 |
< |
try { |
269 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
270 |
< |
} catch (RuntimeException ok) { |
271 |
< |
return; |
272 |
< |
} |
227 |
> |
a = updaterFor("x"); |
228 |
|
x = 1; |
229 |
|
assertEquals(2, a.incrementAndGet(this)); |
230 |
|
assertEquals(2, a.get(this)); |