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.AtomicReferenceFieldUpdater; |
11 |
|
|
12 |
|
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile Integer x = null; |
14 |
|
Object z; |
15 |
|
Integer w; |
16 |
+ |
volatile int i; |
17 |
|
|
18 |
|
public static void main(String[] args) { |
19 |
|
junit.textui.TestRunner.run(suite()); |
22 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
23 |
|
} |
24 |
|
|
25 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) { |
26 |
+ |
return AtomicReferenceFieldUpdater.newUpdater |
27 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); |
28 |
+ |
} |
29 |
+ |
|
30 |
|
/** |
31 |
|
* Construction with non-existent field throws RuntimeException |
32 |
|
*/ |
33 |
|
public void testConstructor() { |
34 |
|
try { |
35 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
31 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
32 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
35 |
> |
updaterFor("y"); |
36 |
|
shouldThrow(); |
37 |
+ |
} catch (RuntimeException success) { |
38 |
+ |
assertTrue(success.getCause() != null); |
39 |
|
} |
35 |
– |
catch (RuntimeException rt) {} |
40 |
|
} |
41 |
|
|
38 |
– |
|
42 |
|
/** |
43 |
< |
* construction with field not of given type throws RuntimeException |
43 |
> |
* construction with field not of given type throws ClassCastException |
44 |
|
*/ |
45 |
|
public void testConstructor2() { |
46 |
|
try { |
47 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
45 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
46 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
47 |
> |
updaterFor("z"); |
48 |
|
shouldThrow(); |
49 |
< |
} |
49 |
< |
catch (RuntimeException rt) {} |
49 |
> |
} catch (ClassCastException success) {} |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Constructor with non-volatile field throws exception |
53 |
> |
* Constructor with non-volatile field throws IllegalArgumentException |
54 |
|
*/ |
55 |
|
public void testConstructor3() { |
56 |
|
try { |
57 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
58 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
59 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
57 |
> |
updaterFor("w"); |
58 |
|
shouldThrow(); |
59 |
< |
} |
62 |
< |
catch (RuntimeException rt) {} |
59 |
> |
} catch (IllegalArgumentException success) {} |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* get returns the last value set or assigned |
63 |
> |
* Constructor with non-reference field throws ClassCastException |
64 |
> |
*/ |
65 |
> |
public void testConstructor4() { |
66 |
> |
try { |
67 |
> |
updaterFor("i"); |
68 |
> |
shouldThrow(); |
69 |
> |
} catch (ClassCastException success) {} |
70 |
> |
} |
71 |
> |
|
72 |
> |
/** |
73 |
> |
* get returns the last value set or assigned |
74 |
|
*/ |
75 |
|
public void testGetSet() { |
76 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
77 |
< |
try { |
71 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
72 |
< |
} catch (RuntimeException ok) { |
73 |
< |
return; |
74 |
< |
} |
77 |
> |
a = updaterFor("x"); |
78 |
|
x = one; |
79 |
< |
assertEquals(one,a.get(this)); |
80 |
< |
a.set(this,two); |
81 |
< |
assertEquals(two,a.get(this)); |
82 |
< |
a.set(this,m3); |
83 |
< |
assertEquals(m3,a.get(this)); |
79 |
> |
assertSame(one, a.get(this)); |
80 |
> |
a.set(this, two); |
81 |
> |
assertSame(two, a.get(this)); |
82 |
> |
a.set(this, m3); |
83 |
> |
assertSame(m3, a.get(this)); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* get returns the last value lazySet by same thread |
87 |
> |
* get returns the last value lazySet by same thread |
88 |
|
*/ |
89 |
|
public void testGetLazySet() { |
90 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
91 |
< |
try { |
89 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
90 |
< |
} catch (RuntimeException ok) { |
91 |
< |
return; |
92 |
< |
} |
91 |
> |
a = updaterFor("x"); |
92 |
|
x = one; |
93 |
< |
assertEquals(one,a.get(this)); |
94 |
< |
a.lazySet(this,two); |
95 |
< |
assertEquals(two,a.get(this)); |
96 |
< |
a.lazySet(this,m3); |
97 |
< |
assertEquals(m3,a.get(this)); |
93 |
> |
assertSame(one, a.get(this)); |
94 |
> |
a.lazySet(this, two); |
95 |
> |
assertSame(two, a.get(this)); |
96 |
> |
a.lazySet(this, m3); |
97 |
> |
assertSame(m3, a.get(this)); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
102 |
|
*/ |
103 |
|
public void testCompareAndSet() { |
104 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
105 |
< |
try { |
107 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
108 |
< |
} catch (RuntimeException ok) { |
109 |
< |
return; |
110 |
< |
} |
105 |
> |
a = updaterFor("x"); |
106 |
|
x = one; |
107 |
< |
assertTrue(a.compareAndSet(this,one,two)); |
108 |
< |
assertTrue(a.compareAndSet(this,two,m4)); |
109 |
< |
assertEquals(m4,a.get(this)); |
110 |
< |
assertFalse(a.compareAndSet(this,m5,seven)); |
111 |
< |
assertFalse((seven == a.get(this))); |
112 |
< |
assertTrue(a.compareAndSet(this,m4,seven)); |
113 |
< |
assertEquals(seven,a.get(this)); |
107 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
108 |
> |
assertTrue(a.compareAndSet(this, two, m4)); |
109 |
> |
assertSame(m4, a.get(this)); |
110 |
> |
assertFalse(a.compareAndSet(this, m5, seven)); |
111 |
> |
assertFalse(seven == a.get(this)); |
112 |
> |
assertTrue(a.compareAndSet(this, m4, seven)); |
113 |
> |
assertSame(seven, a.get(this)); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
|
* compareAndSet in one thread enables another waiting for value |
118 |
|
* to succeed |
119 |
|
*/ |
120 |
< |
public void testCompareAndSetInMultipleThreads() { |
120 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
121 |
|
x = one; |
122 |
|
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
123 |
< |
try { |
129 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
130 |
< |
} catch (RuntimeException ok) { |
131 |
< |
return; |
132 |
< |
} |
123 |
> |
a = updaterFor("x"); |
124 |
|
|
125 |
< |
Thread t = new Thread(new Runnable() { |
126 |
< |
public void run() { |
127 |
< |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); |
128 |
< |
}}); |
129 |
< |
try { |
130 |
< |
t.start(); |
131 |
< |
assertTrue(a.compareAndSet(this, one, two)); |
132 |
< |
t.join(LONG_DELAY_MS); |
133 |
< |
assertFalse(t.isAlive()); |
134 |
< |
assertEquals(a.get(this), three); |
135 |
< |
} |
145 |
< |
catch (Exception e) { |
146 |
< |
unexpectedException(); |
147 |
< |
} |
125 |
> |
Thread t = new Thread(new CheckedRunnable() { |
126 |
> |
public void realRun() { |
127 |
> |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) |
128 |
> |
Thread.yield(); |
129 |
> |
}}); |
130 |
> |
|
131 |
> |
t.start(); |
132 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
133 |
> |
t.join(LONG_DELAY_MS); |
134 |
> |
assertFalse(t.isAlive()); |
135 |
> |
assertSame(three, a.get(this)); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
141 |
|
*/ |
142 |
|
public void testWeakCompareAndSet() { |
143 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
144 |
< |
try { |
157 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
158 |
< |
} catch (RuntimeException ok) { |
159 |
< |
return; |
160 |
< |
} |
144 |
> |
a = updaterFor("x"); |
145 |
|
x = one; |
146 |
< |
while (!a.weakCompareAndSet(this,one,two)); |
147 |
< |
while (!a.weakCompareAndSet(this,two,m4)); |
148 |
< |
assertEquals(m4,a.get(this)); |
149 |
< |
while (!a.weakCompareAndSet(this,m4,seven)); |
150 |
< |
assertEquals(seven,a.get(this)); |
146 |
> |
while (!a.weakCompareAndSet(this, one, two)); |
147 |
> |
while (!a.weakCompareAndSet(this, two, m4)); |
148 |
> |
assertSame(m4, a.get(this)); |
149 |
> |
while (!a.weakCompareAndSet(this, m4, seven)); |
150 |
> |
assertSame(seven, a.get(this)); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
155 |
|
*/ |
156 |
|
public void testGetAndSet() { |
157 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
158 |
< |
try { |
175 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
176 |
< |
} catch (RuntimeException ok) { |
177 |
< |
return; |
178 |
< |
} |
158 |
> |
a = updaterFor("x"); |
159 |
|
x = one; |
160 |
< |
assertEquals(one,a.getAndSet(this, zero)); |
161 |
< |
assertEquals(zero,a.getAndSet(this,m10)); |
162 |
< |
assertEquals(m10,a.getAndSet(this,1)); |
160 |
> |
assertSame(one, a.getAndSet(this, zero)); |
161 |
> |
assertSame(zero, a.getAndSet(this, m10)); |
162 |
> |
assertSame(m10, a.getAndSet(this, 1)); |
163 |
|
} |
164 |
|
|
165 |
|
} |