ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.26
Committed: Mon Apr 1 20:58:58 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.25: +14 -39 lines
Log Message:
RuntimeException is *not* ok for valid fields; refactor a little

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