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