ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.35
Committed: Sat Mar 18 20:42:20 2017 UTC (7 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.34: +2 -2 lines
Log Message:
better assertion style

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.6 * 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 jsr166 1.21 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.11 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.23 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
10 dl 1.1
11 jsr166 1.30 import junit.framework.Test;
12     import junit.framework.TestSuite;
13    
14 jsr166 1.13 public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
15 dl 1.1 volatile Integer x = null;
16 jsr166 1.34 protected volatile Integer protectedField;
17     private volatile Integer privateField;
18 dl 1.1 Object z;
19     Integer w;
20 jsr166 1.28 volatile int i;
21 dl 1.1
22 jsr166 1.13 public static void main(String[] args) {
23 jsr166 1.33 main(suite(), args);
24 dl 1.1 }
25     public static Test suite() {
26     return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
27     }
28    
29 jsr166 1.34 // for testing subclass access
30     static class AtomicReferenceFieldUpdaterTestSubclass extends AtomicReferenceFieldUpdaterTest {
31     public void checkPrivateAccess() {
32     try {
33     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
34     AtomicReferenceFieldUpdater.newUpdater
35     (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
36     shouldThrow();
37     } catch (RuntimeException success) {
38     assertNotNull(success.getCause());
39     }
40     }
41    
42     public void checkCompareAndSetProtectedSub() {
43     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
44     AtomicReferenceFieldUpdater.newUpdater
45     (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField");
46     this.protectedField = one;
47     assertTrue(a.compareAndSet(this, one, two));
48     assertTrue(a.compareAndSet(this, two, m4));
49     assertSame(m4, a.get(this));
50     assertFalse(a.compareAndSet(this, m5, seven));
51 jsr166 1.35 assertNotSame(seven, a.get(this));
52 jsr166 1.34 assertTrue(a.compareAndSet(this, m4, seven));
53     assertSame(seven, a.get(this));
54     }
55     }
56    
57     static class UnrelatedClass {
58     public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
59     obj.x = one;
60     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
61     AtomicReferenceFieldUpdater.newUpdater
62     (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
63     assertSame(one, a.get(obj));
64     assertTrue(a.compareAndSet(obj, one, two));
65     assertSame(two, a.get(obj));
66     }
67    
68     public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) {
69     try {
70     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
71     AtomicReferenceFieldUpdater.newUpdater
72     (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
73     throw new AssertionError("should throw");
74     } catch (RuntimeException success) {
75     assertNotNull(success.getCause());
76     }
77     }
78     }
79    
80     static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
81 jsr166 1.26 return AtomicReferenceFieldUpdater.newUpdater
82     (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
83     }
84    
85 dl 1.3 /**
86 dl 1.7 * Construction with non-existent field throws RuntimeException
87 dl 1.3 */
88 jsr166 1.13 public void testConstructor() {
89 jsr166 1.12 try {
90 jsr166 1.26 updaterFor("y");
91 dl 1.3 shouldThrow();
92 jsr166 1.27 } catch (RuntimeException success) {
93 jsr166 1.29 assertNotNull(success.getCause());
94 jsr166 1.27 }
95 dl 1.1 }
96    
97 dl 1.3 /**
98 jsr166 1.28 * construction with field not of given type throws ClassCastException
99 dl 1.3 */
100 jsr166 1.13 public void testConstructor2() {
101 jsr166 1.12 try {
102 jsr166 1.26 updaterFor("z");
103 dl 1.3 shouldThrow();
104 jsr166 1.28 } catch (ClassCastException success) {}
105 dl 1.1 }
106    
107 dl 1.3 /**
108 jsr166 1.27 * Constructor with non-volatile field throws IllegalArgumentException
109 dl 1.3 */
110 jsr166 1.13 public void testConstructor3() {
111 jsr166 1.12 try {
112 jsr166 1.26 updaterFor("w");
113 dl 1.3 shouldThrow();
114 jsr166 1.27 } catch (IllegalArgumentException success) {}
115 dl 1.1 }
116    
117 dl 1.3 /**
118 jsr166 1.28 * Constructor with non-reference field throws ClassCastException
119     */
120     public void testConstructor4() {
121     try {
122     updaterFor("i");
123     shouldThrow();
124     } catch (ClassCastException success) {}
125     }
126    
127     /**
128 jsr166 1.34 * construction using private field from subclass throws RuntimeException
129     */
130     public void testPrivateFieldInSubclass() {
131     AtomicReferenceFieldUpdaterTestSubclass s =
132     new AtomicReferenceFieldUpdaterTestSubclass();
133     s.checkPrivateAccess();
134     }
135    
136     /**
137     * construction from unrelated class; package access is allowed,
138     * private access is not
139     */
140     public void testUnrelatedClassAccess() {
141     new UnrelatedClass().checkPackageAccess(this);
142     new UnrelatedClass().checkPrivateAccess(this);
143     }
144    
145     /**
146 jsr166 1.20 * get returns the last value set or assigned
147 dl 1.3 */
148 jsr166 1.13 public void testGetSet() {
149 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
150 jsr166 1.26 a = updaterFor("x");
151 dl 1.1 x = one;
152 jsr166 1.24 assertSame(one, a.get(this));
153     a.set(this, two);
154     assertSame(two, a.get(this));
155     a.set(this, m3);
156     assertSame(m3, a.get(this));
157 dl 1.1 }
158 dl 1.10
159     /**
160 jsr166 1.20 * get returns the last value lazySet by same thread
161 dl 1.10 */
162 jsr166 1.13 public void testGetLazySet() {
163 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
164 jsr166 1.26 a = updaterFor("x");
165 dl 1.10 x = one;
166 jsr166 1.24 assertSame(one, a.get(this));
167     a.lazySet(this, two);
168     assertSame(two, a.get(this));
169     a.lazySet(this, m3);
170     assertSame(m3, a.get(this));
171 dl 1.10 }
172    
173 dl 1.3 /**
174 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
175 dl 1.3 */
176 jsr166 1.13 public void testCompareAndSet() {
177 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
178 jsr166 1.26 a = updaterFor("x");
179 dl 1.1 x = one;
180 jsr166 1.19 assertTrue(a.compareAndSet(this, one, two));
181     assertTrue(a.compareAndSet(this, two, m4));
182     assertSame(m4, a.get(this));
183     assertFalse(a.compareAndSet(this, m5, seven));
184 jsr166 1.35 assertNotSame(seven, a.get(this));
185 jsr166 1.19 assertTrue(a.compareAndSet(this, m4, seven));
186 jsr166 1.24 assertSame(seven, a.get(this));
187 dl 1.1 }
188    
189 dl 1.3 /**
190 dl 1.4 * compareAndSet in one thread enables another waiting for value
191     * to succeed
192     */
193 jsr166 1.14 public void testCompareAndSetInMultipleThreads() throws Exception {
194 dl 1.4 x = one;
195 jsr166 1.32 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
196 jsr166 1.26 a = updaterFor("x");
197 dl 1.4
198 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
199     public void realRun() {
200     while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
201     Thread.yield();
202     }});
203 jsr166 1.14
204     t.start();
205     assertTrue(a.compareAndSet(this, one, two));
206     t.join(LONG_DELAY_MS);
207     assertFalse(t.isAlive());
208 jsr166 1.25 assertSame(three, a.get(this));
209 dl 1.4 }
210    
211     /**
212     * repeated weakCompareAndSet succeeds in changing value when equal
213 jsr166 1.11 * to expected
214 dl 1.3 */
215 jsr166 1.13 public void testWeakCompareAndSet() {
216 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
217 jsr166 1.26 a = updaterFor("x");
218 dl 1.1 x = one;
219 jsr166 1.31 do {} while (!a.weakCompareAndSet(this, one, two));
220     do {} while (!a.weakCompareAndSet(this, two, m4));
221 jsr166 1.24 assertSame(m4, a.get(this));
222 jsr166 1.31 do {} while (!a.weakCompareAndSet(this, m4, seven));
223 jsr166 1.24 assertSame(seven, a.get(this));
224 dl 1.1 }
225    
226 dl 1.3 /**
227 dl 1.4 * getAndSet returns previous value and sets to given value
228 dl 1.3 */
229 jsr166 1.13 public void testGetAndSet() {
230 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
231 jsr166 1.26 a = updaterFor("x");
232 dl 1.1 x = one;
233 jsr166 1.24 assertSame(one, a.getAndSet(this, zero));
234     assertSame(zero, a.getAndSet(this, m10));
235     assertSame(m10, a.getAndSet(this, 1));
236 dl 1.1 }
237    
238     }