ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CyclicBarrierTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.6 by dl, Sat Dec 27 20:41:56 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines