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