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 |
|
|
* http://creativecommons.org/licenses/publicdomain |
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 |
|
|
import java.util.concurrent.atomic.*; |
11 |
dl |
1.2 |
import java.io.*; |
12 |
dl |
1.1 |
|
13 |
dl |
1.3 |
public class AtomicReferenceTest extends JSR166TestCase { |
14 |
dl |
1.1 |
public static void main (String[] args) { |
15 |
|
|
junit.textui.TestRunner.run (suite()); |
16 |
|
|
} |
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(AtomicReferenceTest.class); |
19 |
|
|
} |
20 |
|
|
|
21 |
dl |
1.4 |
/** |
22 |
dl |
1.5 |
* constructor initializes to given value |
23 |
dl |
1.4 |
*/ |
24 |
dl |
1.1 |
public void testConstructor(){ |
25 |
|
|
AtomicReference ai = new AtomicReference(one); |
26 |
|
|
assertEquals(one,ai.get()); |
27 |
|
|
} |
28 |
|
|
|
29 |
dl |
1.4 |
/** |
30 |
dl |
1.5 |
* default constructed initializes to null |
31 |
dl |
1.4 |
*/ |
32 |
dl |
1.1 |
public void testConstructor2(){ |
33 |
|
|
AtomicReference ai = new AtomicReference(); |
34 |
|
|
assertNull(ai.get()); |
35 |
|
|
} |
36 |
|
|
|
37 |
dl |
1.4 |
/** |
38 |
dl |
1.5 |
* get returns the last value set |
39 |
dl |
1.4 |
*/ |
40 |
dl |
1.1 |
public void testGetSet(){ |
41 |
|
|
AtomicReference ai = new AtomicReference(one); |
42 |
|
|
assertEquals(one,ai.get()); |
43 |
|
|
ai.set(two); |
44 |
|
|
assertEquals(two,ai.get()); |
45 |
|
|
ai.set(m3); |
46 |
|
|
assertEquals(m3,ai.get()); |
47 |
|
|
} |
48 |
dl |
1.8 |
|
49 |
|
|
/** |
50 |
|
|
* get returns the last value lazySet in same thread |
51 |
|
|
*/ |
52 |
|
|
public void testGetLazySet(){ |
53 |
|
|
AtomicReference ai = new AtomicReference(one); |
54 |
|
|
assertEquals(one,ai.get()); |
55 |
|
|
ai.lazySet(two); |
56 |
|
|
assertEquals(two,ai.get()); |
57 |
|
|
ai.lazySet(m3); |
58 |
|
|
assertEquals(m3,ai.get()); |
59 |
|
|
} |
60 |
|
|
|
61 |
dl |
1.4 |
/** |
62 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
63 |
dl |
1.4 |
*/ |
64 |
dl |
1.1 |
public void testCompareAndSet(){ |
65 |
|
|
AtomicReference ai = new AtomicReference(one); |
66 |
|
|
assertTrue(ai.compareAndSet(one,two)); |
67 |
|
|
assertTrue(ai.compareAndSet(two,m4)); |
68 |
|
|
assertEquals(m4,ai.get()); |
69 |
|
|
assertFalse(ai.compareAndSet(m5,seven)); |
70 |
|
|
assertFalse((seven.equals(ai.get()))); |
71 |
|
|
assertTrue(ai.compareAndSet(m4,seven)); |
72 |
|
|
assertEquals(seven,ai.get()); |
73 |
|
|
} |
74 |
|
|
|
75 |
dl |
1.4 |
/** |
76 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
77 |
|
|
* to succeed |
78 |
|
|
*/ |
79 |
|
|
public void testCompareAndSetInMultipleThreads() { |
80 |
|
|
final AtomicReference ai = new AtomicReference(one); |
81 |
|
|
Thread t = new Thread(new Runnable() { |
82 |
|
|
public void run() { |
83 |
jsr166 |
1.10 |
while (!ai.compareAndSet(two, three)) Thread.yield(); |
84 |
dl |
1.5 |
}}); |
85 |
|
|
try { |
86 |
|
|
t.start(); |
87 |
|
|
assertTrue(ai.compareAndSet(one, two)); |
88 |
|
|
t.join(LONG_DELAY_MS); |
89 |
|
|
assertFalse(t.isAlive()); |
90 |
|
|
assertEquals(ai.get(), three); |
91 |
|
|
} |
92 |
jsr166 |
1.10 |
catch (Exception e) { |
93 |
dl |
1.5 |
unexpectedException(); |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
99 |
jsr166 |
1.9 |
* to expected |
100 |
dl |
1.4 |
*/ |
101 |
dl |
1.1 |
public void testWeakCompareAndSet(){ |
102 |
|
|
AtomicReference ai = new AtomicReference(one); |
103 |
jsr166 |
1.10 |
while (!ai.weakCompareAndSet(one,two)); |
104 |
|
|
while (!ai.weakCompareAndSet(two,m4)); |
105 |
dl |
1.1 |
assertEquals(m4,ai.get()); |
106 |
jsr166 |
1.10 |
while (!ai.weakCompareAndSet(m4,seven)); |
107 |
dl |
1.1 |
assertEquals(seven,ai.get()); |
108 |
|
|
} |
109 |
|
|
|
110 |
dl |
1.4 |
/** |
111 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value |
112 |
dl |
1.4 |
*/ |
113 |
dl |
1.1 |
public void testGetAndSet(){ |
114 |
|
|
AtomicReference ai = new AtomicReference(one); |
115 |
|
|
assertEquals(one,ai.getAndSet(zero)); |
116 |
|
|
assertEquals(zero,ai.getAndSet(m10)); |
117 |
|
|
assertEquals(m10,ai.getAndSet(one)); |
118 |
dl |
1.2 |
} |
119 |
|
|
|
120 |
dl |
1.4 |
/** |
121 |
dl |
1.5 |
* a deserialized serialized atomic holds same value |
122 |
dl |
1.4 |
*/ |
123 |
dl |
1.2 |
public void testSerialization() { |
124 |
|
|
AtomicReference l = new AtomicReference(); |
125 |
|
|
|
126 |
|
|
try { |
127 |
|
|
l.set(one); |
128 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
129 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
130 |
|
|
out.writeObject(l); |
131 |
|
|
out.close(); |
132 |
|
|
|
133 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
134 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
135 |
|
|
AtomicReference r = (AtomicReference) in.readObject(); |
136 |
|
|
assertEquals(l.get(), r.get()); |
137 |
jsr166 |
1.10 |
} catch (Exception e){ |
138 |
dl |
1.4 |
unexpectedException(); |
139 |
dl |
1.2 |
} |
140 |
dl |
1.1 |
} |
141 |
|
|
|
142 |
dl |
1.7 |
/** |
143 |
|
|
* toString returns current value. |
144 |
jsr166 |
1.9 |
*/ |
145 |
dl |
1.7 |
public void testToString() { |
146 |
jsr166 |
1.9 |
AtomicReference<Integer> ai = new AtomicReference<Integer>(one); |
147 |
dl |
1.7 |
assertEquals(ai.toString(), one.toString()); |
148 |
|
|
ai.set(two); |
149 |
|
|
assertEquals(ai.toString(), two.toString()); |
150 |
|
|
} |
151 |
|
|
|
152 |
dl |
1.1 |
} |