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.AtomicReferenceFieldUpdater; |
10 |
|
|
11 |
< |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ |
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
> |
|
14 |
> |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { |
15 |
|
volatile Integer x = null; |
16 |
+ |
protected volatile Integer protectedField; |
17 |
+ |
private volatile Integer privateField; |
18 |
|
Object z; |
19 |
|
Integer w; |
20 |
+ |
volatile int i; |
21 |
|
|
22 |
< |
public static void main(String[] args){ |
23 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
public static void main(String[] args) { |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
27 |
|
} |
28 |
|
|
29 |
+ |
// for testing subclass access |
30 |
+ |
static class AtomicReferenceFieldUpdaterTestSubclass extends AtomicReferenceFieldUpdaterTest { |
31 |
+ |
public void checkPrivateAccess() { |
32 |
+ |
try { |
33 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = |
34 |
+ |
AtomicReferenceFieldUpdater.newUpdater |
35 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField"); |
36 |
+ |
shouldThrow(); |
37 |
+ |
} catch (RuntimeException success) { |
38 |
+ |
assertNotNull(success.getCause()); |
39 |
+ |
} |
40 |
+ |
} |
41 |
+ |
|
42 |
+ |
public void checkCompareAndSetProtectedSub() { |
43 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = |
44 |
+ |
AtomicReferenceFieldUpdater.newUpdater |
45 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField"); |
46 |
+ |
this.protectedField = one; |
47 |
+ |
assertTrue(a.compareAndSet(this, one, two)); |
48 |
+ |
assertTrue(a.compareAndSet(this, two, m4)); |
49 |
+ |
assertSame(m4, a.get(this)); |
50 |
+ |
assertFalse(a.compareAndSet(this, m5, seven)); |
51 |
+ |
assertNotSame(seven, a.get(this)); |
52 |
+ |
assertTrue(a.compareAndSet(this, m4, seven)); |
53 |
+ |
assertSame(seven, a.get(this)); |
54 |
+ |
} |
55 |
+ |
} |
56 |
+ |
|
57 |
+ |
static class UnrelatedClass { |
58 |
+ |
public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) { |
59 |
+ |
obj.x = one; |
60 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = |
61 |
+ |
AtomicReferenceFieldUpdater.newUpdater |
62 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
63 |
+ |
assertSame(one, a.get(obj)); |
64 |
+ |
assertTrue(a.compareAndSet(obj, one, two)); |
65 |
+ |
assertSame(two, a.get(obj)); |
66 |
+ |
} |
67 |
+ |
|
68 |
+ |
public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) { |
69 |
+ |
try { |
70 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = |
71 |
+ |
AtomicReferenceFieldUpdater.newUpdater |
72 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField"); |
73 |
+ |
throw new AssertionError("should throw"); |
74 |
+ |
} catch (RuntimeException success) { |
75 |
+ |
assertNotNull(success.getCause()); |
76 |
+ |
} |
77 |
+ |
} |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) { |
81 |
+ |
return AtomicReferenceFieldUpdater.newUpdater |
82 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); |
83 |
+ |
} |
84 |
+ |
|
85 |
|
/** |
86 |
< |
* Contruction with non-existent field throws RuntimeException |
86 |
> |
* Construction with non-existent field throws RuntimeException |
87 |
|
*/ |
88 |
< |
public void testConstructor(){ |
89 |
< |
try{ |
90 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
31 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
32 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
88 |
> |
public void testConstructor() { |
89 |
> |
try { |
90 |
> |
updaterFor("y"); |
91 |
|
shouldThrow(); |
92 |
+ |
} catch (RuntimeException success) { |
93 |
+ |
assertNotNull(success.getCause()); |
94 |
|
} |
35 |
– |
catch (RuntimeException rt) {} |
95 |
|
} |
96 |
|
|
97 |
+ |
/** |
98 |
+ |
* construction with field not of given type throws ClassCastException |
99 |
+ |
*/ |
100 |
+ |
public void testConstructor2() { |
101 |
+ |
try { |
102 |
+ |
updaterFor("z"); |
103 |
+ |
shouldThrow(); |
104 |
+ |
} catch (ClassCastException success) {} |
105 |
+ |
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* construction with field not of given type throws RuntimeException |
108 |
> |
* Constructor with non-volatile field throws IllegalArgumentException |
109 |
|
*/ |
110 |
< |
public void testConstructor2(){ |
111 |
< |
try{ |
112 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
45 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
46 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
110 |
> |
public void testConstructor3() { |
111 |
> |
try { |
112 |
> |
updaterFor("w"); |
113 |
|
shouldThrow(); |
114 |
< |
} |
49 |
< |
catch (RuntimeException rt) {} |
114 |
> |
} catch (IllegalArgumentException success) {} |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* Constructor with non-volatile field throws exception |
118 |
> |
* Constructor with non-reference field throws ClassCastException |
119 |
|
*/ |
120 |
< |
public void testConstructor3(){ |
121 |
< |
try{ |
122 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
58 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
59 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
120 |
> |
public void testConstructor4() { |
121 |
> |
try { |
122 |
> |
updaterFor("i"); |
123 |
|
shouldThrow(); |
124 |
< |
} |
125 |
< |
catch (RuntimeException rt) {} |
124 |
> |
} catch (ClassCastException success) {} |
125 |
> |
} |
126 |
> |
|
127 |
> |
/** |
128 |
> |
* construction using private field from subclass throws RuntimeException |
129 |
> |
*/ |
130 |
> |
public void testPrivateFieldInSubclass() { |
131 |
> |
AtomicReferenceFieldUpdaterTestSubclass s = |
132 |
> |
new AtomicReferenceFieldUpdaterTestSubclass(); |
133 |
> |
s.checkPrivateAccess(); |
134 |
> |
} |
135 |
> |
|
136 |
> |
/** |
137 |
> |
* construction from unrelated class; package access is allowed, |
138 |
> |
* private access is not |
139 |
> |
*/ |
140 |
> |
public void testUnrelatedClassAccess() { |
141 |
> |
new UnrelatedClass().checkPackageAccess(this); |
142 |
> |
new UnrelatedClass().checkPrivateAccess(this); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* get returns the last value set or assigned |
146 |
> |
* get returns the last value set or assigned |
147 |
|
*/ |
148 |
< |
public void testGetSet(){ |
149 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
148 |
> |
public void testGetSet() { |
149 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
150 |
> |
a = updaterFor("x"); |
151 |
|
x = one; |
152 |
< |
assertEquals(one,a.get(this)); |
153 |
< |
a.set(this,two); |
154 |
< |
assertEquals(two,a.get(this)); |
155 |
< |
a.set(this,-3); |
156 |
< |
assertEquals(-3,a.get(this)); |
76 |
< |
|
152 |
> |
assertSame(one, a.get(this)); |
153 |
> |
a.set(this, two); |
154 |
> |
assertSame(two, a.get(this)); |
155 |
> |
a.set(this, m3); |
156 |
> |
assertSame(m3, a.get(this)); |
157 |
|
} |
158 |
+ |
|
159 |
+ |
/** |
160 |
+ |
* get returns the last value lazySet by same thread |
161 |
+ |
*/ |
162 |
+ |
public void testGetLazySet() { |
163 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
164 |
+ |
a = updaterFor("x"); |
165 |
+ |
x = one; |
166 |
+ |
assertSame(one, a.get(this)); |
167 |
+ |
a.lazySet(this, two); |
168 |
+ |
assertSame(two, a.get(this)); |
169 |
+ |
a.lazySet(this, m3); |
170 |
+ |
assertSame(m3, a.get(this)); |
171 |
+ |
} |
172 |
+ |
|
173 |
|
/** |
174 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
175 |
|
*/ |
176 |
< |
public void testCompareAndSet(){ |
177 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
176 |
> |
public void testCompareAndSet() { |
177 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
178 |
> |
a = updaterFor("x"); |
179 |
|
x = one; |
180 |
< |
assertTrue(a.compareAndSet(this,one,two)); |
181 |
< |
assertTrue(a.compareAndSet(this,two,m4)); |
182 |
< |
assertEquals(m4,a.get(this)); |
183 |
< |
assertFalse(a.compareAndSet(this,m5,seven)); |
184 |
< |
assertFalse((seven == a.get(this))); |
185 |
< |
assertTrue(a.compareAndSet(this,m4,seven)); |
186 |
< |
assertEquals(seven,a.get(this)); |
180 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
181 |
> |
assertTrue(a.compareAndSet(this, two, m4)); |
182 |
> |
assertSame(m4, a.get(this)); |
183 |
> |
assertFalse(a.compareAndSet(this, m5, seven)); |
184 |
> |
assertNotSame(seven, a.get(this)); |
185 |
> |
assertTrue(a.compareAndSet(this, m4, seven)); |
186 |
> |
assertSame(seven, a.get(this)); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
|
* compareAndSet in one thread enables another waiting for value |
191 |
|
* to succeed |
192 |
|
*/ |
193 |
< |
public void testCompareAndSetInMultipleThreads() { |
193 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
194 |
|
x = one; |
195 |
< |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
195 |
> |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
196 |
> |
a = updaterFor("x"); |
197 |
|
|
198 |
< |
Thread t = new Thread(new Runnable() { |
199 |
< |
public void run() { |
200 |
< |
while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); |
201 |
< |
}}); |
202 |
< |
try { |
203 |
< |
t.start(); |
204 |
< |
assertTrue(a.compareAndSet(this, one, two)); |
205 |
< |
t.join(LONG_DELAY_MS); |
206 |
< |
assertFalse(t.isAlive()); |
207 |
< |
assertEquals(a.get(this), three); |
208 |
< |
} |
112 |
< |
catch(Exception e) { |
113 |
< |
unexpectedException(); |
114 |
< |
} |
198 |
> |
Thread t = new Thread(new CheckedRunnable() { |
199 |
> |
public void realRun() { |
200 |
> |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) |
201 |
> |
Thread.yield(); |
202 |
> |
}}); |
203 |
> |
|
204 |
> |
t.start(); |
205 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
206 |
> |
t.join(LONG_DELAY_MS); |
207 |
> |
assertFalse(t.isAlive()); |
208 |
> |
assertSame(three, a.get(this)); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
213 |
< |
* to expected |
213 |
> |
* to expected |
214 |
|
*/ |
215 |
< |
public void testWeakCompareAndSet(){ |
216 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
215 |
> |
public void testWeakCompareAndSet() { |
216 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
217 |
> |
a = updaterFor("x"); |
218 |
|
x = one; |
219 |
< |
while(!a.weakCompareAndSet(this,one,two)); |
220 |
< |
while(!a.weakCompareAndSet(this,two,m4)); |
221 |
< |
assertEquals(m4,a.get(this)); |
222 |
< |
while(!a.weakCompareAndSet(this,m4,seven)); |
223 |
< |
assertEquals(seven,a.get(this)); |
219 |
> |
do {} while (!a.weakCompareAndSet(this, one, two)); |
220 |
> |
do {} while (!a.weakCompareAndSet(this, two, m4)); |
221 |
> |
assertSame(m4, a.get(this)); |
222 |
> |
do {} while (!a.weakCompareAndSet(this, m4, seven)); |
223 |
> |
assertSame(seven, a.get(this)); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
|
* getAndSet returns previous value and sets to given value |
228 |
|
*/ |
229 |
< |
public void testGetAndSet(){ |
230 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
229 |
> |
public void testGetAndSet() { |
230 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
231 |
> |
a = updaterFor("x"); |
232 |
|
x = one; |
233 |
< |
assertEquals(one,a.getAndSet(this, zero)); |
234 |
< |
assertEquals(zero,a.getAndSet(this,m10)); |
235 |
< |
assertEquals(m10,a.getAndSet(this,1)); |
233 |
> |
assertSame(one, a.getAndSet(this, zero)); |
234 |
> |
assertSame(zero, a.getAndSet(this, m10)); |
235 |
> |
assertSame(m10, a.getAndSet(this, 1)); |
236 |
|
} |
237 |
|
|
238 |
|
} |