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