1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.concurrent.atomic.*; |
10 |
dl |
1.2 |
import java.io.*; |
11 |
dl |
1.1 |
|
12 |
dl |
1.3 |
public class AtomicReferenceTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main (String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
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 |
dl |
1.1 |
public void testConstructor(){ |
24 |
|
|
AtomicReference ai = new AtomicReference(one); |
25 |
|
|
assertEquals(one,ai.get()); |
26 |
|
|
} |
27 |
|
|
|
28 |
dl |
1.4 |
/** |
29 |
dl |
1.5 |
* default constructed initializes to null |
30 |
dl |
1.4 |
*/ |
31 |
dl |
1.1 |
public void testConstructor2(){ |
32 |
|
|
AtomicReference ai = new AtomicReference(); |
33 |
|
|
assertNull(ai.get()); |
34 |
|
|
} |
35 |
|
|
|
36 |
dl |
1.4 |
/** |
37 |
dl |
1.5 |
* get returns the last value set |
38 |
dl |
1.4 |
*/ |
39 |
dl |
1.1 |
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 |
|
|
|
47 |
|
|
} |
48 |
dl |
1.4 |
/** |
49 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
50 |
dl |
1.4 |
*/ |
51 |
dl |
1.1 |
public void testCompareAndSet(){ |
52 |
|
|
AtomicReference ai = new AtomicReference(one); |
53 |
|
|
assertTrue(ai.compareAndSet(one,two)); |
54 |
|
|
assertTrue(ai.compareAndSet(two,m4)); |
55 |
|
|
assertEquals(m4,ai.get()); |
56 |
|
|
assertFalse(ai.compareAndSet(m5,seven)); |
57 |
|
|
assertFalse((seven.equals(ai.get()))); |
58 |
|
|
assertTrue(ai.compareAndSet(m4,seven)); |
59 |
|
|
assertEquals(seven,ai.get()); |
60 |
|
|
} |
61 |
|
|
|
62 |
dl |
1.4 |
/** |
63 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
64 |
|
|
* to succeed |
65 |
|
|
*/ |
66 |
|
|
public void testCompareAndSetInMultipleThreads() { |
67 |
|
|
final AtomicReference ai = new AtomicReference(one); |
68 |
|
|
Thread t = new Thread(new Runnable() { |
69 |
|
|
public void run() { |
70 |
|
|
while(!ai.compareAndSet(two, three)) Thread.yield(); |
71 |
|
|
}}); |
72 |
|
|
try { |
73 |
|
|
t.start(); |
74 |
|
|
assertTrue(ai.compareAndSet(one, two)); |
75 |
|
|
t.join(LONG_DELAY_MS); |
76 |
|
|
assertFalse(t.isAlive()); |
77 |
|
|
assertEquals(ai.get(), three); |
78 |
|
|
} |
79 |
|
|
catch(Exception e) { |
80 |
|
|
unexpectedException(); |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
86 |
|
|
* to expected |
87 |
dl |
1.4 |
*/ |
88 |
dl |
1.1 |
public void testWeakCompareAndSet(){ |
89 |
|
|
AtomicReference ai = new AtomicReference(one); |
90 |
|
|
while(!ai.weakCompareAndSet(one,two)); |
91 |
|
|
while(!ai.weakCompareAndSet(two,m4)); |
92 |
|
|
assertEquals(m4,ai.get()); |
93 |
|
|
while(!ai.weakCompareAndSet(m4,seven)); |
94 |
|
|
assertEquals(seven,ai.get()); |
95 |
|
|
} |
96 |
|
|
|
97 |
dl |
1.4 |
/** |
98 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value |
99 |
dl |
1.4 |
*/ |
100 |
dl |
1.1 |
public void testGetAndSet(){ |
101 |
|
|
AtomicReference ai = new AtomicReference(one); |
102 |
|
|
assertEquals(one,ai.getAndSet(zero)); |
103 |
|
|
assertEquals(zero,ai.getAndSet(m10)); |
104 |
|
|
assertEquals(m10,ai.getAndSet(one)); |
105 |
dl |
1.2 |
} |
106 |
|
|
|
107 |
dl |
1.4 |
/** |
108 |
dl |
1.5 |
* a deserialized serialized atomic holds same value |
109 |
dl |
1.4 |
*/ |
110 |
dl |
1.2 |
public void testSerialization() { |
111 |
|
|
AtomicReference l = new AtomicReference(); |
112 |
|
|
|
113 |
|
|
try { |
114 |
|
|
l.set(one); |
115 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
116 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
117 |
|
|
out.writeObject(l); |
118 |
|
|
out.close(); |
119 |
|
|
|
120 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
121 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
122 |
|
|
AtomicReference r = (AtomicReference) in.readObject(); |
123 |
|
|
assertEquals(l.get(), r.get()); |
124 |
|
|
} catch(Exception e){ |
125 |
dl |
1.4 |
unexpectedException(); |
126 |
dl |
1.2 |
} |
127 |
dl |
1.1 |
} |
128 |
|
|
|
129 |
|
|
} |
130 |
|
|
|