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