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