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