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.*; |
11 |
< |
import java.util.concurrent.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
> |
import java.util.concurrent.CountDownLatch; |
12 |
> |
import java.util.concurrent.Exchanger; |
13 |
> |
import java.util.concurrent.TimeoutException; |
14 |
> |
|
15 |
> |
import junit.framework.Test; |
16 |
> |
import junit.framework.TestSuite; |
17 |
|
|
18 |
|
public class ExchangerTest extends JSR166TestCase { |
19 |
< |
|
19 |
> |
|
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
21 |
> |
junit.textui.TestRunner.run(suite()); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(ExchangerTest.class); |
24 |
> |
return new TestSuite(ExchangerTest.class); |
25 |
|
} |
26 |
|
|
27 |
|
/** |
29 |
|
*/ |
30 |
|
public void testExchange() { |
31 |
|
final Exchanger e = new Exchanger(); |
32 |
< |
Thread t1 = new Thread(new Runnable(){ |
33 |
< |
public void run(){ |
34 |
< |
try { |
35 |
< |
Object v = e.exchange(one); |
36 |
< |
threadAssertEquals(v, two); |
37 |
< |
Object w = e.exchange(v); |
38 |
< |
threadAssertEquals(w, one); |
39 |
< |
} catch(InterruptedException e){ |
40 |
< |
threadUnexpectedException(); |
41 |
< |
} |
42 |
< |
} |
43 |
< |
}); |
44 |
< |
Thread t2 = new Thread(new Runnable(){ |
40 |
< |
public void run(){ |
41 |
< |
try { |
42 |
< |
Object v = e.exchange(two); |
43 |
< |
threadAssertEquals(v, one); |
44 |
< |
Object w = e.exchange(v); |
45 |
< |
threadAssertEquals(w, two); |
46 |
< |
} catch(InterruptedException e){ |
47 |
< |
threadUnexpectedException(); |
48 |
< |
} |
49 |
< |
} |
50 |
< |
}); |
51 |
< |
try { |
52 |
< |
t1.start(); |
53 |
< |
t2.start(); |
54 |
< |
t1.join(); |
55 |
< |
t2.join(); |
56 |
< |
} catch(InterruptedException ex) { |
57 |
< |
unexpectedException(); |
58 |
< |
} |
32 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
33 |
> |
public void realRun() throws InterruptedException { |
34 |
> |
assertSame(one, e.exchange(two)); |
35 |
> |
assertSame(two, e.exchange(one)); |
36 |
> |
}}); |
37 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
38 |
> |
public void realRun() throws InterruptedException { |
39 |
> |
assertSame(two, e.exchange(one)); |
40 |
> |
assertSame(one, e.exchange(two)); |
41 |
> |
}}); |
42 |
> |
|
43 |
> |
awaitTermination(t1); |
44 |
> |
awaitTermination(t2); |
45 |
|
} |
46 |
|
|
47 |
|
/** |
49 |
|
*/ |
50 |
|
public void testTimedExchange() { |
51 |
|
final Exchanger e = new Exchanger(); |
52 |
< |
Thread t1 = new Thread(new Runnable(){ |
53 |
< |
public void run(){ |
54 |
< |
try { |
55 |
< |
Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
56 |
< |
threadAssertEquals(v, two); |
57 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
58 |
< |
threadAssertEquals(w, one); |
59 |
< |
} catch(InterruptedException e){ |
60 |
< |
threadUnexpectedException(); |
61 |
< |
} catch(TimeoutException toe) { |
62 |
< |
threadUnexpectedException(); |
63 |
< |
} |
64 |
< |
} |
79 |
< |
}); |
80 |
< |
Thread t2 = new Thread(new Runnable(){ |
81 |
< |
public void run(){ |
82 |
< |
try { |
83 |
< |
Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
84 |
< |
threadAssertEquals(v, one); |
85 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
86 |
< |
threadAssertEquals(w, two); |
87 |
< |
} catch(InterruptedException e){ |
88 |
< |
threadUnexpectedException(); |
89 |
< |
} catch(TimeoutException toe) { |
90 |
< |
threadUnexpectedException(); |
91 |
< |
} |
92 |
< |
} |
93 |
< |
}); |
94 |
< |
try { |
95 |
< |
t1.start(); |
96 |
< |
t2.start(); |
97 |
< |
t1.join(); |
98 |
< |
t2.join(); |
99 |
< |
} catch(InterruptedException ex) { |
100 |
< |
unexpectedException(); |
101 |
< |
} |
52 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
53 |
> |
public void realRun() throws Exception { |
54 |
> |
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
55 |
> |
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
56 |
> |
}}); |
57 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
58 |
> |
public void realRun() throws Exception { |
59 |
> |
assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); |
60 |
> |
assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); |
61 |
> |
}}); |
62 |
> |
|
63 |
> |
awaitTermination(t1); |
64 |
> |
awaitTermination(t2); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
|
* interrupt during wait for exchange throws IE |
69 |
|
*/ |
70 |
< |
public void testExchange_InterruptedException(){ |
70 |
> |
public void testExchange_InterruptedException() { |
71 |
|
final Exchanger e = new Exchanger(); |
72 |
< |
Thread t = new Thread(new Runnable() { |
73 |
< |
public void run(){ |
74 |
< |
try { |
75 |
< |
e.exchange(one); |
76 |
< |
threadShouldThrow(); |
77 |
< |
} catch(InterruptedException success){ |
78 |
< |
} |
79 |
< |
} |
80 |
< |
}); |
81 |
< |
try { |
119 |
< |
t.start(); |
120 |
< |
Thread.sleep(SHORT_DELAY_MS); |
121 |
< |
t.interrupt(); |
122 |
< |
t.join(); |
123 |
< |
} catch(InterruptedException ex) { |
124 |
< |
unexpectedException(); |
125 |
< |
} |
72 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
73 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
74 |
> |
public void realRun() throws InterruptedException { |
75 |
> |
threadStarted.countDown(); |
76 |
> |
e.exchange(one); |
77 |
> |
}}); |
78 |
> |
|
79 |
> |
await(threadStarted); |
80 |
> |
t.interrupt(); |
81 |
> |
awaitTermination(t); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
|
* interrupt during wait for timed exchange throws IE |
86 |
|
*/ |
87 |
< |
public void testTimedExchange_InterruptedException(){ |
87 |
> |
public void testTimedExchange_InterruptedException() { |
88 |
|
final Exchanger e = new Exchanger(); |
89 |
< |
Thread t = new Thread(new Runnable() { |
90 |
< |
public void run(){ |
91 |
< |
try { |
92 |
< |
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
93 |
< |
threadShouldThrow(); |
94 |
< |
} catch(InterruptedException success){ |
95 |
< |
} catch(Exception e2){ |
96 |
< |
threadFail("should throw IE"); |
97 |
< |
} |
98 |
< |
} |
143 |
< |
}); |
144 |
< |
try { |
145 |
< |
t.start(); |
146 |
< |
t.interrupt(); |
147 |
< |
t.join(); |
148 |
< |
} catch(InterruptedException ex){ |
149 |
< |
unexpectedException(); |
150 |
< |
} |
89 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
90 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
91 |
> |
public void realRun() throws Exception { |
92 |
> |
threadStarted.countDown(); |
93 |
> |
e.exchange(null, LONG_DELAY_MS, MILLISECONDS); |
94 |
> |
}}); |
95 |
> |
|
96 |
> |
await(threadStarted); |
97 |
> |
t.interrupt(); |
98 |
> |
awaitTermination(t); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* timeout during wait for timed exchange throws TOE |
103 |
< |
*/ |
104 |
< |
public void testExchange_TimeOutException(){ |
105 |
< |
final Exchanger e = new Exchanger(); |
106 |
< |
Thread t = new Thread(new Runnable() { |
107 |
< |
public void run(){ |
108 |
< |
try { |
109 |
< |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
110 |
< |
threadShouldThrow(); |
111 |
< |
} catch(TimeoutException success){ |
112 |
< |
} catch(InterruptedException e2){ |
113 |
< |
threadFail("should throw TOE"); |
114 |
< |
} |
115 |
< |
} |
116 |
< |
}); |
169 |
< |
try { |
170 |
< |
t.start(); |
171 |
< |
t.join(); |
172 |
< |
} catch(InterruptedException ex){ |
173 |
< |
unexpectedException(); |
174 |
< |
} |
102 |
> |
* timeout during wait for timed exchange throws TimeoutException |
103 |
> |
*/ |
104 |
> |
public void testExchange_TimeoutException() { |
105 |
> |
final Exchanger e = new Exchanger(); |
106 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
107 |
> |
public void realRun() throws Exception { |
108 |
> |
long startTime = System.nanoTime(); |
109 |
> |
try { |
110 |
> |
e.exchange(null, timeoutMillis(), MILLISECONDS); |
111 |
> |
shouldThrow(); |
112 |
> |
} catch (TimeoutException success) {} |
113 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
114 |
> |
}}); |
115 |
> |
|
116 |
> |
awaitTermination(t); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
121 |
|
*/ |
122 |
|
public void testReplacementAfterExchange() { |
123 |
|
final Exchanger e = new Exchanger(); |
124 |
< |
Thread t1 = new Thread(new Runnable(){ |
125 |
< |
public void run(){ |
126 |
< |
try { |
127 |
< |
Object v = e.exchange(one); |
128 |
< |
threadAssertEquals(v, two); |
129 |
< |
Object w = e.exchange(v); |
130 |
< |
threadShouldThrow(); |
131 |
< |
} catch(InterruptedException success){ |
132 |
< |
} |
133 |
< |
} |
134 |
< |
}); |
135 |
< |
Thread t2 = new Thread(new Runnable(){ |
136 |
< |
public void run(){ |
137 |
< |
try { |
138 |
< |
Object v = e.exchange(two); |
139 |
< |
threadAssertEquals(v, one); |
140 |
< |
Thread.sleep(SMALL_DELAY_MS); |
141 |
< |
Object w = e.exchange(v); |
142 |
< |
threadAssertEquals(w, three); |
143 |
< |
} catch(InterruptedException e){ |
144 |
< |
threadUnexpectedException(); |
145 |
< |
} |
146 |
< |
} |
147 |
< |
}); |
148 |
< |
Thread t3 = new Thread(new Runnable(){ |
149 |
< |
public void run(){ |
150 |
< |
try { |
209 |
< |
Thread.sleep(SMALL_DELAY_MS); |
210 |
< |
Object w = e.exchange(three); |
211 |
< |
threadAssertEquals(w, one); |
212 |
< |
} catch(InterruptedException e){ |
213 |
< |
threadUnexpectedException(); |
214 |
< |
} |
215 |
< |
} |
216 |
< |
}); |
217 |
< |
|
218 |
< |
try { |
219 |
< |
t1.start(); |
220 |
< |
t2.start(); |
221 |
< |
t3.start(); |
222 |
< |
Thread.sleep(SHORT_DELAY_MS); |
223 |
< |
t1.interrupt(); |
224 |
< |
t1.join(); |
225 |
< |
t2.join(); |
226 |
< |
t3.join(); |
227 |
< |
} catch(InterruptedException ex) { |
228 |
< |
unexpectedException(); |
229 |
< |
} |
124 |
> |
final CountDownLatch exchanged = new CountDownLatch(2); |
125 |
> |
final CountDownLatch interrupted = new CountDownLatch(1); |
126 |
> |
Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { |
127 |
> |
public void realRun() throws InterruptedException { |
128 |
> |
assertSame(two, e.exchange(one)); |
129 |
> |
exchanged.countDown(); |
130 |
> |
e.exchange(two); |
131 |
> |
}}); |
132 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
133 |
> |
public void realRun() throws InterruptedException { |
134 |
> |
assertSame(one, e.exchange(two)); |
135 |
> |
exchanged.countDown(); |
136 |
> |
interrupted.await(); |
137 |
> |
assertSame(three, e.exchange(one)); |
138 |
> |
}}); |
139 |
> |
Thread t3 = newStartedThread(new CheckedRunnable() { |
140 |
> |
public void realRun() throws InterruptedException { |
141 |
> |
interrupted.await(); |
142 |
> |
assertSame(one, e.exchange(three)); |
143 |
> |
}}); |
144 |
> |
|
145 |
> |
await(exchanged); |
146 |
> |
t1.interrupt(); |
147 |
> |
awaitTermination(t1); |
148 |
> |
interrupted.countDown(); |
149 |
> |
awaitTermination(t2); |
150 |
> |
awaitTermination(t3); |
151 |
|
} |
152 |
|
|
153 |
|
} |