1 |
dl |
1.1 |
/* |
2 |
dl |
1.5 |
* 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.15 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.6 |
* 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.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.11 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.1 |
|
14 |
dl |
1.2 |
public class ExchangerTest extends JSR166TestCase { |
15 |
jsr166 |
1.6 |
|
16 |
dl |
1.1 |
public static void main(String[] args) { |
17 |
jsr166 |
1.14 |
junit.textui.TestRunner.run(suite()); |
18 |
dl |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
jsr166 |
1.10 |
return new TestSuite(ExchangerTest.class); |
21 |
dl |
1.1 |
} |
22 |
|
|
|
23 |
dl |
1.3 |
/** |
24 |
dl |
1.4 |
* exchange exchanges objects across two threads |
25 |
dl |
1.3 |
*/ |
26 |
jsr166 |
1.17 |
public void testExchange() { |
27 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
28 |
jsr166 |
1.17 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
29 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
30 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two)); |
31 |
|
|
assertSame(two, e.exchange(one)); |
32 |
jsr166 |
1.9 |
}}); |
33 |
jsr166 |
1.17 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
34 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
35 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one)); |
36 |
|
|
assertSame(one, e.exchange(two)); |
37 |
jsr166 |
1.9 |
}}); |
38 |
|
|
|
39 |
jsr166 |
1.17 |
awaitTermination(t1); |
40 |
|
|
awaitTermination(t2); |
41 |
dl |
1.2 |
} |
42 |
|
|
|
43 |
dl |
1.3 |
/** |
44 |
dl |
1.4 |
* timed exchange exchanges objects across two threads |
45 |
dl |
1.3 |
*/ |
46 |
jsr166 |
1.17 |
public void testTimedExchange() { |
47 |
dl |
1.2 |
final Exchanger e = new Exchanger(); |
48 |
jsr166 |
1.17 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
49 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
50 |
jsr166 |
1.17 |
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
51 |
|
|
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
52 |
jsr166 |
1.9 |
}}); |
53 |
jsr166 |
1.17 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
54 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
55 |
jsr166 |
1.17 |
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
56 |
|
|
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
57 |
jsr166 |
1.9 |
}}); |
58 |
|
|
|
59 |
jsr166 |
1.17 |
awaitTermination(t1); |
60 |
|
|
awaitTermination(t2); |
61 |
dl |
1.1 |
} |
62 |
|
|
|
63 |
dl |
1.3 |
/** |
64 |
dl |
1.4 |
* interrupt during wait for exchange throws IE |
65 |
dl |
1.3 |
*/ |
66 |
jsr166 |
1.17 |
public void testExchange_InterruptedException() { |
67 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
68 |
jsr166 |
1.17 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
69 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
70 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
71 |
jsr166 |
1.17 |
threadStarted.countDown(); |
72 |
jsr166 |
1.9 |
e.exchange(one); |
73 |
|
|
}}); |
74 |
|
|
|
75 |
jsr166 |
1.17 |
await(threadStarted); |
76 |
jsr166 |
1.9 |
t.interrupt(); |
77 |
jsr166 |
1.17 |
awaitTermination(t); |
78 |
dl |
1.1 |
} |
79 |
|
|
|
80 |
dl |
1.3 |
/** |
81 |
dl |
1.4 |
* interrupt during wait for timed exchange throws IE |
82 |
dl |
1.3 |
*/ |
83 |
jsr166 |
1.17 |
public void testTimedExchange_InterruptedException() { |
84 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
85 |
jsr166 |
1.17 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
86 |
|
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
87 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
88 |
jsr166 |
1.17 |
threadStarted.countDown(); |
89 |
|
|
e.exchange(null, LONG_DELAY_MS, MILLISECONDS); |
90 |
jsr166 |
1.9 |
}}); |
91 |
|
|
|
92 |
jsr166 |
1.17 |
await(threadStarted); |
93 |
jsr166 |
1.9 |
t.interrupt(); |
94 |
jsr166 |
1.17 |
awaitTermination(t); |
95 |
dl |
1.1 |
} |
96 |
|
|
|
97 |
dl |
1.3 |
/** |
98 |
jsr166 |
1.17 |
* timeout during wait for timed exchange throws TimeoutException |
99 |
dl |
1.3 |
*/ |
100 |
jsr166 |
1.17 |
public void testExchange_TimeoutException() { |
101 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
102 |
jsr166 |
1.17 |
Thread t = newStartedThread(new CheckedRunnable() { |
103 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
104 |
jsr166 |
1.17 |
long startTime = System.nanoTime(); |
105 |
|
|
try { |
106 |
jsr166 |
1.20 |
e.exchange(null, timeoutMillis(), MILLISECONDS); |
107 |
jsr166 |
1.17 |
shouldThrow(); |
108 |
jsr166 |
1.20 |
} catch (TimeoutException success) {} |
109 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
110 |
jsr166 |
1.17 |
}}); |
111 |
jsr166 |
1.9 |
|
112 |
jsr166 |
1.17 |
awaitTermination(t); |
113 |
dl |
1.1 |
} |
114 |
dl |
1.4 |
|
115 |
|
|
/** |
116 |
|
|
* If one exchanging thread is interrupted, another succeeds. |
117 |
|
|
*/ |
118 |
jsr166 |
1.18 |
public void testReplacementAfterExchange() { |
119 |
dl |
1.4 |
final Exchanger e = new Exchanger(); |
120 |
jsr166 |
1.17 |
final CountDownLatch exchanged = new CountDownLatch(2); |
121 |
|
|
final CountDownLatch interrupted = new CountDownLatch(1); |
122 |
|
|
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
123 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
124 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one)); |
125 |
jsr166 |
1.17 |
exchanged.countDown(); |
126 |
jsr166 |
1.13 |
e.exchange(two); |
127 |
jsr166 |
1.9 |
}}); |
128 |
jsr166 |
1.17 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
129 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
130 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two)); |
131 |
jsr166 |
1.17 |
exchanged.countDown(); |
132 |
|
|
interrupted.await(); |
133 |
jsr166 |
1.13 |
assertSame(three, e.exchange(one)); |
134 |
jsr166 |
1.9 |
}}); |
135 |
jsr166 |
1.17 |
Thread t3 = newStartedThread(new CheckedRunnable() { |
136 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
137 |
jsr166 |
1.17 |
interrupted.await(); |
138 |
jsr166 |
1.13 |
assertSame(one, e.exchange(three)); |
139 |
jsr166 |
1.9 |
}}); |
140 |
|
|
|
141 |
jsr166 |
1.17 |
await(exchanged); |
142 |
jsr166 |
1.9 |
t1.interrupt(); |
143 |
jsr166 |
1.19 |
awaitTermination(t1); |
144 |
jsr166 |
1.17 |
interrupted.countDown(); |
145 |
|
|
awaitTermination(t2); |
146 |
|
|
awaitTermination(t3); |
147 |
dl |
1.4 |
} |
148 |
|
|
|
149 |
dl |
1.1 |
} |