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.13 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.14 by jsr166, Sat Nov 21 06:26:00 2009 UTC

# Line 33 | Line 33 | public class CyclicBarrierTest extends J
33          try {
34              new CyclicBarrier(-1, (Runnable)null);
35              shouldThrow();
36 <        } catch (IllegalArgumentException e) {}
36 >        } catch (IllegalArgumentException success) {}
37      }
38  
39      /**
# Line 43 | Line 43 | public class CyclicBarrierTest extends J
43          try {
44              new CyclicBarrier(-1);
45              shouldThrow();
46 <        } catch (IllegalArgumentException e) {}
46 >        } catch (IllegalArgumentException success) {}
47      }
48  
49      /**
# Line 58 | Line 58 | public class CyclicBarrierTest extends J
58      /**
59       * A 1-party barrier triggers after single await
60       */
61 <    public void testSingleParty() {
62 <        try {
63 <            CyclicBarrier b = new CyclicBarrier(1);
64 <            assertEquals(1, b.getParties());
65 <            assertEquals(0, b.getNumberWaiting());
66 <            b.await();
67 <            b.await();
68 <            assertEquals(0, b.getNumberWaiting());
69 <        }
70 <        catch (Exception e) {
71 <            unexpectedException();
72 <        }
61 >    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      }
69  
70      /**
71       * The supplied barrier action is run at barrier
72       */
73 <    public void testBarrierAction() {
74 <        try {
75 <            countAction = 0;
76 <            CyclicBarrier b = new CyclicBarrier(1, new MyAction());
77 <            assertEquals(1, b.getParties());
78 <            assertEquals(0, b.getNumberWaiting());
79 <            b.await();
80 <            b.await();
81 <            assertEquals(0, b.getNumberWaiting());
87 <            assertEquals(countAction, 2);
88 <        }
89 <        catch (Exception e) {
90 <            unexpectedException();
91 <        }
73 >    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      }
83  
84      /**
85       * A 2-party/thread barrier triggers after both threads invoke await
86       */
87 <    public void testTwoParties() {
87 >    public void testTwoParties() throws Exception {
88          final CyclicBarrier b = new CyclicBarrier(2);
89 <        Thread t = new Thread(new Runnable() {
90 <                public void run() {
91 <                    try {
92 <                        b.await();
93 <                        b.await();
94 <                        b.await();
95 <                        b.await();
96 <                    } catch (Exception e) {
97 <                        threadUnexpectedException();
98 <                    }}});
99 <
100 <        try {
101 <            t.start();
102 <            b.await();
113 <            b.await();
114 <            b.await();
115 <            b.await();
116 <            t.join();
117 <        } catch (Exception e) {
118 <            unexpectedException();
119 <        }
89 >        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      }
104  
105  
# Line 124 | Line 107 | public class CyclicBarrierTest extends J
107       * An interruption in one party causes others waiting in await to
108       * throw BrokenBarrierException
109       */
110 <    public void testAwait1_Interrupted_BrokenBarrier() {
110 >    public void testAwait1_Interrupted_BrokenBarrier() throws Exception {
111          final CyclicBarrier c = new CyclicBarrier(3);
112 <        Thread t1 = new Thread(new Runnable() {
113 <                public void run() {
114 <                    try {
115 <                        c.await();
116 <                        threadShouldThrow();
117 <                    } catch (InterruptedException success) {}
118 <                    catch (Exception b) {
119 <                        threadUnexpectedException();
120 <                    }
121 <                }
122 <            });
123 <        Thread t2 = new Thread(new Runnable() {
124 <                public void run() {
125 <                    try {
126 <                        c.await();
144 <                        threadShouldThrow();
145 <                    } catch (BrokenBarrierException success) {
146 <                    } catch (Exception i) {
147 <                        threadUnexpectedException();
148 <                    }
149 <                }
150 <            });
151 <        try {
152 <            t1.start();
153 <            t2.start();
154 <            Thread.sleep(SHORT_DELAY_MS);
155 <            t1.interrupt();
156 <            t1.join();
157 <            t2.join();
158 <        } catch (InterruptedException e) {
159 <            unexpectedException();
160 <        }
112 >        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      }
128  
129      /**
130       * An interruption in one party causes others waiting in timed await to
131       * throw BrokenBarrierException
132       */
133 <    public void testAwait2_Interrupted_BrokenBarrier() {
133 >    public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
134          final CyclicBarrier c = new CyclicBarrier(3);
135 <        Thread t1 = new Thread(new Runnable() {
136 <                public void run() {
137 <                    try {
138 <                        c.await(LONG_DELAY_MS, MILLISECONDS);
139 <                        threadShouldThrow();
140 <                    } catch (InterruptedException success) {
141 <                    } catch (Exception b) {
142 <                        threadUnexpectedException();
143 <                    }
144 <                }
145 <            });
146 <        Thread t2 = new Thread(new Runnable() {
147 <                public void run() {
148 <                    try {
149 <                        c.await(LONG_DELAY_MS, MILLISECONDS);
184 <                        threadShouldThrow();
185 <                    } catch (BrokenBarrierException success) {
186 <                    } catch (Exception i) {
187 <                        threadUnexpectedException();
188 <                    }
189 <                }
190 <            });
191 <        try {
192 <            t1.start();
193 <            t2.start();
194 <            Thread.sleep(SHORT_DELAY_MS);
195 <            t1.interrupt();
196 <            t1.join();
197 <            t2.join();
198 <        } catch (InterruptedException e) {
199 <            unexpectedException();
200 <        }
135 >        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      }
151  
152      /**
153       * A timeout in timed await throws TimeoutException
154       */
155 <    public void testAwait3_TimeOutException() {
155 >    public void testAwait3_TimeOutException() throws InterruptedException {
156          final CyclicBarrier c = new CyclicBarrier(2);
157 <        Thread t = new Thread(new Runnable() {
158 <                public void run() {
159 <                    try {
160 <                        c.await(SHORT_DELAY_MS, MILLISECONDS);
212 <                        threadShouldThrow();
213 <                    } catch (TimeoutException success) {
214 <                    } catch (Exception b) {
215 <                        threadUnexpectedException();
157 >        Thread t = new ThreadShouldThrow(TimeoutException.class) {
158 >            public void realRun() throws Exception {
159 >                c.await(SHORT_DELAY_MS, MILLISECONDS);
160 >            }};
161  
162 <                    }
163 <                }
219 <            });
220 <        try {
221 <            t.start();
222 <            t.join();
223 <        } catch (InterruptedException e) {
224 <            unexpectedException();
225 <        }
162 >        t.start();
163 >        t.join();
164      }
165  
166      /**
167       * A timeout in one party causes others waiting in timed await to
168       * throw BrokenBarrierException
169       */
170 <    public void testAwait4_Timeout_BrokenBarrier() {
170 >    public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
171          final CyclicBarrier c = new CyclicBarrier(3);
172 <        Thread t1 = new Thread(new Runnable() {
173 <                public void run() {
174 <                    try {
175 <                        c.await(SHORT_DELAY_MS, MILLISECONDS);
176 <                        threadShouldThrow();
177 <                    } catch (TimeoutException success) {
178 <                    } catch (Exception b) {
179 <                        threadUnexpectedException();
180 <                    }
181 <                }
182 <            });
183 <        Thread t2 = new Thread(new Runnable() {
184 <                public void run() {
247 <                    try {
248 <                        c.await(MEDIUM_DELAY_MS, MILLISECONDS);
249 <                        threadShouldThrow();
250 <                    } catch (BrokenBarrierException success) {
251 <                    } catch (Exception i) {
252 <                        threadUnexpectedException();
253 <                    }
254 <                }
255 <            });
256 <        try {
257 <            t1.start();
258 <            t2.start();
259 <            t1.join();
260 <            t2.join();
261 <        } catch (InterruptedException e) {
262 <            unexpectedException();
263 <        }
172 >        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      }
186  
187      /**
188       * A timeout in one party causes others waiting in await to
189       * throw BrokenBarrierException
190       */
191 <    public void testAwait5_Timeout_BrokenBarrier() {
191 >    public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
192          final CyclicBarrier c = new CyclicBarrier(3);
193 <        Thread t1 = new Thread(new Runnable() {
194 <                public void run() {
195 <                    try {
196 <                        c.await(SHORT_DELAY_MS, MILLISECONDS);
197 <                        threadShouldThrow();
198 <                    } catch (TimeoutException success) {
199 <                    } catch (Exception b) {
200 <                        threadUnexpectedException();
201 <                    }
202 <                }
203 <            });
204 <        Thread t2 = new Thread(new Runnable() {
205 <                public void run() {
285 <                    try {
286 <                        c.await();
287 <                        threadShouldThrow();
288 <                    } catch (BrokenBarrierException success) {
289 <                    } catch (Exception i) {
290 <                        threadUnexpectedException();
291 <                    }
292 <                }
293 <            });
294 <        try {
295 <            t1.start();
296 <            t2.start();
297 <            t1.join();
298 <            t2.join();
299 <        } catch (InterruptedException e) {
300 <            unexpectedException();
301 <        }
193 >        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      }
207  
208      /**
209       * A reset of an active barrier causes waiting threads to throw
210       * BrokenBarrierException
211       */
212 <    public void testReset_BrokenBarrier() {
212 >    public void testReset_BrokenBarrier() throws InterruptedException {
213          final CyclicBarrier c = new CyclicBarrier(3);
214 <        Thread t1 = new Thread(new Runnable() {
215 <                public void run() {
216 <                    try {
217 <                        c.await();
218 <                        threadShouldThrow();
219 <                    } catch (BrokenBarrierException success) {}
220 <                    catch (Exception b) {
221 <                        threadUnexpectedException();
222 <                    }
223 <                }
224 <            });
225 <        Thread t2 = new Thread(new Runnable() {
226 <                public void run() {
227 <                    try {
228 <                        c.await();
325 <                        threadShouldThrow();
326 <                    } catch (BrokenBarrierException success) {
327 <                    } catch (Exception i) {
328 <                        threadUnexpectedException();
329 <                    }
330 <                }
331 <            });
332 <        try {
333 <            t1.start();
334 <            t2.start();
335 <            Thread.sleep(SHORT_DELAY_MS);
336 <            c.reset();
337 <            t1.join();
338 <            t2.join();
339 <        } catch (InterruptedException e) {
340 <            unexpectedException();
341 <        }
214 >        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      }
230  
231      /**
232       * A reset before threads enter barrier does not throw
233       * BrokenBarrierException
234       */
235 <    public void testReset_NoBrokenBarrier() {
235 >    public void testReset_NoBrokenBarrier() throws Exception {
236          final CyclicBarrier c = new CyclicBarrier(3);
237 <        Thread t1 = new Thread(new Runnable() {
238 <                public void run() {
239 <                    try {
240 <                        c.await();
241 <                    } catch (Exception b) {
242 <                        threadUnexpectedException();
243 <                    }
244 <                }
245 <            });
246 <        Thread t2 = new Thread(new Runnable() {
247 <                public void run() {
248 <                    try {
249 <                        c.await();
250 <                    } catch (Exception i) {
251 <                        threadUnexpectedException();
365 <                    }
366 <                }
367 <            });
368 <        try {
369 <            c.reset();
370 <            t1.start();
371 <            t2.start();
372 <            c.await();
373 <            t1.join();
374 <            t2.join();
375 <        } catch (Exception e) {
376 <            unexpectedException();
377 <        }
237 >        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      }
253  
254      /**
255       * All threads block while a barrier is broken.
256       */
257 <    public void testReset_Leakage() {
258 <        try {
259 <            final CyclicBarrier c = new CyclicBarrier(2);
260 <            final AtomicBoolean done = new AtomicBoolean();
261 <            Thread t = new Thread() {
262 <                    public void run() {
263 <                        while (!done.get()) {
264 <                            try {
265 <                                while (c.isBroken())
266 <                                    c.reset();
267 <
268 <                                c.await();
269 <                                threadFail("await should not return");
270 <                            }
271 <                            catch (BrokenBarrierException e) {
272 <                            }
399 <                            catch (InterruptedException ie) {
400 <                            }
257 >    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                          }
274                      }
275 <                };
275 >                }
276 >            };
277  
278 <            t.start();
279 <            for ( int i = 0; i < 4; i++) {
280 <                Thread.sleep(SHORT_DELAY_MS);
408 <                t.interrupt();
409 <            }
410 <            done.set(true);
278 >        t.start();
279 >        for (int i = 0; i < 4; i++) {
280 >            Thread.sleep(SHORT_DELAY_MS);
281              t.interrupt();
282          }
283 <        catch (Exception ex) {
284 <            unexpectedException();
285 <        }
283 >        done.set(true);
284 >        t.interrupt();
285 >        t.join();
286      }
287  
288      /**
289       * Reset of a non-broken barrier does not break barrier
290       */
291 <    public void testResetWithoutBreakage() {
292 <        try {
293 <            final CyclicBarrier start = new CyclicBarrier(3);
294 <            final CyclicBarrier barrier = new CyclicBarrier(3);
295 <            for (int i = 0; i < 3; i++) {
296 <                Thread t1 = new Thread(new Runnable() {
297 <                        public void run() {
298 <                            try { start.await(); }
299 <                            catch (Exception ie) {
300 <                                threadFail("start barrier");
301 <                            }
302 <                            try { barrier.await(); }
303 <                            catch (Throwable thrown) {
304 <                                unexpectedException();
305 <                            }}});
306 <
307 <                Thread t2 = new Thread(new Runnable() {
308 <                        public void run() {
309 <                            try { start.await(); }
310 <                            catch (Exception ie) {
311 <                                threadFail("start barrier");
312 <                            }
313 <                            try { barrier.await(); }
314 <                            catch (Throwable thrown) {
315 <                                unexpectedException();
316 <                            }}});
317 <
448 <
449 <                t1.start();
450 <                t2.start();
451 <                try { start.await(); }
452 <                catch (Exception ie) { threadFail("start barrier"); }
453 <                barrier.await();
454 <                t1.join();
455 <                t2.join();
456 <                assertFalse(barrier.isBroken());
457 <                assertEquals(0, barrier.getNumberWaiting());
458 <                if (i == 1) barrier.reset();
459 <                assertFalse(barrier.isBroken());
460 <                assertEquals(0, barrier.getNumberWaiting());
461 <            }
462 <        }
463 <        catch (Exception ex) {
464 <            unexpectedException();
291 >    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          }
319      }
320  
321      /**
322       * Reset of a barrier after interruption reinitializes it.
323       */
324 <    public void testResetAfterInterrupt() {
325 <        try {
326 <            final CyclicBarrier start = new CyclicBarrier(3);
327 <            final CyclicBarrier barrier = new CyclicBarrier(3);
328 <            for (int i = 0; i < 2; i++) {
329 <                Thread t1 = new Thread(new Runnable() {
330 <                        public void run() {
331 <                            try { start.await(); }
332 <                            catch (Exception ie) {
333 <                                threadFail("start barrier");
334 <                            }
335 <                            try { barrier.await(); }
336 <                            catch (InterruptedException ok) {}
337 <                            catch (Throwable thrown) {
338 <                                unexpectedException();
339 <                            }}});
340 <
341 <                Thread t2 = new Thread(new Runnable() {
342 <                        public void run() {
343 <                            try { start.await(); }
344 <                            catch (Exception ie) {
345 <                                threadFail("start barrier");
346 <                            }
347 <                            try { barrier.await(); }
348 <                            catch (BrokenBarrierException ok) {}
349 <                            catch (Throwable thrown) {
350 <                                unexpectedException();
498 <                            }}});
499 <
500 <                t1.start();
501 <                t2.start();
502 <                try { start.await(); }
503 <                catch (Exception ie) { threadFail("start barrier"); }
504 <                t1.interrupt();
505 <                t1.join();
506 <                t2.join();
507 <                assertTrue(barrier.isBroken());
508 <                assertEquals(0, barrier.getNumberWaiting());
509 <                barrier.reset();
510 <                assertFalse(barrier.isBroken());
511 <                assertEquals(0, barrier.getNumberWaiting());
512 <            }
513 <        }
514 <        catch (Exception ex) {
515 <            unexpectedException();
324 >    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          }
352      }
353  
354      /**
355       * Reset of a barrier after timeout reinitializes it.
356       */
357 <    public void testResetAfterTimeout() {
358 <        try {
359 <            final CyclicBarrier start = new CyclicBarrier(3);
360 <            final CyclicBarrier barrier = new CyclicBarrier(3);
361 <            for (int i = 0; i < 2; i++) {
362 <                Thread t1 = new Thread(new Runnable() {
363 <                        public void run() {
364 <                            try { start.await(); }
365 <                            catch (Exception ie) {
366 <                                threadFail("start barrier");
367 <                            }
368 <                            try { barrier.await(MEDIUM_DELAY_MS, MILLISECONDS); }
369 <                            catch (TimeoutException ok) {}
370 <                            catch (Throwable thrown) {
371 <                                unexpectedException();
372 <                            }}});
373 <
374 <                Thread t2 = new Thread(new Runnable() {
375 <                        public void run() {
376 <                            try { start.await(); }
377 <                            catch (Exception ie) {
378 <                                threadFail("start barrier");
379 <                            }
380 <                            try { barrier.await(); }
381 <                            catch (BrokenBarrierException ok) {}
382 <                            catch (Throwable thrown) {
548 <                                unexpectedException();
549 <                            }}});
550 <
551 <                t1.start();
552 <                t2.start();
553 <                try { start.await(); }
554 <                catch (Exception ie) { threadFail("start barrier"); }
555 <                t1.join();
556 <                t2.join();
557 <                assertTrue(barrier.isBroken());
558 <                assertEquals(0, barrier.getNumberWaiting());
559 <                barrier.reset();
560 <                assertFalse(barrier.isBroken());
561 <                assertEquals(0, barrier.getNumberWaiting());
562 <            }
563 <        }
564 <        catch (Exception ex) {
565 <            unexpectedException();
357 >    public void testResetAfterTimeout() throws Exception {
358 >        final CyclicBarrier start = new CyclicBarrier(3);
359 >        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 >                        barrier.await(MEDIUM_DELAY_MS, MILLISECONDS);
365 >                    }};
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 >            start.await();
376 >            t1.join();
377 >            t2.join();
378 >            assertTrue(barrier.isBroken());
379 >            assertEquals(0, barrier.getNumberWaiting());
380 >            barrier.reset();
381 >            assertFalse(barrier.isBroken());
382 >            assertEquals(0, barrier.getNumberWaiting());
383          }
384      }
385  
# Line 570 | Line 387 | public class CyclicBarrierTest extends J
387      /**
388       * Reset of a barrier after a failed command reinitializes it.
389       */
390 <    public void testResetAfterCommandException() {
391 <        try {
392 <            final CyclicBarrier start = new CyclicBarrier(3);
393 <            final CyclicBarrier barrier =
394 <                new CyclicBarrier(3, new Runnable() {
395 <                        public void run() {
396 <                            throw new NullPointerException(); }});
397 <            for (int i = 0; i < 2; i++) {
398 <                Thread t1 = new Thread(new Runnable() {
399 <                        public void run() {
400 <                            try { start.await(); }
401 <                            catch (Exception ie) {
402 <                                threadFail("start barrier");
403 <                            }
404 <                            try { barrier.await(); }
405 <                            catch (BrokenBarrierException ok) {}
406 <                            catch (Throwable thrown) {
407 <                                unexpectedException();
408 <                            }}});
409 <
410 <                Thread t2 = new Thread(new Runnable() {
411 <                        public void run() {
412 <                            try { start.await(); }
413 <                            catch (Exception ie) {
414 <                                threadFail("start barrier");
415 <                            }
416 <                            try { barrier.await(); }
417 <                            catch (BrokenBarrierException ok) {}
418 <                            catch (Throwable thrown) {
419 <                                unexpectedException();
420 <                            }}});
421 <
422 <                t1.start();
423 <                t2.start();
607 <                try { start.await(); }
608 <                catch (Exception ie) { threadFail("start barrier"); }
609 <                while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
610 <                try { barrier.await(); }
611 <                catch (Exception ok) { }
612 <                t1.join();
613 <                t2.join();
614 <                assertTrue(barrier.isBroken());
615 <                assertEquals(0, barrier.getNumberWaiting());
616 <                barrier.reset();
617 <                assertFalse(barrier.isBroken());
618 <                assertEquals(0, barrier.getNumberWaiting());
619 <            }
620 <        }
621 <        catch (Exception ex) {
622 <            unexpectedException();
390 >    public void testResetAfterCommandException() throws Exception {
391 >        final CyclicBarrier start = new CyclicBarrier(3);
392 >        final CyclicBarrier barrier =
393 >            new CyclicBarrier(3, new Runnable() {
394 >                    public void run() {
395 >                        throw new NullPointerException(); }});
396 >        for (int i = 0; i < 2; i++) {
397 >            Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
398 >                public void realRun() throws Exception {
399 >                    start.await();
400 >                    barrier.await();
401 >                }};
402 >
403 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
404 >                public void realRun() throws Exception {
405 >                    start.await();
406 >                    barrier.await();
407 >                }};
408 >
409 >            t1.start();
410 >            t2.start();
411 >            start.await();
412 >            while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
413 >            try {
414 >                barrier.await();
415 >                shouldThrow();
416 >            } catch (NullPointerException success) {}
417 >            t1.join();
418 >            t2.join();
419 >            assertTrue(barrier.isBroken());
420 >            assertEquals(0, barrier.getNumberWaiting());
421 >            barrier.reset();
422 >            assertFalse(barrier.isBroken());
423 >            assertEquals(0, barrier.getNumberWaiting());
424          }
425      }
426   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines