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 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
jsr166 |
1.9 |
* 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 |
dl |
1.8 |
import java.util.concurrent.locks.*; |
13 |
|
|
import java.util.concurrent.atomic.*; |
14 |
jsr166 |
1.13 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
dl |
1.1 |
|
16 |
jsr166 |
1.11 |
public class CyclicBarrierTest extends JSR166TestCase { |
17 |
dl |
1.1 |
public static void main(String[] args) { |
18 |
jsr166 |
1.15 |
junit.textui.TestRunner.run(suite()); |
19 |
dl |
1.1 |
} |
20 |
|
|
public static Test suite() { |
21 |
jsr166 |
1.12 |
return new TestSuite(CyclicBarrierTest.class); |
22 |
dl |
1.1 |
} |
23 |
dl |
1.3 |
|
24 |
|
|
private volatile int countAction; |
25 |
|
|
private class MyAction implements Runnable { |
26 |
|
|
public void run() { ++countAction; } |
27 |
|
|
} |
28 |
jsr166 |
1.9 |
|
29 |
dl |
1.3 |
/** |
30 |
dl |
1.4 |
* Creating with negative parties throws IAE |
31 |
dl |
1.3 |
*/ |
32 |
|
|
public void testConstructor1() { |
33 |
|
|
try { |
34 |
dl |
1.1 |
new CyclicBarrier(-1, (Runnable)null); |
35 |
dl |
1.3 |
shouldThrow(); |
36 |
jsr166 |
1.14 |
} catch (IllegalArgumentException success) {} |
37 |
dl |
1.1 |
} |
38 |
|
|
|
39 |
dl |
1.3 |
/** |
40 |
dl |
1.4 |
* Creating with negative parties and no action throws IAE |
41 |
dl |
1.3 |
*/ |
42 |
|
|
public void testConstructor2() { |
43 |
|
|
try { |
44 |
dl |
1.1 |
new CyclicBarrier(-1); |
45 |
dl |
1.3 |
shouldThrow(); |
46 |
jsr166 |
1.14 |
} catch (IllegalArgumentException success) {} |
47 |
dl |
1.1 |
} |
48 |
|
|
|
49 |
dl |
1.3 |
/** |
50 |
dl |
1.4 |
* getParties returns the number of parties given in constructor |
51 |
dl |
1.3 |
*/ |
52 |
dl |
1.4 |
public void testGetParties() { |
53 |
dl |
1.1 |
CyclicBarrier b = new CyclicBarrier(2); |
54 |
jsr166 |
1.12 |
assertEquals(2, b.getParties()); |
55 |
dl |
1.1 |
assertEquals(0, b.getNumberWaiting()); |
56 |
|
|
} |
57 |
|
|
|
58 |
dl |
1.3 |
/** |
59 |
dl |
1.5 |
* A 1-party barrier triggers after single await |
60 |
dl |
1.3 |
*/ |
61 |
jsr166 |
1.14 |
public void testSingleParty() throws Exception { |
62 |
|
|
CyclicBarrier b = new CyclicBarrier(1); |
63 |
|
|
assertEquals(1, b.getParties()); |
64 |
|
|
assertEquals(0, b.getNumberWaiting()); |
65 |
|
|
b.await(); |
66 |
|
|
b.await(); |
67 |
|
|
assertEquals(0, b.getNumberWaiting()); |
68 |
dl |
1.1 |
} |
69 |
jsr166 |
1.9 |
|
70 |
dl |
1.3 |
/** |
71 |
dl |
1.4 |
* The supplied barrier action is run at barrier |
72 |
dl |
1.3 |
*/ |
73 |
jsr166 |
1.14 |
public void testBarrierAction() throws Exception { |
74 |
|
|
countAction = 0; |
75 |
|
|
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
76 |
|
|
assertEquals(1, b.getParties()); |
77 |
|
|
assertEquals(0, b.getNumberWaiting()); |
78 |
|
|
b.await(); |
79 |
|
|
b.await(); |
80 |
|
|
assertEquals(0, b.getNumberWaiting()); |
81 |
|
|
assertEquals(countAction, 2); |
82 |
dl |
1.1 |
} |
83 |
|
|
|
84 |
dl |
1.3 |
/** |
85 |
dl |
1.5 |
* A 2-party/thread barrier triggers after both threads invoke await |
86 |
dl |
1.3 |
*/ |
87 |
jsr166 |
1.14 |
public void testTwoParties() throws Exception { |
88 |
dl |
1.1 |
final CyclicBarrier b = new CyclicBarrier(2); |
89 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
90 |
|
|
public void realRun() throws Exception { |
91 |
|
|
b.await(); |
92 |
|
|
b.await(); |
93 |
|
|
b.await(); |
94 |
|
|
b.await(); |
95 |
|
|
}}); |
96 |
|
|
|
97 |
|
|
t.start(); |
98 |
|
|
b.await(); |
99 |
|
|
b.await(); |
100 |
|
|
b.await(); |
101 |
|
|
b.await(); |
102 |
|
|
t.join(); |
103 |
dl |
1.1 |
} |
104 |
|
|
|
105 |
|
|
|
106 |
dl |
1.3 |
/** |
107 |
dl |
1.4 |
* An interruption in one party causes others waiting in await to |
108 |
|
|
* throw BrokenBarrierException |
109 |
dl |
1.3 |
*/ |
110 |
jsr166 |
1.14 |
public void testAwait1_Interrupted_BrokenBarrier() throws Exception { |
111 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(3); |
112 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
113 |
|
|
public void realRun() throws Exception { |
114 |
|
|
c.await(); |
115 |
|
|
}}; |
116 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
117 |
|
|
public void realRun() throws Exception { |
118 |
|
|
c.await(); |
119 |
|
|
}}; |
120 |
|
|
|
121 |
|
|
t1.start(); |
122 |
|
|
t2.start(); |
123 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
124 |
|
|
t1.interrupt(); |
125 |
|
|
t1.join(); |
126 |
|
|
t2.join(); |
127 |
dl |
1.1 |
} |
128 |
|
|
|
129 |
dl |
1.3 |
/** |
130 |
dl |
1.4 |
* An interruption in one party causes others waiting in timed await to |
131 |
|
|
* throw BrokenBarrierException |
132 |
dl |
1.3 |
*/ |
133 |
jsr166 |
1.14 |
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
134 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
135 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
136 |
|
|
public void realRun() throws Exception { |
137 |
|
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
138 |
|
|
}}; |
139 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
140 |
|
|
public void realRun() throws Exception { |
141 |
|
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
142 |
|
|
}}; |
143 |
|
|
|
144 |
|
|
t1.start(); |
145 |
|
|
t2.start(); |
146 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
147 |
|
|
t1.interrupt(); |
148 |
|
|
t1.join(); |
149 |
|
|
t2.join(); |
150 |
dl |
1.1 |
} |
151 |
jsr166 |
1.9 |
|
152 |
dl |
1.3 |
/** |
153 |
dl |
1.4 |
* A timeout in timed await throws TimeoutException |
154 |
dl |
1.3 |
*/ |
155 |
jsr166 |
1.14 |
public void testAwait3_TimeOutException() throws InterruptedException { |
156 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(2); |
157 |
jsr166 |
1.14 |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
158 |
|
|
public void realRun() throws Exception { |
159 |
|
|
c.await(SHORT_DELAY_MS, MILLISECONDS); |
160 |
|
|
}}; |
161 |
jsr166 |
1.9 |
|
162 |
jsr166 |
1.14 |
t.start(); |
163 |
|
|
t.join(); |
164 |
dl |
1.5 |
} |
165 |
|
|
|
166 |
|
|
/** |
167 |
|
|
* A timeout in one party causes others waiting in timed await to |
168 |
|
|
* throw BrokenBarrierException |
169 |
|
|
*/ |
170 |
jsr166 |
1.14 |
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
171 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
172 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
173 |
|
|
public void realRun() throws Exception { |
174 |
|
|
c.await(SHORT_DELAY_MS, MILLISECONDS); |
175 |
|
|
}}; |
176 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
177 |
|
|
public void realRun() throws Exception { |
178 |
|
|
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
179 |
|
|
}}; |
180 |
|
|
|
181 |
|
|
t1.start(); |
182 |
|
|
t2.start(); |
183 |
|
|
t1.join(); |
184 |
|
|
t2.join(); |
185 |
dl |
1.5 |
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* A timeout in one party causes others waiting in await to |
189 |
|
|
* throw BrokenBarrierException |
190 |
|
|
*/ |
191 |
jsr166 |
1.14 |
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
192 |
dl |
1.8 |
final CyclicBarrier c = new CyclicBarrier(3); |
193 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
194 |
|
|
public void realRun() throws Exception { |
195 |
|
|
c.await(SHORT_DELAY_MS, MILLISECONDS); |
196 |
|
|
}}; |
197 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
198 |
|
|
public void realRun() throws Exception { |
199 |
|
|
c.await(); |
200 |
|
|
}}; |
201 |
|
|
|
202 |
|
|
t1.start(); |
203 |
|
|
t2.start(); |
204 |
|
|
t1.join(); |
205 |
|
|
t2.join(); |
206 |
dl |
1.1 |
} |
207 |
jsr166 |
1.9 |
|
208 |
dl |
1.4 |
/** |
209 |
|
|
* A reset of an active barrier causes waiting threads to throw |
210 |
|
|
* BrokenBarrierException |
211 |
|
|
*/ |
212 |
jsr166 |
1.14 |
public void testReset_BrokenBarrier() throws InterruptedException { |
213 |
dl |
1.4 |
final CyclicBarrier c = new CyclicBarrier(3); |
214 |
jsr166 |
1.14 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
215 |
|
|
public void realRun() throws Exception { |
216 |
|
|
c.await(); |
217 |
|
|
}}; |
218 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
219 |
|
|
public void realRun() throws Exception { |
220 |
|
|
c.await(); |
221 |
|
|
}}; |
222 |
|
|
|
223 |
|
|
t1.start(); |
224 |
|
|
t2.start(); |
225 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
226 |
|
|
c.reset(); |
227 |
|
|
t1.join(); |
228 |
|
|
t2.join(); |
229 |
dl |
1.4 |
} |
230 |
|
|
|
231 |
|
|
/** |
232 |
|
|
* A reset before threads enter barrier does not throw |
233 |
|
|
* BrokenBarrierException |
234 |
|
|
*/ |
235 |
jsr166 |
1.14 |
public void testReset_NoBrokenBarrier() throws Exception { |
236 |
dl |
1.4 |
final CyclicBarrier c = new CyclicBarrier(3); |
237 |
jsr166 |
1.14 |
Thread t1 = new Thread(new CheckedRunnable() { |
238 |
|
|
public void realRun() throws Exception { |
239 |
|
|
c.await(); |
240 |
|
|
}}); |
241 |
|
|
Thread t2 = new Thread(new CheckedRunnable() { |
242 |
|
|
public void realRun() throws Exception { |
243 |
|
|
c.await(); |
244 |
|
|
}}); |
245 |
|
|
|
246 |
|
|
c.reset(); |
247 |
|
|
t1.start(); |
248 |
|
|
t2.start(); |
249 |
|
|
c.await(); |
250 |
|
|
t1.join(); |
251 |
|
|
t2.join(); |
252 |
dl |
1.4 |
} |
253 |
|
|
|
254 |
dl |
1.8 |
/** |
255 |
|
|
* All threads block while a barrier is broken. |
256 |
|
|
*/ |
257 |
jsr166 |
1.14 |
public void testReset_Leakage() throws InterruptedException { |
258 |
|
|
final CyclicBarrier c = new CyclicBarrier(2); |
259 |
|
|
final AtomicBoolean done = new AtomicBoolean(); |
260 |
|
|
Thread t = new Thread() { |
261 |
|
|
public void run() { |
262 |
|
|
while (!done.get()) { |
263 |
|
|
try { |
264 |
|
|
while (c.isBroken()) |
265 |
|
|
c.reset(); |
266 |
|
|
|
267 |
|
|
c.await(); |
268 |
|
|
threadFail("await should not return"); |
269 |
|
|
} |
270 |
|
|
catch (BrokenBarrierException e) { |
271 |
|
|
} |
272 |
|
|
catch (InterruptedException ie) { |
273 |
dl |
1.8 |
} |
274 |
|
|
} |
275 |
jsr166 |
1.14 |
} |
276 |
|
|
}; |
277 |
jsr166 |
1.9 |
|
278 |
jsr166 |
1.14 |
t.start(); |
279 |
|
|
for (int i = 0; i < 4; i++) { |
280 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
281 |
dl |
1.8 |
t.interrupt(); |
282 |
|
|
} |
283 |
jsr166 |
1.14 |
done.set(true); |
284 |
|
|
t.interrupt(); |
285 |
|
|
t.join(); |
286 |
dl |
1.8 |
} |
287 |
|
|
|
288 |
|
|
/** |
289 |
|
|
* Reset of a non-broken barrier does not break barrier |
290 |
|
|
*/ |
291 |
jsr166 |
1.14 |
public void testResetWithoutBreakage() throws Exception { |
292 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
293 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
294 |
|
|
for (int i = 0; i < 3; i++) { |
295 |
|
|
Thread t1 = new Thread(new CheckedRunnable() { |
296 |
|
|
public void realRun() throws Exception { |
297 |
|
|
start.await(); |
298 |
|
|
barrier.await(); |
299 |
|
|
}}); |
300 |
|
|
|
301 |
|
|
Thread t2 = new Thread(new CheckedRunnable() { |
302 |
|
|
public void realRun() throws Exception { |
303 |
|
|
start.await(); |
304 |
|
|
barrier.await(); |
305 |
|
|
}}); |
306 |
|
|
|
307 |
|
|
t1.start(); |
308 |
|
|
t2.start(); |
309 |
|
|
start.await(); |
310 |
|
|
barrier.await(); |
311 |
|
|
t1.join(); |
312 |
|
|
t2.join(); |
313 |
|
|
assertFalse(barrier.isBroken()); |
314 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
315 |
|
|
if (i == 1) barrier.reset(); |
316 |
|
|
assertFalse(barrier.isBroken()); |
317 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
318 |
dl |
1.8 |
} |
319 |
|
|
} |
320 |
jsr166 |
1.9 |
|
321 |
dl |
1.8 |
/** |
322 |
|
|
* Reset of a barrier after interruption reinitializes it. |
323 |
|
|
*/ |
324 |
jsr166 |
1.14 |
public void testResetAfterInterrupt() throws Exception { |
325 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
326 |
|
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
327 |
|
|
for (int i = 0; i < 2; i++) { |
328 |
|
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
329 |
|
|
public void realRun() throws Exception { |
330 |
|
|
start.await(); |
331 |
|
|
barrier.await(); |
332 |
|
|
}}; |
333 |
|
|
|
334 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
335 |
|
|
public void realRun() throws Exception { |
336 |
|
|
start.await(); |
337 |
|
|
barrier.await(); |
338 |
|
|
}}; |
339 |
|
|
|
340 |
|
|
t1.start(); |
341 |
|
|
t2.start(); |
342 |
|
|
start.await(); |
343 |
|
|
t1.interrupt(); |
344 |
|
|
t1.join(); |
345 |
|
|
t2.join(); |
346 |
|
|
assertTrue(barrier.isBroken()); |
347 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
348 |
|
|
barrier.reset(); |
349 |
|
|
assertFalse(barrier.isBroken()); |
350 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
351 |
dl |
1.8 |
} |
352 |
|
|
} |
353 |
jsr166 |
1.9 |
|
354 |
dl |
1.8 |
/** |
355 |
|
|
* Reset of a barrier after timeout reinitializes it. |
356 |
|
|
*/ |
357 |
jsr166 |
1.14 |
public void testResetAfterTimeout() throws Exception { |
358 |
jsr166 |
1.16 |
final CyclicBarrier start = new CyclicBarrier(2); |
359 |
jsr166 |
1.14 |
final CyclicBarrier barrier = new CyclicBarrier(3); |
360 |
|
|
for (int i = 0; i < 2; i++) { |
361 |
|
|
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
362 |
|
|
public void realRun() throws Exception { |
363 |
|
|
start.await(); |
364 |
jsr166 |
1.16 |
barrier.await(SHORT_DELAY_MS, MILLISECONDS); |
365 |
jsr166 |
1.14 |
}}; |
366 |
|
|
|
367 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
368 |
|
|
public void realRun() throws Exception { |
369 |
|
|
start.await(); |
370 |
|
|
barrier.await(); |
371 |
|
|
}}; |
372 |
|
|
|
373 |
|
|
t1.start(); |
374 |
|
|
t2.start(); |
375 |
|
|
t1.join(); |
376 |
|
|
t2.join(); |
377 |
|
|
assertTrue(barrier.isBroken()); |
378 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
379 |
|
|
barrier.reset(); |
380 |
|
|
assertFalse(barrier.isBroken()); |
381 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
382 |
dl |
1.8 |
} |
383 |
|
|
} |
384 |
|
|
|
385 |
jsr166 |
1.9 |
|
386 |
dl |
1.8 |
/** |
387 |
|
|
* Reset of a barrier after a failed command reinitializes it. |
388 |
|
|
*/ |
389 |
jsr166 |
1.14 |
public void testResetAfterCommandException() throws Exception { |
390 |
|
|
final CyclicBarrier start = new CyclicBarrier(3); |
391 |
|
|
final CyclicBarrier barrier = |
392 |
|
|
new CyclicBarrier(3, new Runnable() { |
393 |
|
|
public void run() { |
394 |
|
|
throw new NullPointerException(); }}); |
395 |
|
|
for (int i = 0; i < 2; i++) { |
396 |
|
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
397 |
|
|
public void realRun() throws Exception { |
398 |
|
|
start.await(); |
399 |
|
|
barrier.await(); |
400 |
|
|
}}; |
401 |
|
|
|
402 |
|
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
403 |
|
|
public void realRun() throws Exception { |
404 |
|
|
start.await(); |
405 |
|
|
barrier.await(); |
406 |
|
|
}}; |
407 |
|
|
|
408 |
|
|
t1.start(); |
409 |
|
|
t2.start(); |
410 |
|
|
start.await(); |
411 |
|
|
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
412 |
|
|
try { |
413 |
|
|
barrier.await(); |
414 |
|
|
shouldThrow(); |
415 |
|
|
} catch (NullPointerException success) {} |
416 |
|
|
t1.join(); |
417 |
|
|
t2.join(); |
418 |
|
|
assertTrue(barrier.isBroken()); |
419 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
420 |
|
|
barrier.reset(); |
421 |
|
|
assertFalse(barrier.isBroken()); |
422 |
|
|
assertEquals(0, barrier.getNumberWaiting()); |
423 |
dl |
1.8 |
} |
424 |
|
|
} |
425 |
dl |
1.1 |
} |