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) {} |
36 |
> |
} catch (RuntimeException success) { |
37 |
> |
assertTrue(success.getCause() != null); |
38 |
> |
} |
39 |
|
} |
40 |
|
|
41 |
|
/** |
43 |
|
*/ |
44 |
|
public void testConstructor2() { |
45 |
|
try { |
46 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
42 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
43 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
46 |
> |
updaterFor("z"); |
47 |
|
shouldThrow(); |
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> |
54 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
55 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
56 |
> |
updaterFor("w"); |
57 |
|
shouldThrow(); |
58 |
< |
} catch (RuntimeException success) {} |
58 |
> |
} catch (IllegalArgumentException success) {} |
59 |
|
} |
60 |
|
|
61 |
|
/** |
63 |
|
*/ |
64 |
|
public void testGetSet() { |
65 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
66 |
< |
try { |
66 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
67 |
< |
} catch (RuntimeException ok) { |
68 |
< |
return; |
69 |
< |
} |
66 |
> |
a = updaterFor("x"); |
67 |
|
x = one; |
68 |
|
assertSame(one, a.get(this)); |
69 |
|
a.set(this, two); |
77 |
|
*/ |
78 |
|
public void testGetLazySet() { |
79 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
80 |
< |
try { |
84 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
85 |
< |
} catch (RuntimeException ok) { |
86 |
< |
return; |
87 |
< |
} |
80 |
> |
a = updaterFor("x"); |
81 |
|
x = one; |
82 |
|
assertSame(one, a.get(this)); |
83 |
|
a.lazySet(this, two); |
91 |
|
*/ |
92 |
|
public void testCompareAndSet() { |
93 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
94 |
< |
try { |
102 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
103 |
< |
} catch (RuntimeException ok) { |
104 |
< |
return; |
105 |
< |
} |
94 |
> |
a = updaterFor("x"); |
95 |
|
x = one; |
96 |
|
assertTrue(a.compareAndSet(this, one, two)); |
97 |
|
assertTrue(a.compareAndSet(this, two, m4)); |
109 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
110 |
|
x = one; |
111 |
|
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
112 |
< |
try { |
124 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
125 |
< |
} catch (RuntimeException ok) { |
126 |
< |
return; |
127 |
< |
} |
112 |
> |
a = updaterFor("x"); |
113 |
|
|
114 |
|
Thread t = new Thread(new CheckedRunnable() { |
115 |
|
public void realRun() { |
121 |
|
assertTrue(a.compareAndSet(this, one, two)); |
122 |
|
t.join(LONG_DELAY_MS); |
123 |
|
assertFalse(t.isAlive()); |
124 |
< |
assertSame(a.get(this), three); |
124 |
> |
assertSame(three, a.get(this)); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
130 |
|
*/ |
131 |
|
public void testWeakCompareAndSet() { |
132 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
133 |
< |
try { |
149 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
150 |
< |
} catch (RuntimeException ok) { |
151 |
< |
return; |
152 |
< |
} |
133 |
> |
a = updaterFor("x"); |
134 |
|
x = one; |
135 |
|
while (!a.weakCompareAndSet(this, one, two)); |
136 |
|
while (!a.weakCompareAndSet(this, two, m4)); |
144 |
|
*/ |
145 |
|
public void testGetAndSet() { |
146 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
147 |
< |
try { |
167 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
168 |
< |
} catch (RuntimeException ok) { |
169 |
< |
return; |
170 |
< |
} |
147 |
> |
a = updaterFor("x"); |
148 |
|
x = one; |
149 |
|
assertSame(one, a.getAndSet(this, zero)); |
150 |
|
assertSame(zero, a.getAndSet(this, m10)); |