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.17 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.9 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
jsr166 |
1.18 |
import java.util.concurrent.atomic.AtomicReference; |
11 |
dl |
1.1 |
|
12 |
dl |
1.3 |
public class AtomicReferenceTest extends JSR166TestCase { |
13 |
jsr166 |
1.16 |
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run(suite()); |
15 |
dl |
1.1 |
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(AtomicReferenceTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
dl |
1.4 |
/** |
21 |
dl |
1.5 |
* constructor initializes to given value |
22 |
dl |
1.4 |
*/ |
23 |
jsr166 |
1.11 |
public void testConstructor() { |
24 |
dl |
1.1 |
AtomicReference ai = new AtomicReference(one); |
25 |
jsr166 |
1.19 |
assertSame(one, ai.get()); |
26 |
dl |
1.1 |
} |
27 |
|
|
|
28 |
dl |
1.4 |
/** |
29 |
dl |
1.5 |
* default constructed initializes to null |
30 |
dl |
1.4 |
*/ |
31 |
jsr166 |
1.11 |
public void testConstructor2() { |
32 |
dl |
1.1 |
AtomicReference ai = new AtomicReference(); |
33 |
jsr166 |
1.14 |
assertNull(ai.get()); |
34 |
dl |
1.1 |
} |
35 |
|
|
|
36 |
dl |
1.4 |
/** |
37 |
dl |
1.5 |
* get returns the last value set |
38 |
dl |
1.4 |
*/ |
39 |
jsr166 |
1.11 |
public void testGetSet() { |
40 |
dl |
1.1 |
AtomicReference ai = new AtomicReference(one); |
41 |
jsr166 |
1.19 |
assertSame(one, ai.get()); |
42 |
jsr166 |
1.14 |
ai.set(two); |
43 |
jsr166 |
1.19 |
assertSame(two, ai.get()); |
44 |
jsr166 |
1.14 |
ai.set(m3); |
45 |
jsr166 |
1.19 |
assertSame(m3, ai.get()); |
46 |
dl |
1.1 |
} |
47 |
dl |
1.8 |
|
48 |
|
|
/** |
49 |
|
|
* get returns the last value lazySet in same thread |
50 |
|
|
*/ |
51 |
jsr166 |
1.11 |
public void testGetLazySet() { |
52 |
dl |
1.8 |
AtomicReference ai = new AtomicReference(one); |
53 |
jsr166 |
1.19 |
assertSame(one, ai.get()); |
54 |
jsr166 |
1.14 |
ai.lazySet(two); |
55 |
jsr166 |
1.19 |
assertSame(two, ai.get()); |
56 |
jsr166 |
1.14 |
ai.lazySet(m3); |
57 |
jsr166 |
1.19 |
assertSame(m3, ai.get()); |
58 |
dl |
1.8 |
} |
59 |
|
|
|
60 |
dl |
1.4 |
/** |
61 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
62 |
dl |
1.4 |
*/ |
63 |
jsr166 |
1.11 |
public void testCompareAndSet() { |
64 |
dl |
1.1 |
AtomicReference ai = new AtomicReference(one); |
65 |
jsr166 |
1.19 |
assertTrue(ai.compareAndSet(one, two)); |
66 |
|
|
assertTrue(ai.compareAndSet(two, m4)); |
67 |
|
|
assertSame(m4, ai.get()); |
68 |
|
|
assertFalse(ai.compareAndSet(m5, seven)); |
69 |
|
|
assertSame(m4, ai.get()); |
70 |
|
|
assertTrue(ai.compareAndSet(m4, seven)); |
71 |
|
|
assertSame(seven, ai.get()); |
72 |
dl |
1.1 |
} |
73 |
|
|
|
74 |
dl |
1.4 |
/** |
75 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
76 |
|
|
* to succeed |
77 |
|
|
*/ |
78 |
jsr166 |
1.12 |
public void testCompareAndSetInMultipleThreads() throws Exception { |
79 |
dl |
1.5 |
final AtomicReference ai = new AtomicReference(one); |
80 |
jsr166 |
1.13 |
Thread t = new Thread(new CheckedRunnable() { |
81 |
|
|
public void realRun() { |
82 |
|
|
while (!ai.compareAndSet(two, three)) |
83 |
|
|
Thread.yield(); |
84 |
|
|
}}); |
85 |
jsr166 |
1.12 |
|
86 |
|
|
t.start(); |
87 |
|
|
assertTrue(ai.compareAndSet(one, two)); |
88 |
|
|
t.join(LONG_DELAY_MS); |
89 |
|
|
assertFalse(t.isAlive()); |
90 |
jsr166 |
1.20 |
assertSame(three, ai.get()); |
91 |
dl |
1.5 |
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
95 |
jsr166 |
1.9 |
* to expected |
96 |
dl |
1.4 |
*/ |
97 |
jsr166 |
1.11 |
public void testWeakCompareAndSet() { |
98 |
dl |
1.1 |
AtomicReference ai = new AtomicReference(one); |
99 |
jsr166 |
1.19 |
while (!ai.weakCompareAndSet(one, two)); |
100 |
|
|
while (!ai.weakCompareAndSet(two, m4)); |
101 |
|
|
assertSame(m4, ai.get()); |
102 |
|
|
while (!ai.weakCompareAndSet(m4, seven)); |
103 |
|
|
assertSame(seven, ai.get()); |
104 |
dl |
1.1 |
} |
105 |
|
|
|
106 |
dl |
1.4 |
/** |
107 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value |
108 |
dl |
1.4 |
*/ |
109 |
jsr166 |
1.11 |
public void testGetAndSet() { |
110 |
dl |
1.1 |
AtomicReference ai = new AtomicReference(one); |
111 |
jsr166 |
1.19 |
assertSame(one, ai.getAndSet(zero)); |
112 |
|
|
assertSame(zero, ai.getAndSet(m10)); |
113 |
|
|
assertSame(m10, ai.getAndSet(one)); |
114 |
dl |
1.2 |
} |
115 |
|
|
|
116 |
dl |
1.4 |
/** |
117 |
dl |
1.5 |
* a deserialized serialized atomic holds same value |
118 |
dl |
1.4 |
*/ |
119 |
jsr166 |
1.12 |
public void testSerialization() throws Exception { |
120 |
jsr166 |
1.18 |
AtomicReference x = new AtomicReference(); |
121 |
|
|
AtomicReference y = serialClone(x); |
122 |
jsr166 |
1.21 |
assertNotSame(x, y); |
123 |
jsr166 |
1.18 |
x.set(one); |
124 |
|
|
AtomicReference z = serialClone(x); |
125 |
jsr166 |
1.21 |
assertNotSame(y, z); |
126 |
jsr166 |
1.18 |
assertEquals(one, x.get()); |
127 |
|
|
assertEquals(null, y.get()); |
128 |
|
|
assertEquals(one, z.get()); |
129 |
dl |
1.1 |
} |
130 |
|
|
|
131 |
dl |
1.7 |
/** |
132 |
|
|
* toString returns current value. |
133 |
jsr166 |
1.9 |
*/ |
134 |
dl |
1.7 |
public void testToString() { |
135 |
jsr166 |
1.9 |
AtomicReference<Integer> ai = new AtomicReference<Integer>(one); |
136 |
jsr166 |
1.20 |
assertEquals(one.toString(), ai.toString()); |
137 |
dl |
1.7 |
ai.set(two); |
138 |
jsr166 |
1.20 |
assertEquals(two.toString(), ai.toString()); |
139 |
dl |
1.7 |
} |
140 |
|
|
|
141 |
dl |
1.1 |
} |