ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.28
Committed: Tue Apr 2 16:14:39 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.27: +13 -2 lines
Log Message:
fix exception specs to match reality, and update corresponding tck tests

File Contents

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