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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
12 |
|
import java.util.concurrent.locks.*; |
13 |
|
import java.util.concurrent.atomic.*; |
14 |
|
|
15 |
< |
public class CyclicBarrierTest extends JSR166TestCase{ |
15 |
> |
public class CyclicBarrierTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(CyclicBarrierTest.class); |
24 |
|
private class MyAction implements Runnable { |
25 |
|
public void run() { ++countAction; } |
26 |
|
} |
27 |
< |
|
27 |
> |
|
28 |
|
/** |
29 |
|
* Creating with negative parties throws IAE |
30 |
|
*/ |
32 |
|
try { |
33 |
|
new CyclicBarrier(-1, (Runnable)null); |
34 |
|
shouldThrow(); |
35 |
< |
} catch(IllegalArgumentException e){} |
35 |
> |
} catch (IllegalArgumentException e) {} |
36 |
|
} |
37 |
|
|
38 |
|
/** |
42 |
|
try { |
43 |
|
new CyclicBarrier(-1); |
44 |
|
shouldThrow(); |
45 |
< |
} catch(IllegalArgumentException e){} |
45 |
> |
} catch (IllegalArgumentException e) {} |
46 |
|
} |
47 |
|
|
48 |
|
/** |
66 |
|
b.await(); |
67 |
|
assertEquals(0, b.getNumberWaiting()); |
68 |
|
} |
69 |
< |
catch(Exception e) { |
69 |
> |
catch (Exception e) { |
70 |
|
unexpectedException(); |
71 |
|
} |
72 |
|
} |
73 |
< |
|
73 |
> |
|
74 |
|
/** |
75 |
|
* The supplied barrier action is run at barrier |
76 |
|
*/ |
85 |
|
assertEquals(0, b.getNumberWaiting()); |
86 |
|
assertEquals(countAction, 2); |
87 |
|
} |
88 |
< |
catch(Exception e) { |
88 |
> |
catch (Exception e) { |
89 |
|
unexpectedException(); |
90 |
|
} |
91 |
|
} |
102 |
|
b.await(); |
103 |
|
b.await(); |
104 |
|
b.await(); |
105 |
< |
} catch(Exception e){ |
105 |
> |
} catch (Exception e) { |
106 |
|
threadUnexpectedException(); |
107 |
|
}}}); |
108 |
|
|
113 |
|
b.await(); |
114 |
|
b.await(); |
115 |
|
t.join(); |
116 |
< |
} catch(Exception e){ |
116 |
> |
} catch (Exception e) { |
117 |
|
unexpectedException(); |
118 |
|
} |
119 |
|
} |
130 |
|
try { |
131 |
|
c.await(); |
132 |
|
threadShouldThrow(); |
133 |
< |
} catch(InterruptedException success){} |
134 |
< |
catch(Exception b){ |
133 |
> |
} catch (InterruptedException success) {} |
134 |
> |
catch (Exception b) { |
135 |
|
threadUnexpectedException(); |
136 |
|
} |
137 |
|
} |
140 |
|
public void run() { |
141 |
|
try { |
142 |
|
c.await(); |
143 |
< |
threadShouldThrow(); |
144 |
< |
} catch(BrokenBarrierException success){ |
145 |
< |
} catch(Exception i){ |
143 |
> |
threadShouldThrow(); |
144 |
> |
} catch (BrokenBarrierException success) { |
145 |
> |
} catch (Exception i) { |
146 |
|
threadUnexpectedException(); |
147 |
|
} |
148 |
|
} |
152 |
|
t2.start(); |
153 |
|
Thread.sleep(SHORT_DELAY_MS); |
154 |
|
t1.interrupt(); |
155 |
< |
t1.join(); |
155 |
> |
t1.join(); |
156 |
|
t2.join(); |
157 |
< |
} catch(InterruptedException e){ |
157 |
> |
} catch (InterruptedException e) { |
158 |
|
unexpectedException(); |
159 |
|
} |
160 |
|
} |
170 |
|
try { |
171 |
|
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
172 |
|
threadShouldThrow(); |
173 |
< |
} catch(InterruptedException success){ |
174 |
< |
} catch(Exception b){ |
173 |
> |
} catch (InterruptedException success) { |
174 |
> |
} catch (Exception b) { |
175 |
|
threadUnexpectedException(); |
176 |
|
} |
177 |
|
} |
180 |
|
public void run() { |
181 |
|
try { |
182 |
|
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
183 |
< |
threadShouldThrow(); |
184 |
< |
} catch(BrokenBarrierException success){ |
185 |
< |
} catch(Exception i){ |
183 |
> |
threadShouldThrow(); |
184 |
> |
} catch (BrokenBarrierException success) { |
185 |
> |
} catch (Exception i) { |
186 |
|
threadUnexpectedException(); |
187 |
|
} |
188 |
|
} |
192 |
|
t2.start(); |
193 |
|
Thread.sleep(SHORT_DELAY_MS); |
194 |
|
t1.interrupt(); |
195 |
< |
t1.join(); |
195 |
> |
t1.join(); |
196 |
|
t2.join(); |
197 |
< |
} catch(InterruptedException e){ |
197 |
> |
} catch (InterruptedException e) { |
198 |
|
unexpectedException(); |
199 |
|
} |
200 |
|
} |
201 |
< |
|
201 |
> |
|
202 |
|
/** |
203 |
|
* A timeout in timed await throws TimeoutException |
204 |
|
*/ |
209 |
|
try { |
210 |
|
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
211 |
|
threadShouldThrow(); |
212 |
< |
} catch(TimeoutException success){ |
213 |
< |
} catch(Exception b){ |
212 |
> |
} catch (TimeoutException success) { |
213 |
> |
} catch (Exception b) { |
214 |
|
threadUnexpectedException(); |
215 |
< |
|
215 |
> |
|
216 |
|
} |
217 |
|
} |
218 |
|
}); |
219 |
|
try { |
220 |
|
t.start(); |
221 |
< |
t.join(); |
222 |
< |
} catch(InterruptedException e){ |
221 |
> |
t.join(); |
222 |
> |
} catch (InterruptedException e) { |
223 |
|
unexpectedException(); |
224 |
|
} |
225 |
|
} |
235 |
|
try { |
236 |
|
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
237 |
|
threadShouldThrow(); |
238 |
< |
} catch(TimeoutException success){ |
239 |
< |
} catch(Exception b){ |
238 |
> |
} catch (TimeoutException success) { |
239 |
> |
} catch (Exception b) { |
240 |
|
threadUnexpectedException(); |
241 |
|
} |
242 |
|
} |
245 |
|
public void run() { |
246 |
|
try { |
247 |
|
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
248 |
< |
threadShouldThrow(); |
249 |
< |
} catch(BrokenBarrierException success){ |
250 |
< |
} catch(Exception i){ |
248 |
> |
threadShouldThrow(); |
249 |
> |
} catch (BrokenBarrierException success) { |
250 |
> |
} catch (Exception i) { |
251 |
|
threadUnexpectedException(); |
252 |
|
} |
253 |
|
} |
255 |
|
try { |
256 |
|
t1.start(); |
257 |
|
t2.start(); |
258 |
< |
t1.join(); |
258 |
> |
t1.join(); |
259 |
|
t2.join(); |
260 |
< |
} catch(InterruptedException e){ |
260 |
> |
} catch (InterruptedException e) { |
261 |
|
unexpectedException(); |
262 |
|
} |
263 |
|
} |
273 |
|
try { |
274 |
|
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
275 |
|
threadShouldThrow(); |
276 |
< |
} catch(TimeoutException success){ |
277 |
< |
} catch(Exception b){ |
276 |
> |
} catch (TimeoutException success) { |
277 |
> |
} catch (Exception b) { |
278 |
|
threadUnexpectedException(); |
279 |
|
} |
280 |
|
} |
283 |
|
public void run() { |
284 |
|
try { |
285 |
|
c.await(); |
286 |
< |
threadShouldThrow(); |
287 |
< |
} catch(BrokenBarrierException success){ |
288 |
< |
} catch(Exception i){ |
286 |
> |
threadShouldThrow(); |
287 |
> |
} catch (BrokenBarrierException success) { |
288 |
> |
} catch (Exception i) { |
289 |
|
threadUnexpectedException(); |
290 |
|
} |
291 |
|
} |
293 |
|
try { |
294 |
|
t1.start(); |
295 |
|
t2.start(); |
296 |
< |
t1.join(); |
296 |
> |
t1.join(); |
297 |
|
t2.join(); |
298 |
< |
} catch(InterruptedException e){ |
298 |
> |
} catch (InterruptedException e) { |
299 |
|
unexpectedException(); |
300 |
|
} |
301 |
|
} |
302 |
< |
|
302 |
> |
|
303 |
|
/** |
304 |
|
* A reset of an active barrier causes waiting threads to throw |
305 |
|
* BrokenBarrierException |
311 |
|
try { |
312 |
|
c.await(); |
313 |
|
threadShouldThrow(); |
314 |
< |
} catch(BrokenBarrierException success){} |
315 |
< |
catch(Exception b){ |
314 |
> |
} catch (BrokenBarrierException success) {} |
315 |
> |
catch (Exception b) { |
316 |
|
threadUnexpectedException(); |
317 |
|
} |
318 |
|
} |
321 |
|
public void run() { |
322 |
|
try { |
323 |
|
c.await(); |
324 |
< |
threadShouldThrow(); |
325 |
< |
} catch(BrokenBarrierException success){ |
326 |
< |
} catch(Exception i){ |
324 |
> |
threadShouldThrow(); |
325 |
> |
} catch (BrokenBarrierException success) { |
326 |
> |
} catch (Exception i) { |
327 |
|
threadUnexpectedException(); |
328 |
|
} |
329 |
|
} |
333 |
|
t2.start(); |
334 |
|
Thread.sleep(SHORT_DELAY_MS); |
335 |
|
c.reset(); |
336 |
< |
t1.join(); |
336 |
> |
t1.join(); |
337 |
|
t2.join(); |
338 |
< |
} catch(InterruptedException e){ |
338 |
> |
} catch (InterruptedException e) { |
339 |
|
unexpectedException(); |
340 |
|
} |
341 |
|
} |
350 |
|
public void run() { |
351 |
|
try { |
352 |
|
c.await(); |
353 |
< |
} catch(Exception b){ |
353 |
> |
} catch (Exception b) { |
354 |
|
threadUnexpectedException(); |
355 |
|
} |
356 |
|
} |
359 |
|
public void run() { |
360 |
|
try { |
361 |
|
c.await(); |
362 |
< |
} catch(Exception i){ |
362 |
> |
} catch (Exception i) { |
363 |
|
threadUnexpectedException(); |
364 |
|
} |
365 |
|
} |
369 |
|
t1.start(); |
370 |
|
t2.start(); |
371 |
|
c.await(); |
372 |
< |
t1.join(); |
372 |
> |
t1.join(); |
373 |
|
t2.join(); |
374 |
< |
} catch(Exception e){ |
374 |
> |
} catch (Exception e) { |
375 |
|
unexpectedException(); |
376 |
|
} |
377 |
|
} |
389 |
|
try { |
390 |
|
while (c.isBroken()) |
391 |
|
c.reset(); |
392 |
< |
|
392 |
> |
|
393 |
|
c.await(); |
394 |
|
threadFail("await should not return"); |
395 |
|
} |
400 |
|
} |
401 |
|
} |
402 |
|
}; |
403 |
< |
|
403 |
> |
|
404 |
|
t.start(); |
405 |
< |
for( int i = 0; i < 4; i++) { |
405 |
> |
for ( int i = 0; i < 4; i++) { |
406 |
|
Thread.sleep(SHORT_DELAY_MS); |
407 |
|
t.interrupt(); |
408 |
|
} |
425 |
|
Thread t1 = new Thread(new Runnable() { |
426 |
|
public void run() { |
427 |
|
try { start.await(); } |
428 |
< |
catch (Exception ie) { |
429 |
< |
threadFail("start barrier"); |
428 |
> |
catch (Exception ie) { |
429 |
> |
threadFail("start barrier"); |
430 |
|
} |
431 |
|
try { barrier.await(); } |
432 |
< |
catch (Throwable thrown) { |
433 |
< |
unexpectedException(); |
432 |
> |
catch (Throwable thrown) { |
433 |
> |
unexpectedException(); |
434 |
|
}}}); |
435 |
< |
|
435 |
> |
|
436 |
|
Thread t2 = new Thread(new Runnable() { |
437 |
|
public void run() { |
438 |
|
try { start.await(); } |
439 |
< |
catch (Exception ie) { |
440 |
< |
threadFail("start barrier"); |
439 |
> |
catch (Exception ie) { |
440 |
> |
threadFail("start barrier"); |
441 |
|
} |
442 |
|
try { barrier.await(); } |
443 |
< |
catch (Throwable thrown) { |
444 |
< |
unexpectedException(); |
443 |
> |
catch (Throwable thrown) { |
444 |
> |
unexpectedException(); |
445 |
|
}}}); |
446 |
< |
|
447 |
< |
|
446 |
> |
|
447 |
> |
|
448 |
|
t1.start(); |
449 |
|
t2.start(); |
450 |
|
try { start.await(); } |
463 |
|
unexpectedException(); |
464 |
|
} |
465 |
|
} |
466 |
< |
|
466 |
> |
|
467 |
|
/** |
468 |
|
* Reset of a barrier after interruption reinitializes it. |
469 |
|
*/ |
475 |
|
Thread t1 = new Thread(new Runnable() { |
476 |
|
public void run() { |
477 |
|
try { start.await(); } |
478 |
< |
catch (Exception ie) { |
479 |
< |
threadFail("start barrier"); |
478 |
> |
catch (Exception ie) { |
479 |
> |
threadFail("start barrier"); |
480 |
|
} |
481 |
|
try { barrier.await(); } |
482 |
< |
catch(InterruptedException ok) {} |
483 |
< |
catch (Throwable thrown) { |
484 |
< |
unexpectedException(); |
482 |
> |
catch (InterruptedException ok) {} |
483 |
> |
catch (Throwable thrown) { |
484 |
> |
unexpectedException(); |
485 |
|
}}}); |
486 |
< |
|
486 |
> |
|
487 |
|
Thread t2 = new Thread(new Runnable() { |
488 |
|
public void run() { |
489 |
|
try { start.await(); } |
490 |
< |
catch (Exception ie) { |
491 |
< |
threadFail("start barrier"); |
490 |
> |
catch (Exception ie) { |
491 |
> |
threadFail("start barrier"); |
492 |
|
} |
493 |
|
try { barrier.await(); } |
494 |
< |
catch(BrokenBarrierException ok) {} |
495 |
< |
catch (Throwable thrown) { |
496 |
< |
unexpectedException(); |
494 |
> |
catch (BrokenBarrierException ok) {} |
495 |
> |
catch (Throwable thrown) { |
496 |
> |
unexpectedException(); |
497 |
|
}}}); |
498 |
< |
|
498 |
> |
|
499 |
|
t1.start(); |
500 |
|
t2.start(); |
501 |
|
try { start.await(); } |
514 |
|
unexpectedException(); |
515 |
|
} |
516 |
|
} |
517 |
< |
|
517 |
> |
|
518 |
|
/** |
519 |
|
* Reset of a barrier after timeout reinitializes it. |
520 |
|
*/ |
526 |
|
Thread t1 = new Thread(new Runnable() { |
527 |
|
public void run() { |
528 |
|
try { start.await(); } |
529 |
< |
catch (Exception ie) { |
530 |
< |
threadFail("start barrier"); |
529 |
> |
catch (Exception ie) { |
530 |
> |
threadFail("start barrier"); |
531 |
|
} |
532 |
|
try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } |
533 |
< |
catch(TimeoutException ok) {} |
534 |
< |
catch (Throwable thrown) { |
535 |
< |
unexpectedException(); |
533 |
> |
catch (TimeoutException ok) {} |
534 |
> |
catch (Throwable thrown) { |
535 |
> |
unexpectedException(); |
536 |
|
}}}); |
537 |
< |
|
537 |
> |
|
538 |
|
Thread t2 = new Thread(new Runnable() { |
539 |
|
public void run() { |
540 |
|
try { start.await(); } |
541 |
< |
catch (Exception ie) { |
542 |
< |
threadFail("start barrier"); |
541 |
> |
catch (Exception ie) { |
542 |
> |
threadFail("start barrier"); |
543 |
|
} |
544 |
|
try { barrier.await(); } |
545 |
< |
catch(BrokenBarrierException ok) {} |
546 |
< |
catch (Throwable thrown) { |
547 |
< |
unexpectedException(); |
545 |
> |
catch (BrokenBarrierException ok) {} |
546 |
> |
catch (Throwable thrown) { |
547 |
> |
unexpectedException(); |
548 |
|
}}}); |
549 |
< |
|
549 |
> |
|
550 |
|
t1.start(); |
551 |
|
t2.start(); |
552 |
|
try { start.await(); } |
565 |
|
} |
566 |
|
} |
567 |
|
|
568 |
< |
|
568 |
> |
|
569 |
|
/** |
570 |
|
* Reset of a barrier after a failed command reinitializes it. |
571 |
|
*/ |
572 |
|
public void testResetAfterCommandException() { |
573 |
|
try { |
574 |
|
final CyclicBarrier start = new CyclicBarrier(3); |
575 |
< |
final CyclicBarrier barrier = |
575 |
> |
final CyclicBarrier barrier = |
576 |
|
new CyclicBarrier(3, new Runnable() { |
577 |
< |
public void run() { |
577 |
> |
public void run() { |
578 |
|
throw new NullPointerException(); }}); |
579 |
|
for (int i = 0; i < 2; i++) { |
580 |
|
Thread t1 = new Thread(new Runnable() { |
581 |
|
public void run() { |
582 |
|
try { start.await(); } |
583 |
< |
catch (Exception ie) { |
584 |
< |
threadFail("start barrier"); |
583 |
> |
catch (Exception ie) { |
584 |
> |
threadFail("start barrier"); |
585 |
|
} |
586 |
|
try { barrier.await(); } |
587 |
< |
catch(BrokenBarrierException ok) {} |
588 |
< |
catch (Throwable thrown) { |
589 |
< |
unexpectedException(); |
587 |
> |
catch (BrokenBarrierException ok) {} |
588 |
> |
catch (Throwable thrown) { |
589 |
> |
unexpectedException(); |
590 |
|
}}}); |
591 |
< |
|
591 |
> |
|
592 |
|
Thread t2 = new Thread(new Runnable() { |
593 |
|
public void run() { |
594 |
|
try { start.await(); } |
595 |
< |
catch (Exception ie) { |
596 |
< |
threadFail("start barrier"); |
595 |
> |
catch (Exception ie) { |
596 |
> |
threadFail("start barrier"); |
597 |
|
} |
598 |
|
try { barrier.await(); } |
599 |
< |
catch(BrokenBarrierException ok) {} |
600 |
< |
catch (Throwable thrown) { |
601 |
< |
unexpectedException(); |
599 |
> |
catch (BrokenBarrierException ok) {} |
600 |
> |
catch (Throwable thrown) { |
601 |
> |
unexpectedException(); |
602 |
|
}}}); |
603 |
< |
|
603 |
> |
|
604 |
|
t1.start(); |
605 |
|
t2.start(); |
606 |
|
try { start.await(); } |