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