ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java (file contents):
Revision 1.13 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.35 by jsr166, Sat Mar 18 20:42:20 2017 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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.*;
9 > import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
15      volatile Integer x = null;
16 +    protected volatile Integer protectedField;
17 +    private volatile Integer privateField;
18      Object z;
19      Integer w;
20 +    volatile int i;
21  
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25      public static Test suite() {
26          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
27      }
28  
29 +    // 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 +            assertNotSame(seven, a.get(this));
52 +            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 +        return AtomicReferenceFieldUpdater.newUpdater
82 +            (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
83 +    }
84 +
85      /**
86       * Construction with non-existent field throws RuntimeException
87       */
88      public void testConstructor() {
89          try {
90 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31 <                a = AtomicReferenceFieldUpdater.newUpdater
32 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
90 >            updaterFor("y");
91              shouldThrow();
92 +        } catch (RuntimeException success) {
93 +            assertNotNull(success.getCause());
94          }
35        catch (RuntimeException rt) {}
95      }
96  
38
97      /**
98 <     * construction with field not of given type throws RuntimeException
98 >     * construction with field not of given type throws ClassCastException
99       */
100      public void testConstructor2() {
101          try {
102 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
45 <                a = AtomicReferenceFieldUpdater.newUpdater
46 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
102 >            updaterFor("z");
103              shouldThrow();
104 <        }
49 <        catch (RuntimeException rt) {}
104 >        } catch (ClassCastException success) {}
105      }
106  
107      /**
108 <     * Constructor with non-volatile field throws exception
108 >     * Constructor with non-volatile field throws IllegalArgumentException
109       */
110      public void testConstructor3() {
111          try {
112 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
58 <                a = AtomicReferenceFieldUpdater.newUpdater
59 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
112 >            updaterFor("w");
113              shouldThrow();
114 <        }
62 <        catch (RuntimeException rt) {}
114 >        } catch (IllegalArgumentException success) {}
115      }
116  
117      /**
118 <     *  get returns the last value set or assigned
118 >     * Constructor with non-reference field throws ClassCastException
119       */
120 <    public void testGetSet() {
69 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
120 >    public void testConstructor4() {
121          try {
122 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
123 <        } catch (RuntimeException ok) {
124 <            return;
125 <        }
122 >            updaterFor("i");
123 >            shouldThrow();
124 >        } catch (ClassCastException success) {}
125 >    }
126 >
127 >    /**
128 >     * 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 >     * get returns the last value set or assigned
147 >     */
148 >    public void testGetSet() {
149 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
150 >        a = updaterFor("x");
151          x = one;
152 <        assertEquals(one,a.get(this));
153 <        a.set(this,two);
154 <        assertEquals(two,a.get(this));
155 <        a.set(this,m3);
156 <        assertEquals(m3,a.get(this));
152 >        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      }
158  
159      /**
160 <     *  get returns the last value lazySet by same thread
160 >     * get returns the last value lazySet by same thread
161       */
162      public void testGetLazySet() {
163 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
164 <        try {
89 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
90 <        } catch (RuntimeException ok) {
91 <            return;
92 <        }
163 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
164 >        a = updaterFor("x");
165          x = one;
166 <        assertEquals(one,a.get(this));
167 <        a.lazySet(this,two);
168 <        assertEquals(two,a.get(this));
169 <        a.lazySet(this,m3);
170 <        assertEquals(m3,a.get(this));
166 >        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      }
172  
173      /**
174       * compareAndSet succeeds in changing value if equal to expected else fails
175       */
176      public void testCompareAndSet() {
177 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
178 <        try {
107 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
108 <        } catch (RuntimeException ok) {
109 <            return;
110 <        }
177 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
178 >        a = updaterFor("x");
179          x = one;
180 <        assertTrue(a.compareAndSet(this,one,two));
181 <        assertTrue(a.compareAndSet(this,two,m4));
182 <        assertEquals(m4,a.get(this));
183 <        assertFalse(a.compareAndSet(this,m5,seven));
184 <        assertFalse((seven == a.get(this)));
185 <        assertTrue(a.compareAndSet(this,m4,seven));
186 <        assertEquals(seven,a.get(this));
180 >        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 >        assertNotSame(seven, a.get(this));
185 >        assertTrue(a.compareAndSet(this, m4, seven));
186 >        assertSame(seven, a.get(this));
187      }
188  
189      /**
190       * compareAndSet in one thread enables another waiting for value
191       * to succeed
192       */
193 <    public void testCompareAndSetInMultipleThreads() {
193 >    public void testCompareAndSetInMultipleThreads() throws Exception {
194          x = one;
195 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
196 <        try {
129 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
130 <        } catch (RuntimeException ok) {
131 <            return;
132 <        }
195 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
196 >        a = updaterFor("x");
197  
198 <        Thread t = new Thread(new Runnable() {
199 <                public void run() {
200 <                    while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
201 <                }});
202 <        try {
203 <            t.start();
204 <            assertTrue(a.compareAndSet(this, one, two));
205 <            t.join(LONG_DELAY_MS);
206 <            assertFalse(t.isAlive());
207 <            assertEquals(a.get(this), three);
208 <        }
145 <        catch (Exception e) {
146 <            unexpectedException();
147 <        }
198 >        Thread t = new Thread(new CheckedRunnable() {
199 >            public void realRun() {
200 >                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
201 >                    Thread.yield();
202 >            }});
203 >
204 >        t.start();
205 >        assertTrue(a.compareAndSet(this, one, two));
206 >        t.join(LONG_DELAY_MS);
207 >        assertFalse(t.isAlive());
208 >        assertSame(three, a.get(this));
209      }
210  
211      /**
# Line 152 | Line 213 | public class AtomicReferenceFieldUpdater
213       * to expected
214       */
215      public void testWeakCompareAndSet() {
216 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
217 <        try {
157 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
158 <        } catch (RuntimeException ok) {
159 <            return;
160 <        }
216 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
217 >        a = updaterFor("x");
218          x = one;
219 <        while (!a.weakCompareAndSet(this,one,two));
220 <        while (!a.weakCompareAndSet(this,two,m4));
221 <        assertEquals(m4,a.get(this));
222 <        while (!a.weakCompareAndSet(this,m4,seven));
223 <        assertEquals(seven,a.get(this));
219 >        do {} while (!a.weakCompareAndSet(this, one, two));
220 >        do {} while (!a.weakCompareAndSet(this, two, m4));
221 >        assertSame(m4, a.get(this));
222 >        do {} while (!a.weakCompareAndSet(this, m4, seven));
223 >        assertSame(seven, a.get(this));
224      }
225  
226      /**
227       * getAndSet returns previous value and sets to given value
228       */
229      public void testGetAndSet() {
230 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
231 <        try {
175 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
176 <        } catch (RuntimeException ok) {
177 <            return;
178 <        }
230 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
231 >        a = updaterFor("x");
232          x = one;
233 <        assertEquals(one,a.getAndSet(this, zero));
234 <        assertEquals(zero,a.getAndSet(this,m10));
235 <        assertEquals(m10,a.getAndSet(this,1));
233 >        assertSame(one, a.getAndSet(this, zero));
234 >        assertSame(zero, a.getAndSet(this, m10));
235 >        assertSame(m10, a.getAndSet(this, 1));
236      }
237  
238   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines