6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
10 |
|
|
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()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
37 |
|
updaterFor("y"); |
38 |
|
shouldThrow(); |
39 |
|
} catch (RuntimeException success) { |
40 |
< |
assertTrue(success.getCause() != null); |
40 |
> |
assertNotNull(success.getCause()); |
41 |
|
} |
42 |
|
} |
43 |
|
|
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 |
|
updaterFor("z"); |
50 |
|
shouldThrow(); |
51 |
< |
} catch (RuntimeException success) {} |
51 |
> |
} catch (ClassCastException success) {} |
52 |
|
} |
53 |
|
|
54 |
|
/** |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
+ |
* Constructor with non-reference field throws ClassCastException |
66 |
+ |
*/ |
67 |
+ |
public void testConstructor4() { |
68 |
+ |
try { |
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; |
78 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
79 |
|
a = updaterFor("x"); |
80 |
|
x = one; |
81 |
|
assertSame(one, a.get(this)); |
89 |
|
* get returns the last value lazySet by same thread |
90 |
|
*/ |
91 |
|
public void testGetLazySet() { |
92 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
92 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
93 |
|
a = updaterFor("x"); |
94 |
|
x = one; |
95 |
|
assertSame(one, a.get(this)); |
103 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
104 |
|
*/ |
105 |
|
public void testCompareAndSet() { |
106 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
106 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
107 |
|
a = updaterFor("x"); |
108 |
|
x = one; |
109 |
|
assertTrue(a.compareAndSet(this, one, two)); |
121 |
|
*/ |
122 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
123 |
|
x = one; |
124 |
< |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
124 |
> |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
125 |
|
a = updaterFor("x"); |
126 |
|
|
127 |
|
Thread t = new Thread(new CheckedRunnable() { |
142 |
|
* to expected |
143 |
|
*/ |
144 |
|
public void testWeakCompareAndSet() { |
145 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
145 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
146 |
|
a = updaterFor("x"); |
147 |
|
x = one; |
148 |
< |
while (!a.weakCompareAndSet(this, one, two)); |
149 |
< |
while (!a.weakCompareAndSet(this, two, m4)); |
148 |
> |
do {} while (!a.weakCompareAndSet(this, one, two)); |
149 |
> |
do {} while (!a.weakCompareAndSet(this, two, m4)); |
150 |
|
assertSame(m4, a.get(this)); |
151 |
< |
while (!a.weakCompareAndSet(this, m4, seven)); |
151 |
> |
do {} while (!a.weakCompareAndSet(this, m4, seven)); |
152 |
|
assertSame(seven, a.get(this)); |
153 |
|
} |
154 |
|
|
156 |
|
* getAndSet returns previous value and sets to given value |
157 |
|
*/ |
158 |
|
public void testGetAndSet() { |
159 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
159 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; |
160 |
|
a = updaterFor("x"); |
161 |
|
x = one; |
162 |
|
assertSame(one, a.getAndSet(this, zero)); |