ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.36
Committed: Wed Sep 20 00:41:13 2017 UTC (6 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +4 -56 lines
Log Message:
8187607: [Testbug] Atomic*FieldUpdaterTest.checkPrivateAccess uses nested classes

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 static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
30 jsr166 1.26 return AtomicReferenceFieldUpdater.newUpdater
31     (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
32     }
33    
34 dl 1.3 /**
35 dl 1.7 * Construction with non-existent field throws RuntimeException
36 dl 1.3 */
37 jsr166 1.13 public void testConstructor() {
38 jsr166 1.12 try {
39 jsr166 1.26 updaterFor("y");
40 dl 1.3 shouldThrow();
41 jsr166 1.27 } catch (RuntimeException success) {
42 jsr166 1.29 assertNotNull(success.getCause());
43 jsr166 1.27 }
44 dl 1.1 }
45    
46 dl 1.3 /**
47 jsr166 1.28 * construction with field not of given type throws ClassCastException
48 dl 1.3 */
49 jsr166 1.13 public void testConstructor2() {
50 jsr166 1.12 try {
51 jsr166 1.26 updaterFor("z");
52 dl 1.3 shouldThrow();
53 jsr166 1.28 } catch (ClassCastException success) {}
54 dl 1.1 }
55    
56 dl 1.3 /**
57 jsr166 1.27 * Constructor with non-volatile field throws IllegalArgumentException
58 dl 1.3 */
59 jsr166 1.13 public void testConstructor3() {
60 jsr166 1.12 try {
61 jsr166 1.26 updaterFor("w");
62 dl 1.3 shouldThrow();
63 jsr166 1.27 } catch (IllegalArgumentException success) {}
64 dl 1.1 }
65    
66 dl 1.3 /**
67 jsr166 1.28 * Constructor with non-reference field throws ClassCastException
68     */
69     public void testConstructor4() {
70     try {
71     updaterFor("i");
72     shouldThrow();
73     } catch (ClassCastException success) {}
74     }
75    
76     /**
77 jsr166 1.34 * construction using private field from subclass throws RuntimeException
78     */
79     public void testPrivateFieldInSubclass() {
80 jsr166 1.36 new NonNestmates.AtomicReferenceFieldUpdaterTestSubclass()
81     .checkPrivateAccess();
82 jsr166 1.34 }
83    
84     /**
85     * construction from unrelated class; package access is allowed,
86     * private access is not
87     */
88     public void testUnrelatedClassAccess() {
89 jsr166 1.36 new NonNestmates().checkPackageAccess(this);
90     new NonNestmates().checkPrivateAccess(this);
91 jsr166 1.34 }
92    
93     /**
94 jsr166 1.20 * get returns the last value set or assigned
95 dl 1.3 */
96 jsr166 1.13 public void testGetSet() {
97 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
98 jsr166 1.26 a = updaterFor("x");
99 dl 1.1 x = one;
100 jsr166 1.24 assertSame(one, a.get(this));
101     a.set(this, two);
102     assertSame(two, a.get(this));
103     a.set(this, m3);
104     assertSame(m3, a.get(this));
105 dl 1.1 }
106 dl 1.10
107     /**
108 jsr166 1.20 * get returns the last value lazySet by same thread
109 dl 1.10 */
110 jsr166 1.13 public void testGetLazySet() {
111 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
112 jsr166 1.26 a = updaterFor("x");
113 dl 1.10 x = one;
114 jsr166 1.24 assertSame(one, a.get(this));
115     a.lazySet(this, two);
116     assertSame(two, a.get(this));
117     a.lazySet(this, m3);
118     assertSame(m3, a.get(this));
119 dl 1.10 }
120    
121 dl 1.3 /**
122 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
123 dl 1.3 */
124 jsr166 1.13 public void testCompareAndSet() {
125 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
126 jsr166 1.26 a = updaterFor("x");
127 dl 1.1 x = one;
128 jsr166 1.19 assertTrue(a.compareAndSet(this, one, two));
129     assertTrue(a.compareAndSet(this, two, m4));
130     assertSame(m4, a.get(this));
131     assertFalse(a.compareAndSet(this, m5, seven));
132 jsr166 1.35 assertNotSame(seven, a.get(this));
133 jsr166 1.19 assertTrue(a.compareAndSet(this, m4, seven));
134 jsr166 1.24 assertSame(seven, a.get(this));
135 dl 1.1 }
136    
137 dl 1.3 /**
138 dl 1.4 * compareAndSet in one thread enables another waiting for value
139     * to succeed
140     */
141 jsr166 1.14 public void testCompareAndSetInMultipleThreads() throws Exception {
142 dl 1.4 x = one;
143 jsr166 1.32 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
144 jsr166 1.26 a = updaterFor("x");
145 dl 1.4
146 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
147     public void realRun() {
148     while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
149     Thread.yield();
150     }});
151 jsr166 1.14
152     t.start();
153     assertTrue(a.compareAndSet(this, one, two));
154     t.join(LONG_DELAY_MS);
155     assertFalse(t.isAlive());
156 jsr166 1.25 assertSame(three, a.get(this));
157 dl 1.4 }
158    
159     /**
160     * repeated weakCompareAndSet succeeds in changing value when equal
161 jsr166 1.11 * to expected
162 dl 1.3 */
163 jsr166 1.13 public void testWeakCompareAndSet() {
164 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
165 jsr166 1.26 a = updaterFor("x");
166 dl 1.1 x = one;
167 jsr166 1.31 do {} while (!a.weakCompareAndSet(this, one, two));
168     do {} while (!a.weakCompareAndSet(this, two, m4));
169 jsr166 1.24 assertSame(m4, a.get(this));
170 jsr166 1.31 do {} while (!a.weakCompareAndSet(this, m4, seven));
171 jsr166 1.24 assertSame(seven, a.get(this));
172 dl 1.1 }
173    
174 dl 1.3 /**
175 dl 1.4 * getAndSet returns previous value and sets to given value
176 dl 1.3 */
177 jsr166 1.13 public void testGetAndSet() {
178 jsr166 1.32 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
179 jsr166 1.26 a = updaterFor("x");
180 dl 1.1 x = one;
181 jsr166 1.24 assertSame(one, a.getAndSet(this, zero));
182     assertSame(zero, a.getAndSet(this, m10));
183     assertSame(m10, a.getAndSet(this, 1));
184 dl 1.1 }
185    
186     }