ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.5
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +78 -2 lines
Log Message:
Added tests and documentation

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11    
12 dl 1.2 public class CyclicBarrierTest extends JSR166TestCase{
13 dl 1.1 public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(CyclicBarrierTest.class);
18     }
19 dl 1.3
20     private volatile int countAction;
21     private class MyAction implements Runnable {
22     public void run() { ++countAction; }
23     }
24 dl 1.1
25 dl 1.3 /**
26 dl 1.4 * Creating with negative parties throws IAE
27 dl 1.3 */
28     public void testConstructor1() {
29     try {
30 dl 1.1 new CyclicBarrier(-1, (Runnable)null);
31 dl 1.3 shouldThrow();
32 dl 1.1 } catch(IllegalArgumentException e){}
33     }
34    
35 dl 1.3 /**
36 dl 1.4 * Creating with negative parties and no action throws IAE
37 dl 1.3 */
38     public void testConstructor2() {
39     try {
40 dl 1.1 new CyclicBarrier(-1);
41 dl 1.3 shouldThrow();
42 dl 1.1 } catch(IllegalArgumentException e){}
43     }
44    
45 dl 1.3 /**
46 dl 1.4 * getParties returns the number of parties given in constructor
47 dl 1.3 */
48 dl 1.4 public void testGetParties() {
49 dl 1.1 CyclicBarrier b = new CyclicBarrier(2);
50     assertEquals(2, b.getParties());
51     assertEquals(0, b.getNumberWaiting());
52     }
53    
54 dl 1.3 /**
55 dl 1.5 * A 1-party barrier triggers after single await
56 dl 1.3 */
57 dl 1.1 public void testSingleParty() {
58     try {
59     CyclicBarrier b = new CyclicBarrier(1);
60     assertEquals(1, b.getParties());
61     assertEquals(0, b.getNumberWaiting());
62     b.await();
63     b.await();
64     assertEquals(0, b.getNumberWaiting());
65     }
66     catch(Exception e) {
67 dl 1.3 unexpectedException();
68 dl 1.1 }
69     }
70    
71 dl 1.3 /**
72 dl 1.4 * The supplied barrier action is run at barrier
73 dl 1.3 */
74 dl 1.1 public void testBarrierAction() {
75     try {
76     countAction = 0;
77     CyclicBarrier b = new CyclicBarrier(1, new MyAction());
78     assertEquals(1, b.getParties());
79     assertEquals(0, b.getNumberWaiting());
80     b.await();
81     b.await();
82     assertEquals(0, b.getNumberWaiting());
83     assertEquals(countAction, 2);
84     }
85     catch(Exception e) {
86 dl 1.3 unexpectedException();
87 dl 1.1 }
88     }
89    
90 dl 1.3 /**
91 dl 1.5 * A 2-party/thread barrier triggers after both threads invoke await
92 dl 1.3 */
93     public void testTwoParties() {
94 dl 1.1 final CyclicBarrier b = new CyclicBarrier(2);
95     Thread t = new Thread(new Runnable() {
96 dl 1.3 public void run() {
97 dl 1.1 try {
98     b.await();
99     b.await();
100     b.await();
101     b.await();
102     } catch(Exception e){
103 dl 1.3 threadUnexpectedException();
104 dl 1.1 }}});
105    
106     try {
107     t.start();
108     b.await();
109     b.await();
110     b.await();
111     b.await();
112     t.join();
113     } catch(Exception e){
114 dl 1.3 unexpectedException();
115 dl 1.1 }
116     }
117    
118    
119 dl 1.3 /**
120 dl 1.4 * An interruption in one party causes others waiting in await to
121     * throw BrokenBarrierException
122 dl 1.3 */
123     public void testAwait1_Interrupted_BrokenBarrier() {
124 dl 1.1 final CyclicBarrier c = new CyclicBarrier(3);
125     Thread t1 = new Thread(new Runnable() {
126 dl 1.3 public void run() {
127     try {
128 dl 1.1 c.await();
129 dl 1.3 threadShouldThrow();
130 dl 1.1 } catch(InterruptedException success){}
131     catch(Exception b){
132 dl 1.4 threadUnexpectedException();
133 dl 1.1 }
134     }
135     });
136 dl 1.3 Thread t2 = new Thread(new Runnable() {
137     public void run() {
138     try {
139 dl 1.1 c.await();
140 dl 1.3 threadShouldThrow();
141 dl 1.1 } catch(BrokenBarrierException success){
142     } catch(Exception i){
143 dl 1.4 threadUnexpectedException();
144 dl 1.1 }
145     }
146     });
147     try {
148     t1.start();
149     t2.start();
150     Thread.sleep(SHORT_DELAY_MS);
151     t1.interrupt();
152     t1.join();
153     t2.join();
154     } catch(InterruptedException e){
155 dl 1.3 unexpectedException();
156 dl 1.1 }
157     }
158    
159 dl 1.3 /**
160 dl 1.4 * An interruption in one party causes others waiting in timed await to
161     * throw BrokenBarrierException
162 dl 1.3 */
163     public void testAwait2_Interrupted_BrokenBarrier() {
164 dl 1.1 final CyclicBarrier c = new CyclicBarrier(3);
165     Thread t1 = new Thread(new Runnable() {
166 dl 1.3 public void run() {
167     try {
168 dl 1.1 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
169 dl 1.3 threadShouldThrow();
170 dl 1.1 } catch(InterruptedException success){
171     } catch(Exception b){
172 dl 1.4 threadUnexpectedException();
173 dl 1.1 }
174     }
175     });
176 dl 1.3 Thread t2 = new Thread(new Runnable() {
177     public void run() {
178     try {
179 dl 1.1 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
180 dl 1.3 threadShouldThrow();
181 dl 1.1 } catch(BrokenBarrierException success){
182     } catch(Exception i){
183 dl 1.4 threadUnexpectedException();
184 dl 1.1 }
185     }
186     });
187     try {
188     t1.start();
189     t2.start();
190     Thread.sleep(SHORT_DELAY_MS);
191     t1.interrupt();
192     t1.join();
193     t2.join();
194     } catch(InterruptedException e){
195 dl 1.3 unexpectedException();
196 dl 1.1 }
197     }
198    
199 dl 1.3 /**
200 dl 1.4 * A timeout in timed await throws TimeoutException
201 dl 1.3 */
202     public void testAwait3_TimeOutException() {
203 dl 1.1 final CyclicBarrier c = new CyclicBarrier(2);
204     Thread t = new Thread(new Runnable() {
205 dl 1.3 public void run() {
206     try {
207 dl 1.1 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
208 dl 1.3 threadShouldThrow();
209 dl 1.1 } catch(TimeoutException success){
210     } catch(Exception b){
211 dl 1.4 threadUnexpectedException();
212 dl 1.1
213     }
214     }
215     });
216     try {
217     t.start();
218     t.join();
219 dl 1.5 } catch(InterruptedException e){
220     unexpectedException();
221     }
222     }
223    
224     /**
225     * A timeout in one party causes others waiting in timed await to
226     * throw BrokenBarrierException
227     */
228     public void testAwait4_Timeout_BrokenBarrier() {
229     final CyclicBarrier c = new CyclicBarrier(3);
230     Thread t1 = new Thread(new Runnable() {
231     public void run() {
232     try {
233     c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
234     threadShouldThrow();
235     } catch(TimeoutException success){
236     } catch(Exception b){
237     threadUnexpectedException();
238     }
239     }
240     });
241     Thread t2 = new Thread(new Runnable() {
242     public void run() {
243     try {
244     c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
245     threadShouldThrow();
246     } catch(BrokenBarrierException success){
247     } catch(Exception i){
248     threadUnexpectedException();
249     }
250     }
251     });
252     try {
253     t1.start();
254     t2.start();
255     t1.join();
256     t2.join();
257     } catch(InterruptedException e){
258     unexpectedException();
259     }
260     }
261    
262     /**
263     * A timeout in one party causes others waiting in await to
264     * throw BrokenBarrierException
265     */
266     public void testAwait5_Timeout_BrokenBarrier() {
267     final CyclicBarrier c = new CyclicBarrier(3);
268     Thread t1 = new Thread(new Runnable() {
269     public void run() {
270     try {
271     c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
272     threadShouldThrow();
273     } catch(TimeoutException success){
274     } catch(Exception b){
275     threadUnexpectedException();
276     }
277     }
278     });
279     Thread t2 = new Thread(new Runnable() {
280     public void run() {
281     try {
282     c.await();
283     threadShouldThrow();
284     } catch(BrokenBarrierException success){
285     } catch(Exception i){
286     threadUnexpectedException();
287     }
288     }
289     });
290     try {
291     t1.start();
292     t2.start();
293     t1.join();
294     t2.join();
295 dl 1.1 } catch(InterruptedException e){
296 dl 1.3 unexpectedException();
297 dl 1.1 }
298     }
299    
300 dl 1.4 /**
301     * A reset of an active barrier causes waiting threads to throw
302     * BrokenBarrierException
303     */
304     public void testReset_BrokenBarrier() {
305     final CyclicBarrier c = new CyclicBarrier(3);
306     Thread t1 = new Thread(new Runnable() {
307     public void run() {
308     try {
309     c.await();
310     threadShouldThrow();
311     } catch(BrokenBarrierException success){}
312     catch(Exception b){
313     threadUnexpectedException();
314     }
315     }
316     });
317     Thread t2 = new Thread(new Runnable() {
318     public void run() {
319     try {
320     c.await();
321     threadShouldThrow();
322     } catch(BrokenBarrierException success){
323     } catch(Exception i){
324     threadUnexpectedException();
325     }
326     }
327     });
328     try {
329     t1.start();
330     t2.start();
331     Thread.sleep(SHORT_DELAY_MS);
332     c.reset();
333     t1.join();
334     t2.join();
335     } catch(InterruptedException e){
336     unexpectedException();
337     }
338     }
339    
340     /**
341     * A reset before threads enter barrier does not throw
342     * BrokenBarrierException
343     */
344     public void testReset_NoBrokenBarrier() {
345     final CyclicBarrier c = new CyclicBarrier(3);
346     Thread t1 = new Thread(new Runnable() {
347     public void run() {
348     try {
349     c.await();
350     } catch(Exception b){
351     threadUnexpectedException();
352     }
353     }
354     });
355     Thread t2 = new Thread(new Runnable() {
356     public void run() {
357     try {
358     c.await();
359     } catch(Exception i){
360     threadUnexpectedException();
361     }
362     }
363     });
364     try {
365     c.reset();
366     t1.start();
367     t2.start();
368     c.await();
369     t1.join();
370     t2.join();
371     } catch(Exception e){
372     unexpectedException();
373     }
374     }
375    
376 dl 1.1 }