1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/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.*; |
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
9 |
> |
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
10 |
|
|
11 |
< |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ |
12 |
< |
volatile Integer x = null; |
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
> |
|
14 |
> |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { |
15 |
> |
volatile Item x = null; |
16 |
> |
protected volatile Item protectedField; |
17 |
> |
private volatile Item privateField; |
18 |
|
Object z; |
19 |
< |
Integer w; |
19 |
> |
Item w; |
20 |
> |
volatile int i; |
21 |
|
|
22 |
< |
public static void main(String[] args){ |
23 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
public static void main(String[] args) { |
23 |
> |
main(suite(), args); |
24 |
|
} |
20 |
– |
|
21 |
– |
|
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
27 |
|
} |
28 |
|
|
29 |
+ |
static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> updaterFor(String fieldName) { |
30 |
+ |
return AtomicReferenceFieldUpdater.newUpdater |
31 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Item.class, fieldName); |
32 |
+ |
} |
33 |
+ |
|
34 |
|
/** |
35 |
< |
* |
35 |
> |
* Construction with non-existent field throws RuntimeException |
36 |
|
*/ |
37 |
< |
public void testConstructor(){ |
38 |
< |
try{ |
39 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
32 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
33 |
< |
(getClass(), Integer.class, "y"); |
37 |
> |
public void testConstructor() { |
38 |
> |
try { |
39 |
> |
updaterFor("y"); |
40 |
|
shouldThrow(); |
41 |
+ |
} catch (RuntimeException success) { |
42 |
+ |
assertNotNull(success.getCause()); |
43 |
|
} |
36 |
– |
catch (RuntimeException rt) {} |
44 |
|
} |
45 |
|
|
46 |
+ |
/** |
47 |
+ |
* construction with field not of given type throws ClassCastException |
48 |
+ |
*/ |
49 |
+ |
public void testConstructor2() { |
50 |
+ |
try { |
51 |
+ |
updaterFor("z"); |
52 |
+ |
shouldThrow(); |
53 |
+ |
} catch (ClassCastException success) {} |
54 |
+ |
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* |
57 |
> |
* Constructor with non-volatile field throws IllegalArgumentException |
58 |
|
*/ |
59 |
< |
public void testConstructor2(){ |
60 |
< |
try{ |
61 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
46 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
47 |
< |
(getClass(), Integer.class, "z"); |
59 |
> |
public void testConstructor3() { |
60 |
> |
try { |
61 |
> |
updaterFor("w"); |
62 |
|
shouldThrow(); |
63 |
< |
} |
50 |
< |
catch (RuntimeException rt) {} |
63 |
> |
} catch (IllegalArgumentException success) {} |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* |
67 |
> |
* Constructor with non-reference field throws ClassCastException |
68 |
|
*/ |
69 |
< |
public void testConstructor3(){ |
70 |
< |
try{ |
71 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
59 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
60 |
< |
(getClass(), Integer.class, "w"); |
69 |
> |
public void testConstructor4() { |
70 |
> |
try { |
71 |
> |
updaterFor("i"); |
72 |
|
shouldThrow(); |
73 |
< |
} |
74 |
< |
catch (RuntimeException rt) {} |
73 |
> |
} catch (ClassCastException success) {} |
74 |
> |
} |
75 |
> |
|
76 |
> |
/** |
77 |
> |
* construction using private field from subclass throws RuntimeException |
78 |
> |
*/ |
79 |
> |
public void testPrivateFieldInSubclass() { |
80 |
> |
new NonNestmates.AtomicReferenceFieldUpdaterTestSubclass() |
81 |
> |
.checkPrivateAccess(); |
82 |
> |
} |
83 |
> |
|
84 |
> |
/** |
85 |
> |
* construction from unrelated class; package access is allowed, |
86 |
> |
* private access is not |
87 |
> |
*/ |
88 |
> |
public void testUnrelatedClassAccess() { |
89 |
> |
new NonNestmates().checkPackageAccess(this); |
90 |
> |
new NonNestmates().checkPrivateAccess(this); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
< |
* |
94 |
> |
* get returns the last value set or assigned |
95 |
|
*/ |
96 |
< |
public void testGetSet(){ |
97 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
96 |
> |
public void testGetSet() { |
97 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a; |
98 |
> |
a = updaterFor("x"); |
99 |
|
x = one; |
100 |
< |
assertEquals(one,a.get(this)); |
101 |
< |
a.set(this,two); |
102 |
< |
assertEquals(two,a.get(this)); |
103 |
< |
a.set(this,-3); |
104 |
< |
assertEquals(-3,a.get(this)); |
77 |
< |
|
100 |
> |
assertSame(one, a.get(this)); |
101 |
> |
a.set(this, two); |
102 |
> |
assertSame(two, a.get(this)); |
103 |
> |
a.set(this, minusThree); |
104 |
> |
assertSame(minusThree, a.get(this)); |
105 |
|
} |
106 |
+ |
|
107 |
|
/** |
108 |
< |
* |
108 |
> |
* get returns the last value lazySet by same thread |
109 |
|
*/ |
110 |
< |
public void testCompareAndSet(){ |
111 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
110 |
> |
public void testGetLazySet() { |
111 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a; |
112 |
> |
a = updaterFor("x"); |
113 |
|
x = one; |
114 |
< |
assertTrue(a.compareAndSet(this,one,two)); |
115 |
< |
assertTrue(a.compareAndSet(this,two,m4)); |
116 |
< |
assertEquals(m4,a.get(this)); |
117 |
< |
assertFalse(a.compareAndSet(this,m5,seven)); |
118 |
< |
assertFalse((seven == a.get(this))); |
119 |
< |
assertTrue(a.compareAndSet(this,m4,seven)); |
120 |
< |
assertEquals(seven,a.get(this)); |
114 |
> |
assertSame(one, a.get(this)); |
115 |
> |
a.lazySet(this, two); |
116 |
> |
assertSame(two, a.get(this)); |
117 |
> |
a.lazySet(this, minusThree); |
118 |
> |
assertSame(minusThree, a.get(this)); |
119 |
> |
} |
120 |
> |
|
121 |
> |
/** |
122 |
> |
* compareAndSet succeeds in changing value if same as expected else fails |
123 |
> |
*/ |
124 |
> |
public void testCompareAndSet() { |
125 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a; |
126 |
> |
a = updaterFor("x"); |
127 |
> |
x = one; |
128 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
129 |
> |
assertTrue(a.compareAndSet(this, two, minusFour)); |
130 |
> |
assertSame(minusFour, a.get(this)); |
131 |
> |
assertFalse(a.compareAndSet(this, minusFive, seven)); |
132 |
> |
assertNotSame(seven, a.get(this)); |
133 |
> |
assertTrue(a.compareAndSet(this, minusFour, seven)); |
134 |
> |
assertSame(seven, a.get(this)); |
135 |
> |
} |
136 |
> |
|
137 |
> |
/** |
138 |
> |
* compareAndSet succeeds in changing protected field value if |
139 |
> |
* same as expected else fails |
140 |
> |
*/ |
141 |
> |
public void testCompareAndSetProtectedInSubclass() { |
142 |
> |
new NonNestmates.AtomicReferenceFieldUpdaterTestSubclass() |
143 |
> |
.checkCompareAndSetProtectedSub(); |
144 |
> |
} |
145 |
> |
|
146 |
> |
/** |
147 |
> |
* compareAndSet in one thread enables another waiting for value |
148 |
> |
* to succeed |
149 |
> |
*/ |
150 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
151 |
> |
x = one; |
152 |
> |
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a; |
153 |
> |
a = updaterFor("x"); |
154 |
> |
|
155 |
> |
Thread t = new Thread(new CheckedRunnable() { |
156 |
> |
public void realRun() { |
157 |
> |
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) |
158 |
> |
Thread.yield(); |
159 |
> |
}}); |
160 |
> |
|
161 |
> |
t.start(); |
162 |
> |
assertTrue(a.compareAndSet(this, one, two)); |
163 |
> |
t.join(LONG_DELAY_MS); |
164 |
> |
assertFalse(t.isAlive()); |
165 |
> |
assertSame(three, a.get(this)); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* |
169 |
> |
* repeated weakCompareAndSet succeeds in changing value when same as expected |
170 |
|
*/ |
171 |
< |
public void testWeakCompareAndSet(){ |
172 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
171 |
> |
public void testWeakCompareAndSet() { |
172 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a; |
173 |
> |
a = updaterFor("x"); |
174 |
|
x = one; |
175 |
< |
while(!a.weakCompareAndSet(this,one,two)); |
176 |
< |
while(!a.weakCompareAndSet(this,two,m4)); |
177 |
< |
assertEquals(m4,a.get(this)); |
178 |
< |
while(!a.weakCompareAndSet(this,m4,seven)); |
179 |
< |
assertEquals(seven,a.get(this)); |
175 |
> |
do {} while (!a.weakCompareAndSet(this, one, two)); |
176 |
> |
do {} while (!a.weakCompareAndSet(this, two, minusFour)); |
177 |
> |
assertSame(minusFour, a.get(this)); |
178 |
> |
do {} while (!a.weakCompareAndSet(this, minusFour, seven)); |
179 |
> |
assertSame(seven, a.get(this)); |
180 |
|
} |
181 |
|
|
182 |
|
/** |
183 |
< |
* |
183 |
> |
* getAndSet returns previous value and sets to given value |
184 |
|
*/ |
185 |
< |
public void testGetAndSet(){ |
186 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
185 |
> |
public void testGetAndSet() { |
186 |
> |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a; |
187 |
> |
a = updaterFor("x"); |
188 |
|
x = one; |
189 |
< |
assertEquals(one,a.getAndSet(this, zero)); |
190 |
< |
assertEquals(zero,a.getAndSet(this,m10)); |
191 |
< |
assertEquals(m10,a.getAndSet(this,1)); |
189 |
> |
assertSame(one, a.getAndSet(this, zero)); |
190 |
> |
assertSame(zero, a.getAndSet(this, minusTen)); |
191 |
> |
assertSame(minusTen, a.getAndSet(this, one)); |
192 |
|
} |
193 |
|
|
194 |
|
} |