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 |
dl |
1.1 |
} |
35 |
|
|
catch (RuntimeException rt) {} |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
|
39 |
dl |
1.3 |
/** |
40 |
dl |
1.4 |
* construction with field not of given type throws RuntimeException |
41 |
dl |
1.3 |
*/ |
42 |
jsr166 |
1.13 |
public void testConstructor2() { |
43 |
jsr166 |
1.12 |
try { |
44 |
dl |
1.1 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
45 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater |
46 |
dl |
1.5 |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
47 |
dl |
1.3 |
shouldThrow(); |
48 |
dl |
1.1 |
} |
49 |
|
|
catch (RuntimeException rt) {} |
50 |
|
|
} |
51 |
|
|
|
52 |
dl |
1.3 |
/** |
53 |
dl |
1.4 |
* Constructor with non-volatile field throws exception |
54 |
dl |
1.3 |
*/ |
55 |
jsr166 |
1.13 |
public void testConstructor3() { |
56 |
jsr166 |
1.12 |
try { |
57 |
dl |
1.1 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
58 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater |
59 |
dl |
1.5 |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
60 |
dl |
1.3 |
shouldThrow(); |
61 |
dl |
1.1 |
} |
62 |
|
|
catch (RuntimeException rt) {} |
63 |
|
|
} |
64 |
|
|
|
65 |
dl |
1.3 |
/** |
66 |
dl |
1.4 |
* get returns the last value set or assigned |
67 |
dl |
1.3 |
*/ |
68 |
jsr166 |
1.13 |
public void testGetSet() { |
69 |
dl |
1.8 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
70 |
|
|
try { |
71 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
72 |
|
|
} catch (RuntimeException ok) { |
73 |
|
|
return; |
74 |
|
|
} |
75 |
dl |
1.1 |
x = one; |
76 |
|
|
assertEquals(one,a.get(this)); |
77 |
|
|
a.set(this,two); |
78 |
|
|
assertEquals(two,a.get(this)); |
79 |
dl |
1.9 |
a.set(this,m3); |
80 |
|
|
assertEquals(m3,a.get(this)); |
81 |
dl |
1.1 |
} |
82 |
dl |
1.10 |
|
83 |
|
|
/** |
84 |
|
|
* get returns the last value lazySet by same thread |
85 |
|
|
*/ |
86 |
jsr166 |
1.13 |
public void testGetLazySet() { |
87 |
dl |
1.10 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
88 |
|
|
try { |
89 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
90 |
|
|
} catch (RuntimeException ok) { |
91 |
|
|
return; |
92 |
|
|
} |
93 |
|
|
x = one; |
94 |
|
|
assertEquals(one,a.get(this)); |
95 |
|
|
a.lazySet(this,two); |
96 |
|
|
assertEquals(two,a.get(this)); |
97 |
|
|
a.lazySet(this,m3); |
98 |
|
|
assertEquals(m3,a.get(this)); |
99 |
|
|
} |
100 |
|
|
|
101 |
dl |
1.3 |
/** |
102 |
dl |
1.4 |
* compareAndSet succeeds in changing value if equal to expected else fails |
103 |
dl |
1.3 |
*/ |
104 |
jsr166 |
1.13 |
public void testCompareAndSet() { |
105 |
dl |
1.8 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
106 |
|
|
try { |
107 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
108 |
|
|
} catch (RuntimeException ok) { |
109 |
|
|
return; |
110 |
|
|
} |
111 |
dl |
1.1 |
x = one; |
112 |
|
|
assertTrue(a.compareAndSet(this,one,two)); |
113 |
|
|
assertTrue(a.compareAndSet(this,two,m4)); |
114 |
|
|
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)); |
119 |
|
|
} |
120 |
|
|
|
121 |
dl |
1.3 |
/** |
122 |
dl |
1.4 |
* compareAndSet in one thread enables another waiting for value |
123 |
|
|
* to succeed |
124 |
|
|
*/ |
125 |
jsr166 |
1.14 |
public void testCompareAndSetInMultipleThreads() throws Exception { |
126 |
dl |
1.4 |
x = one; |
127 |
dl |
1.8 |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
128 |
|
|
try { |
129 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
130 |
|
|
} catch (RuntimeException ok) { |
131 |
|
|
return; |
132 |
|
|
} |
133 |
dl |
1.4 |
|
134 |
|
|
Thread t = new Thread(new Runnable() { |
135 |
|
|
public void run() { |
136 |
jsr166 |
1.12 |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); |
137 |
dl |
1.4 |
}}); |
138 |
jsr166 |
1.14 |
|
139 |
|
|
t.start(); |
140 |
|
|
assertTrue(a.compareAndSet(this, one, two)); |
141 |
|
|
t.join(LONG_DELAY_MS); |
142 |
|
|
assertFalse(t.isAlive()); |
143 |
|
|
assertEquals(a.get(this), three); |
144 |
dl |
1.4 |
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
148 |
jsr166 |
1.11 |
* to expected |
149 |
dl |
1.3 |
*/ |
150 |
jsr166 |
1.13 |
public void testWeakCompareAndSet() { |
151 |
dl |
1.8 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
152 |
|
|
try { |
153 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
154 |
|
|
} catch (RuntimeException ok) { |
155 |
|
|
return; |
156 |
|
|
} |
157 |
dl |
1.1 |
x = one; |
158 |
jsr166 |
1.12 |
while (!a.weakCompareAndSet(this,one,two)); |
159 |
|
|
while (!a.weakCompareAndSet(this,two,m4)); |
160 |
dl |
1.1 |
assertEquals(m4,a.get(this)); |
161 |
jsr166 |
1.12 |
while (!a.weakCompareAndSet(this,m4,seven)); |
162 |
dl |
1.1 |
assertEquals(seven,a.get(this)); |
163 |
|
|
} |
164 |
|
|
|
165 |
dl |
1.3 |
/** |
166 |
dl |
1.4 |
* getAndSet returns previous value and sets to given value |
167 |
dl |
1.3 |
*/ |
168 |
jsr166 |
1.13 |
public void testGetAndSet() { |
169 |
dl |
1.8 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
170 |
|
|
try { |
171 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
172 |
|
|
} catch (RuntimeException ok) { |
173 |
|
|
return; |
174 |
|
|
} |
175 |
dl |
1.1 |
x = one; |
176 |
|
|
assertEquals(one,a.getAndSet(this, zero)); |
177 |
|
|
assertEquals(zero,a.getAndSet(this,m10)); |
178 |
|
|
assertEquals(m10,a.getAndSet(this,1)); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
} |