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 |
|
* Construction with non-existent field throws RuntimeException |
31 |
|
*/ |
32 |
|
public void testConstructor() { |
33 |
|
try { |
34 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
30 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
31 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
34 |
> |
updaterFor("y"); |
35 |
|
shouldThrow(); |
36 |
|
} catch (RuntimeException success) {} |
37 |
|
} |
41 |
|
*/ |
42 |
|
public void testConstructor2() { |
43 |
|
try { |
44 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
42 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
43 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
44 |
> |
updaterFor("z"); |
45 |
|
shouldThrow(); |
46 |
|
} catch (RuntimeException success) {} |
47 |
|
} |
51 |
|
*/ |
52 |
|
public void testConstructor3() { |
53 |
|
try { |
54 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
54 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
55 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
54 |
> |
updaterFor("w"); |
55 |
|
shouldThrow(); |
56 |
|
} catch (RuntimeException success) {} |
57 |
|
} |
61 |
|
*/ |
62 |
|
public void testGetSet() { |
63 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
64 |
< |
try { |
66 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
67 |
< |
} catch (RuntimeException ok) { |
68 |
< |
return; |
69 |
< |
} |
64 |
> |
a = updaterFor("x"); |
65 |
|
x = one; |
66 |
|
assertSame(one, a.get(this)); |
67 |
|
a.set(this, two); |
75 |
|
*/ |
76 |
|
public void testGetLazySet() { |
77 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
78 |
< |
try { |
84 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
85 |
< |
} catch (RuntimeException ok) { |
86 |
< |
return; |
87 |
< |
} |
78 |
> |
a = updaterFor("x"); |
79 |
|
x = one; |
80 |
|
assertSame(one, a.get(this)); |
81 |
|
a.lazySet(this, two); |
89 |
|
*/ |
90 |
|
public void testCompareAndSet() { |
91 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
92 |
< |
try { |
102 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
103 |
< |
} catch (RuntimeException ok) { |
104 |
< |
return; |
105 |
< |
} |
92 |
> |
a = updaterFor("x"); |
93 |
|
x = one; |
94 |
|
assertTrue(a.compareAndSet(this, one, two)); |
95 |
|
assertTrue(a.compareAndSet(this, two, m4)); |
107 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
108 |
|
x = one; |
109 |
|
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
110 |
< |
try { |
124 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
125 |
< |
} catch (RuntimeException ok) { |
126 |
< |
return; |
127 |
< |
} |
110 |
> |
a = updaterFor("x"); |
111 |
|
|
112 |
|
Thread t = new Thread(new CheckedRunnable() { |
113 |
|
public void realRun() { |
128 |
|
*/ |
129 |
|
public void testWeakCompareAndSet() { |
130 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
131 |
< |
try { |
149 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
150 |
< |
} catch (RuntimeException ok) { |
151 |
< |
return; |
152 |
< |
} |
131 |
> |
a = updaterFor("x"); |
132 |
|
x = one; |
133 |
|
while (!a.weakCompareAndSet(this, one, two)); |
134 |
|
while (!a.weakCompareAndSet(this, two, m4)); |
142 |
|
*/ |
143 |
|
public void testGetAndSet() { |
144 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
145 |
< |
try { |
167 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
168 |
< |
} catch (RuntimeException ok) { |
169 |
< |
return; |
170 |
< |
} |
145 |
> |
a = updaterFor("x"); |
146 |
|
x = one; |
147 |
|
assertSame(one, a.getAndSet(this, zero)); |
148 |
|
assertSame(zero, a.getAndSet(this, m10)); |