ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.8
Committed: Mon Apr 25 15:04:50 2005 UTC (19 years ago) by dl
Branch: MAIN
Changes since 1.7: +250 -3 lines
Log Message:
New tests for reset()

File Contents

# User Rev Content
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     * 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 dl 1.1
15 dl 1.2 public class CyclicBarrierTest extends JSR166TestCase{
16 dl 1.1 public static void main(String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19     public static Test suite() {
20     return new TestSuite(CyclicBarrierTest.class);
21     }
22 dl 1.3
23     private volatile int countAction;
24     private class MyAction implements Runnable {
25     public void run() { ++countAction; }
26     }
27 dl 1.1
28 dl 1.3 /**
29 dl 1.4 * Creating with negative parties throws IAE
30 dl 1.3 */
31     public void testConstructor1() {
32     try {
33 dl 1.1 new CyclicBarrier(-1, (Runnable)null);
34 dl 1.3 shouldThrow();
35 dl 1.1 } catch(IllegalArgumentException e){}
36     }
37    
38 dl 1.3 /**
39 dl 1.4 * Creating with negative parties and no action throws IAE
40 dl 1.3 */
41     public void testConstructor2() {
42     try {
43 dl 1.1 new CyclicBarrier(-1);
44 dl 1.3 shouldThrow();
45 dl 1.1 } catch(IllegalArgumentException e){}
46     }
47    
48 dl 1.3 /**
49 dl 1.4 * getParties returns the number of parties given in constructor
50 dl 1.3 */
51 dl 1.4 public void testGetParties() {
52 dl 1.1 CyclicBarrier b = new CyclicBarrier(2);
53     assertEquals(2, b.getParties());
54     assertEquals(0, b.getNumberWaiting());
55     }
56    
57 dl 1.3 /**
58 dl 1.5 * A 1-party barrier triggers after single await
59 dl 1.3 */
60 dl 1.1 public void testSingleParty() {
61     try {
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     }
69     catch(Exception e) {
70 dl 1.3 unexpectedException();
71 dl 1.1 }
72     }
73    
74 dl 1.3 /**
75 dl 1.4 * The supplied barrier action is run at barrier
76 dl 1.3 */
77 dl 1.1 public void testBarrierAction() {
78     try {
79     countAction = 0;
80     CyclicBarrier b = new CyclicBarrier(1, new MyAction());
81     assertEquals(1, b.getParties());
82     assertEquals(0, b.getNumberWaiting());
83     b.await();
84     b.await();
85     assertEquals(0, b.getNumberWaiting());
86     assertEquals(countAction, 2);
87     }
88     catch(Exception e) {
89 dl 1.3 unexpectedException();
90 dl 1.1 }
91     }
92    
93 dl 1.3 /**
94 dl 1.5 * A 2-party/thread barrier triggers after both threads invoke await
95 dl 1.3 */
96     public void testTwoParties() {
97 dl 1.1 final CyclicBarrier b = new CyclicBarrier(2);
98     Thread t = new Thread(new Runnable() {
99 dl 1.3 public void run() {
100 dl 1.1 try {
101     b.await();
102     b.await();
103     b.await();
104     b.await();
105     } catch(Exception e){
106 dl 1.3 threadUnexpectedException();
107 dl 1.1 }}});
108    
109     try {
110     t.start();
111     b.await();
112     b.await();
113     b.await();
114     b.await();
115     t.join();
116     } catch(Exception e){
117 dl 1.3 unexpectedException();
118 dl 1.1 }
119     }
120    
121    
122 dl 1.3 /**
123 dl 1.4 * An interruption in one party causes others waiting in await to
124     * throw BrokenBarrierException
125 dl 1.3 */
126     public void testAwait1_Interrupted_BrokenBarrier() {
127 dl 1.1 final CyclicBarrier c = new CyclicBarrier(3);
128     Thread t1 = new Thread(new Runnable() {
129 dl 1.3 public void run() {
130     try {
131 dl 1.1 c.await();
132 dl 1.3 threadShouldThrow();
133 dl 1.1 } catch(InterruptedException success){}
134     catch(Exception b){
135 dl 1.4 threadUnexpectedException();
136 dl 1.1 }
137     }
138     });
139 dl 1.3 Thread t2 = new Thread(new Runnable() {
140     public void run() {
141     try {
142 dl 1.1 c.await();
143 dl 1.3 threadShouldThrow();
144 dl 1.1 } catch(BrokenBarrierException success){
145     } catch(Exception i){
146 dl 1.4 threadUnexpectedException();
147 dl 1.1 }
148     }
149     });
150     try {
151     t1.start();
152     t2.start();
153     Thread.sleep(SHORT_DELAY_MS);
154     t1.interrupt();
155     t1.join();
156     t2.join();
157     } catch(InterruptedException e){
158 dl 1.3 unexpectedException();
159 dl 1.1 }
160     }
161    
162 dl 1.3 /**
163 dl 1.4 * An interruption in one party causes others waiting in timed await to
164     * throw BrokenBarrierException
165 dl 1.3 */
166     public void testAwait2_Interrupted_BrokenBarrier() {
167 dl 1.8 final CyclicBarrier c = new CyclicBarrier(3);
168 dl 1.1 Thread t1 = new Thread(new Runnable() {
169 dl 1.3 public void run() {
170     try {
171 dl 1.7 c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
172 dl 1.3 threadShouldThrow();
173 dl 1.1 } catch(InterruptedException success){
174     } catch(Exception b){
175 dl 1.4 threadUnexpectedException();
176 dl 1.1 }
177     }
178     });
179 dl 1.3 Thread t2 = new Thread(new Runnable() {
180     public void run() {
181     try {
182 dl 1.7 c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
183 dl 1.3 threadShouldThrow();
184 dl 1.1 } catch(BrokenBarrierException success){
185     } catch(Exception i){
186 dl 1.4 threadUnexpectedException();
187 dl 1.1 }
188     }
189     });
190     try {
191     t1.start();
192     t2.start();
193     Thread.sleep(SHORT_DELAY_MS);
194     t1.interrupt();
195     t1.join();
196     t2.join();
197     } catch(InterruptedException e){
198 dl 1.3 unexpectedException();
199 dl 1.1 }
200     }
201    
202 dl 1.3 /**
203 dl 1.4 * A timeout in timed await throws TimeoutException
204 dl 1.3 */
205     public void testAwait3_TimeOutException() {
206 dl 1.1 final CyclicBarrier c = new CyclicBarrier(2);
207     Thread t = new Thread(new Runnable() {
208 dl 1.3 public void run() {
209     try {
210 dl 1.1 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
211 dl 1.3 threadShouldThrow();
212 dl 1.1 } catch(TimeoutException success){
213     } catch(Exception b){
214 dl 1.4 threadUnexpectedException();
215 dl 1.1
216     }
217     }
218     });
219     try {
220     t.start();
221     t.join();
222 dl 1.5 } catch(InterruptedException e){
223     unexpectedException();
224     }
225     }
226    
227     /**
228     * A timeout in one party causes others waiting in timed await to
229     * throw BrokenBarrierException
230     */
231     public void testAwait4_Timeout_BrokenBarrier() {
232 dl 1.8 final CyclicBarrier c = new CyclicBarrier(3);
233 dl 1.5 Thread t1 = new Thread(new Runnable() {
234     public void run() {
235     try {
236     c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
237     threadShouldThrow();
238     } catch(TimeoutException success){
239     } catch(Exception b){
240     threadUnexpectedException();
241     }
242     }
243     });
244     Thread t2 = new Thread(new Runnable() {
245     public void run() {
246     try {
247     c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
248     threadShouldThrow();
249     } catch(BrokenBarrierException success){
250     } catch(Exception i){
251     threadUnexpectedException();
252     }
253     }
254     });
255     try {
256     t1.start();
257     t2.start();
258     t1.join();
259     t2.join();
260     } catch(InterruptedException e){
261     unexpectedException();
262     }
263     }
264    
265     /**
266     * A timeout in one party causes others waiting in await to
267     * throw BrokenBarrierException
268     */
269     public void testAwait5_Timeout_BrokenBarrier() {
270 dl 1.8 final CyclicBarrier c = new CyclicBarrier(3);
271 dl 1.5 Thread t1 = new Thread(new Runnable() {
272     public void run() {
273     try {
274     c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
275     threadShouldThrow();
276     } catch(TimeoutException success){
277     } catch(Exception b){
278     threadUnexpectedException();
279     }
280     }
281     });
282     Thread t2 = new Thread(new Runnable() {
283     public void run() {
284     try {
285     c.await();
286     threadShouldThrow();
287     } catch(BrokenBarrierException success){
288     } catch(Exception i){
289     threadUnexpectedException();
290     }
291     }
292     });
293     try {
294     t1.start();
295     t2.start();
296     t1.join();
297     t2.join();
298 dl 1.1 } catch(InterruptedException e){
299 dl 1.3 unexpectedException();
300 dl 1.1 }
301     }
302    
303 dl 1.4 /**
304     * A reset of an active barrier causes waiting threads to throw
305     * BrokenBarrierException
306     */
307     public void testReset_BrokenBarrier() {
308     final CyclicBarrier c = new CyclicBarrier(3);
309     Thread t1 = new Thread(new Runnable() {
310     public void run() {
311     try {
312     c.await();
313     threadShouldThrow();
314     } catch(BrokenBarrierException success){}
315     catch(Exception b){
316     threadUnexpectedException();
317     }
318     }
319     });
320     Thread t2 = new Thread(new Runnable() {
321     public void run() {
322     try {
323     c.await();
324     threadShouldThrow();
325     } catch(BrokenBarrierException success){
326     } catch(Exception i){
327     threadUnexpectedException();
328     }
329     }
330     });
331     try {
332     t1.start();
333     t2.start();
334     Thread.sleep(SHORT_DELAY_MS);
335     c.reset();
336     t1.join();
337     t2.join();
338     } catch(InterruptedException e){
339     unexpectedException();
340     }
341     }
342    
343     /**
344     * A reset before threads enter barrier does not throw
345     * BrokenBarrierException
346     */
347     public void testReset_NoBrokenBarrier() {
348     final CyclicBarrier c = new CyclicBarrier(3);
349     Thread t1 = new Thread(new Runnable() {
350     public void run() {
351     try {
352     c.await();
353     } catch(Exception b){
354     threadUnexpectedException();
355     }
356     }
357     });
358     Thread t2 = new Thread(new Runnable() {
359     public void run() {
360     try {
361     c.await();
362     } catch(Exception i){
363     threadUnexpectedException();
364     }
365     }
366     });
367     try {
368     c.reset();
369     t1.start();
370     t2.start();
371     c.await();
372     t1.join();
373     t2.join();
374     } catch(Exception e){
375     unexpectedException();
376     }
377     }
378    
379 dl 1.8 /**
380     * All threads block while a barrier is broken.
381     */
382     public void testReset_Leakage() {
383     try {
384     final CyclicBarrier c = new CyclicBarrier(2);
385     final AtomicBoolean done = new AtomicBoolean();
386     Thread t = new Thread() {
387     public void run() {
388     while (!done.get()) {
389     try {
390     while (c.isBroken())
391     c.reset();
392    
393     c.await();
394     threadFail("await should not return");
395     }
396     catch (BrokenBarrierException e) {
397     }
398     catch (InterruptedException ie) {
399     }
400     }
401     }
402     };
403    
404     t.start();
405     for( int i = 0; i < 4; i++) {
406     Thread.sleep(SHORT_DELAY_MS);
407     t.interrupt();
408     }
409     done.set(true);
410     t.interrupt();
411     }
412     catch (Exception ex) {
413     unexpectedException();
414     }
415     }
416    
417     /**
418     * Reset of a non-broken barrier does not break barrier
419     */
420     public void testResetWithoutBreakage() {
421     try {
422     final CyclicBarrier start = new CyclicBarrier(3);
423     final CyclicBarrier barrier = new CyclicBarrier(3);
424     for (int i = 0; i < 3; i++) {
425     Thread t1 = new Thread(new Runnable() {
426     public void run() {
427     try { start.await(); }
428     catch (Exception ie) {
429     threadFail("start barrier");
430     }
431     try { barrier.await(); }
432     catch (Throwable thrown) {
433     unexpectedException();
434     }}});
435    
436     Thread t2 = new Thread(new Runnable() {
437     public void run() {
438     try { start.await(); }
439     catch (Exception ie) {
440     threadFail("start barrier");
441     }
442     try { barrier.await(); }
443     catch (Throwable thrown) {
444     unexpectedException();
445     }}});
446    
447    
448     t1.start();
449     t2.start();
450     try { start.await(); }
451     catch (Exception ie) { threadFail("start barrier"); }
452     barrier.await();
453     t1.join();
454     t2.join();
455     assertFalse(barrier.isBroken());
456     assertEquals(0, barrier.getNumberWaiting());
457     if (i == 1) barrier.reset();
458     assertFalse(barrier.isBroken());
459     assertEquals(0, barrier.getNumberWaiting());
460     }
461     }
462     catch (Exception ex) {
463     unexpectedException();
464     }
465     }
466    
467     /**
468     * Reset of a barrier after interruption reinitializes it.
469     */
470     public void testResetAfterInterrupt() {
471     try {
472     final CyclicBarrier start = new CyclicBarrier(3);
473     final CyclicBarrier barrier = new CyclicBarrier(3);
474     for (int i = 0; i < 2; i++) {
475     Thread t1 = new Thread(new Runnable() {
476     public void run() {
477     try { start.await(); }
478     catch (Exception ie) {
479     threadFail("start barrier");
480     }
481     try { barrier.await(); }
482     catch(InterruptedException ok) {}
483     catch (Throwable thrown) {
484     unexpectedException();
485     }}});
486    
487     Thread t2 = new Thread(new Runnable() {
488     public void run() {
489     try { start.await(); }
490     catch (Exception ie) {
491     threadFail("start barrier");
492     }
493     try { barrier.await(); }
494     catch(BrokenBarrierException ok) {}
495     catch (Throwable thrown) {
496     unexpectedException();
497     }}});
498    
499     t1.start();
500     t2.start();
501     try { start.await(); }
502     catch (Exception ie) { threadFail("start barrier"); }
503     t1.interrupt();
504     t1.join();
505     t2.join();
506     assertTrue(barrier.isBroken());
507     assertEquals(0, barrier.getNumberWaiting());
508     barrier.reset();
509     assertFalse(barrier.isBroken());
510     assertEquals(0, barrier.getNumberWaiting());
511     }
512     }
513     catch (Exception ex) {
514     unexpectedException();
515     }
516     }
517    
518     /**
519     * Reset of a barrier after timeout reinitializes it.
520     */
521     public void testResetAfterTimeout() {
522     try {
523     final CyclicBarrier start = new CyclicBarrier(3);
524     final CyclicBarrier barrier = new CyclicBarrier(3);
525     for (int i = 0; i < 2; i++) {
526     Thread t1 = new Thread(new Runnable() {
527     public void run() {
528     try { start.await(); }
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();
536     }}});
537    
538     Thread t2 = new Thread(new Runnable() {
539     public void run() {
540     try { start.await(); }
541     catch (Exception ie) {
542     threadFail("start barrier");
543     }
544     try { barrier.await(); }
545     catch(BrokenBarrierException ok) {}
546     catch (Throwable thrown) {
547     unexpectedException();
548     }}});
549    
550     t1.start();
551     t2.start();
552     try { start.await(); }
553     catch (Exception ie) { threadFail("start barrier"); }
554     t1.join();
555     t2.join();
556     assertTrue(barrier.isBroken());
557     assertEquals(0, barrier.getNumberWaiting());
558     barrier.reset();
559     assertFalse(barrier.isBroken());
560     assertEquals(0, barrier.getNumberWaiting());
561     }
562     }
563     catch (Exception ex) {
564     unexpectedException();
565     }
566     }
567    
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 =
576     new CyclicBarrier(3, new Runnable() {
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");
585     }
586     try { barrier.await(); }
587     catch(BrokenBarrierException ok) {}
588     catch (Throwable thrown) {
589     unexpectedException();
590     }}});
591    
592     Thread t2 = new Thread(new Runnable() {
593     public void run() {
594     try { start.await(); }
595     catch (Exception ie) {
596     threadFail("start barrier");
597     }
598     try { barrier.await(); }
599     catch(BrokenBarrierException ok) {}
600     catch (Throwable thrown) {
601     unexpectedException();
602     }}});
603    
604     t1.start();
605     t2.start();
606     try { start.await(); }
607     catch (Exception ie) { threadFail("start barrier"); }
608     while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
609     try { barrier.await(); }
610     catch (Exception ok) { }
611     t1.join();
612     t2.join();
613     assertTrue(barrier.isBroken());
614     assertEquals(0, barrier.getNumberWaiting());
615     barrier.reset();
616     assertFalse(barrier.isBroken());
617     assertEquals(0, barrier.getNumberWaiting());
618     }
619     }
620     catch (Exception ex) {
621     unexpectedException();
622     }
623     }
624 dl 1.1 }