ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.30
Committed: Wed Dec 31 19:05:42 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +3 -1 lines
Log Message:
no wildcard imports

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