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.21 by jsr166, Sat May 28 14:52:11 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 15 | Line 15 | import static java.util.concurrent.TimeU
15  
16   public class CyclicBarrierTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21          return new TestSuite(CyclicBarrierTest.class);
# 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();
112 <            b.await();
113 <            b.await();
114 <            b.await();
115 <            b.await();
116 <            t.join();
117 <        } catch (Exception e) {
118 <            unexpectedException();
119 <        }
89 >        Thread t = newStartedThread(new CheckedRunnable() {
90 >            public void realRun() throws Exception {
91 >                b.await();
92 >                b.await();
93 >                b.await();
94 >                b.await();
95 >            }});
96 >
97 >        b.await();
98 >        b.await();
99 >        b.await();
100 >        b.await();
101 >        awaitTermination(t);
102      }
103  
122
104      /**
105       * An interruption in one party causes others waiting in await to
106       * throw BrokenBarrierException
107       */
108      public void testAwait1_Interrupted_BrokenBarrier() {
109          final CyclicBarrier c = new CyclicBarrier(3);
110 <        Thread t1 = new Thread(new Runnable() {
111 <                public void run() {
112 <                    try {
113 <                        c.await();
114 <                        threadShouldThrow();
115 <                    } catch (InterruptedException success) {}
116 <                    catch (Exception b) {
117 <                        threadUnexpectedException();
118 <                    }
119 <                }
120 <            });
121 <        Thread t2 = new Thread(new Runnable() {
122 <                public void run() {
123 <                    try {
124 <                        c.await();
125 <                        threadShouldThrow();
126 <                    } catch (BrokenBarrierException success) {
127 <                    } 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 <        }
110 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
111 >        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
112 >            public void realRun() throws Exception {
113 >                pleaseInterrupt.countDown();
114 >                c.await();
115 >            }};
116 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
117 >            public void realRun() throws Exception {
118 >                pleaseInterrupt.countDown();
119 >                c.await();
120 >            }};
121 >
122 >        t1.start();
123 >        t2.start();
124 >        await(pleaseInterrupt);
125 >        t1.interrupt();
126 >        awaitTermination(t1);
127 >        awaitTermination(t2);
128      }
129  
130      /**
131       * An interruption in one party causes others waiting in timed await to
132       * throw BrokenBarrierException
133       */
134 <    public void testAwait2_Interrupted_BrokenBarrier() {
134 >    public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
135          final CyclicBarrier c = new CyclicBarrier(3);
136 <        Thread t1 = new Thread(new Runnable() {
137 <                public void run() {
138 <                    try {
139 <                        c.await(LONG_DELAY_MS, MILLISECONDS);
140 <                        threadShouldThrow();
141 <                    } catch (InterruptedException success) {
142 <                    } catch (Exception b) {
143 <                        threadUnexpectedException();
144 <                    }
145 <                }
146 <            });
147 <        Thread t2 = new Thread(new Runnable() {
148 <                public void run() {
149 <                    try {
150 <                        c.await(LONG_DELAY_MS, MILLISECONDS);
151 <                        threadShouldThrow();
152 <                    } catch (BrokenBarrierException success) {
153 <                    } 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 <        }
136 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
137 >        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
138 >            public void realRun() throws Exception {
139 >                pleaseInterrupt.countDown();
140 >                c.await(LONG_DELAY_MS, MILLISECONDS);
141 >            }};
142 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
143 >            public void realRun() throws Exception {
144 >                pleaseInterrupt.countDown();
145 >                c.await(LONG_DELAY_MS, MILLISECONDS);
146 >            }};
147 >
148 >        t1.start();
149 >        t2.start();
150 >        await(pleaseInterrupt);
151 >        t1.interrupt();
152 >        awaitTermination(t1);
153 >        awaitTermination(t2);
154      }
155  
156      /**
157       * A timeout in timed await throws TimeoutException
158       */
159 <    public void testAwait3_TimeOutException() {
159 >    public void testAwait3_TimeoutException() throws InterruptedException {
160          final CyclicBarrier c = new CyclicBarrier(2);
161 <        Thread t = new Thread(new Runnable() {
162 <                public void run() {
163 <                    try {
164 <                        c.await(SHORT_DELAY_MS, MILLISECONDS);
165 <                        threadShouldThrow();
166 <                    } catch (TimeoutException success) {
167 <                    } catch (Exception b) {
168 <                        threadUnexpectedException();
161 >        Thread t = newStartedThread(new CheckedRunnable() {
162 >            public void realRun() throws Exception {
163 >                long startTime = System.nanoTime();
164 >                try {
165 >                    c.await(timeoutMillis(), MILLISECONDS);
166 >                    shouldThrow();
167 >                } catch (TimeoutException success) {}
168 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
169 >            }});
170  
171 <                    }
218 <                }
219 <            });
220 <        try {
221 <            t.start();
222 <            t.join();
223 <        } catch (InterruptedException e) {
224 <            unexpectedException();
225 <        }
171 >        awaitTermination(t);
172      }
173  
174      /**
175       * A timeout in one party causes others waiting in timed await to
176       * throw BrokenBarrierException
177       */
178 <    public void testAwait4_Timeout_BrokenBarrier() {
178 >    public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
179          final CyclicBarrier c = new CyclicBarrier(3);
180 <        Thread t1 = new Thread(new Runnable() {
181 <                public void run() {
182 <                    try {
183 <                        c.await(SHORT_DELAY_MS, MILLISECONDS);
184 <                        threadShouldThrow();
185 <                    } catch (TimeoutException success) {
186 <                    } catch (Exception b) {
187 <                        threadUnexpectedException();
188 <                    }
189 <                }
190 <            });
191 <        Thread t2 = new Thread(new Runnable() {
192 <                public void run() {
193 <                    try {
194 <                        c.await(MEDIUM_DELAY_MS, MILLISECONDS);
195 <                        threadShouldThrow();
196 <                    } catch (BrokenBarrierException success) {
197 <                    } catch (Exception i) {
198 <                        threadUnexpectedException();
199 <                    }
200 <                }
255 <            });
256 <        try {
257 <            t1.start();
258 <            t2.start();
259 <            t1.join();
260 <            t2.join();
261 <        } catch (InterruptedException e) {
262 <            unexpectedException();
263 <        }
180 >        Thread t1 = newStartedThread(new CheckedRunnable() {
181 >            public void realRun() throws Exception {
182 >                try {
183 >                    c.await(LONG_DELAY_MS, MILLISECONDS);
184 >                    shouldThrow();
185 >                } catch (BrokenBarrierException success) {}
186 >            }});
187 >        Thread t2 = newStartedThread(new CheckedRunnable() {
188 >            public void realRun() throws Exception {
189 >                while (c.getNumberWaiting() == 0)
190 >                    Thread.yield();
191 >                long startTime = System.nanoTime();
192 >                try {
193 >                    c.await(timeoutMillis(), MILLISECONDS);
194 >                    shouldThrow();
195 >                } catch (TimeoutException success) {}
196 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
197 >            }});
198 >
199 >        awaitTermination(t1);
200 >        awaitTermination(t2);
201      }
202  
203      /**
204       * A timeout in one party causes others waiting in await to
205       * throw BrokenBarrierException
206       */
207 <    public void testAwait5_Timeout_BrokenBarrier() {
207 >    public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
208          final CyclicBarrier c = new CyclicBarrier(3);
209 <        Thread t1 = new Thread(new Runnable() {
210 <                public void run() {
211 <                    try {
212 <                        c.await(SHORT_DELAY_MS, MILLISECONDS);
213 <                        threadShouldThrow();
214 <                    } catch (TimeoutException success) {
215 <                    } catch (Exception b) {
216 <                        threadUnexpectedException();
217 <                    }
218 <                }
219 <            });
220 <        Thread t2 = new Thread(new Runnable() {
221 <                public void run() {
222 <                    try {
223 <                        c.await();
224 <                        threadShouldThrow();
225 <                    } catch (BrokenBarrierException success) {
226 <                    } catch (Exception i) {
227 <                        threadUnexpectedException();
228 <                    }
229 <                }
293 <            });
294 <        try {
295 <            t1.start();
296 <            t2.start();
297 <            t1.join();
298 <            t2.join();
299 <        } catch (InterruptedException e) {
300 <            unexpectedException();
301 <        }
209 >        Thread t1 = newStartedThread(new CheckedRunnable() {
210 >            public void realRun() throws Exception {
211 >                try {
212 >                    c.await();
213 >                    shouldThrow();
214 >                } catch (BrokenBarrierException success) {}
215 >            }});
216 >        Thread t2 = newStartedThread(new CheckedRunnable() {
217 >            public void realRun() throws Exception {
218 >                while (c.getNumberWaiting() == 0)
219 >                    Thread.yield();
220 >                long startTime = System.nanoTime();
221 >                try {
222 >                    c.await(timeoutMillis(), MILLISECONDS);
223 >                    shouldThrow();
224 >                } catch (TimeoutException success) {}
225 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
226 >            }});
227 >
228 >        awaitTermination(t1);
229 >        awaitTermination(t2);
230      }
231  
232      /**
233       * A reset of an active barrier causes waiting threads to throw
234       * BrokenBarrierException
235       */
236 <    public void testReset_BrokenBarrier() {
236 >    public void testReset_BrokenBarrier() throws InterruptedException {
237          final CyclicBarrier c = new CyclicBarrier(3);
238 <        Thread t1 = new Thread(new Runnable() {
239 <                public void run() {
240 <                    try {
241 <                        c.await();
242 <                        threadShouldThrow();
243 <                    } catch (BrokenBarrierException success) {}
244 <                    catch (Exception b) {
245 <                        threadUnexpectedException();
246 <                    }
247 <                }
248 <            });
249 <        Thread t2 = new Thread(new Runnable() {
250 <                public void run() {
251 <                    try {
252 <                        c.await();
253 <                        threadShouldThrow();
254 <                    } catch (BrokenBarrierException success) {
255 <                    } 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 <        }
238 >        final CountDownLatch pleaseReset = new CountDownLatch(2);
239 >        Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
240 >            public void realRun() throws Exception {
241 >                pleaseReset.countDown();
242 >                c.await();
243 >            }};
244 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
245 >            public void realRun() throws Exception {
246 >                pleaseReset.countDown();
247 >                c.await();
248 >            }};
249 >
250 >        t1.start();
251 >        t2.start();
252 >        await(pleaseReset);
253 >        c.reset();
254 >        awaitTermination(t1);
255 >        awaitTermination(t2);
256      }
257  
258      /**
259       * A reset before threads enter barrier does not throw
260       * BrokenBarrierException
261       */
262 <    public void testReset_NoBrokenBarrier() {
262 >    public void testReset_NoBrokenBarrier() throws Exception {
263          final CyclicBarrier c = new CyclicBarrier(3);
264 <        Thread t1 = new Thread(new Runnable() {
265 <                public void run() {
266 <                    try {
267 <                        c.await();
268 <                    } catch (Exception b) {
269 <                        threadUnexpectedException();
270 <                    }
271 <                }
272 <            });
273 <        Thread t2 = new Thread(new Runnable() {
274 <                public void run() {
275 <                    try {
276 <                        c.await();
277 <                    } catch (Exception i) {
364 <                        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 <        }
264 >        c.reset();
265 >
266 >        Thread t1 = newStartedThread(new CheckedRunnable() {
267 >            public void realRun() throws Exception {
268 >                c.await();
269 >            }});
270 >        Thread t2 = newStartedThread(new CheckedRunnable() {
271 >            public void realRun() throws Exception {
272 >                c.await();
273 >            }});
274 >
275 >        c.await();
276 >        awaitTermination(t1);
277 >        awaitTermination(t2);
278      }
279  
280      /**
281       * All threads block while a barrier is broken.
282       */
283 <    public void testReset_Leakage() {
284 <        try {
285 <            final CyclicBarrier c = new CyclicBarrier(2);
286 <            final AtomicBoolean done = new AtomicBoolean();
287 <            Thread t = new Thread() {
288 <                    public void run() {
289 <                        while (!done.get()) {
290 <                            try {
291 <                                while (c.isBroken())
292 <                                    c.reset();
293 <
294 <                                c.await();
395 <                                threadFail("await should not return");
396 <                            }
397 <                            catch (BrokenBarrierException e) {
398 <                            }
399 <                            catch (InterruptedException ie) {
400 <                            }
401 <                        }
283 >    public void testReset_Leakage() throws InterruptedException {
284 >        final CyclicBarrier c = new CyclicBarrier(2);
285 >        final AtomicBoolean done = new AtomicBoolean();
286 >        Thread t = newStartedThread(new CheckedRunnable() {
287 >            public void realRun() {
288 >                while (!done.get()) {
289 >                    try {
290 >                        while (c.isBroken())
291 >                            c.reset();
292 >
293 >                        c.await();
294 >                        shouldThrow();
295                      }
296 <                };
296 >                    catch (BrokenBarrierException ok) {}
297 >                    catch (InterruptedException ok) {}
298 >                }}});
299  
300 <            t.start();
301 <            for ( int i = 0; i < 4; i++) {
407 <                Thread.sleep(SHORT_DELAY_MS);
408 <                t.interrupt();
409 <            }
410 <            done.set(true);
300 >        for (int i = 0; i < 4; i++) {
301 >            delay(timeoutMillis());
302              t.interrupt();
303          }
304 <        catch (Exception ex) {
305 <            unexpectedException();
306 <        }
304 >        done.set(true);
305 >        t.interrupt();
306 >        awaitTermination(t);
307      }
308  
309      /**
310       * Reset of a non-broken barrier does not break barrier
311       */
312 <    public void testResetWithoutBreakage() {
313 <        try {
312 >    public void testResetWithoutBreakage() throws Exception {
313 >        final CyclicBarrier barrier = new CyclicBarrier(3);
314 >        for (int i = 0; i < 3; i++) {
315              final CyclicBarrier start = new CyclicBarrier(3);
316 <            final CyclicBarrier barrier = new CyclicBarrier(3);
317 <            for (int i = 0; i < 3; i++) {
318 <                Thread t1 = new Thread(new Runnable() {
319 <                        public void run() {
320 <                            try { start.await(); }
321 <                            catch (Exception ie) {
322 <                                threadFail("start barrier");
323 <                            }
324 <                            try { barrier.await(); }
325 <                            catch (Throwable thrown) {
326 <                                unexpectedException();
327 <                            }}});
328 <
329 <                Thread t2 = 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 (Throwable thrown) {
445 <                                unexpectedException();
446 <                            }}});
447 <
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();
316 >            Thread t1 = newStartedThread(new CheckedRunnable() {
317 >                public void realRun() throws Exception {
318 >                    start.await();
319 >                    barrier.await();
320 >                }});
321 >
322 >            Thread t2 = newStartedThread(new CheckedRunnable() {
323 >                public void realRun() throws Exception {
324 >                    start.await();
325 >                    barrier.await();
326 >                }});
327 >
328 >            start.await();
329 >            barrier.await();
330 >            awaitTermination(t1);
331 >            awaitTermination(t2);
332 >            assertFalse(barrier.isBroken());
333 >            assertEquals(0, barrier.getNumberWaiting());
334 >            if (i == 1) barrier.reset();
335 >            assertFalse(barrier.isBroken());
336 >            assertEquals(0, barrier.getNumberWaiting());
337          }
338      }
339  
340      /**
341       * Reset of a barrier after interruption reinitializes it.
342       */
343 <    public void testResetAfterInterrupt() {
344 <        try {
343 >    public void testResetAfterInterrupt() throws Exception {
344 >        final CyclicBarrier barrier = new CyclicBarrier(3);
345 >        for (int i = 0; i < 2; i++) {
346              final CyclicBarrier start = new CyclicBarrier(3);
347 <            final CyclicBarrier barrier = new CyclicBarrier(3);
348 <            for (int i = 0; i < 2; i++) {
349 <                Thread t1 = new Thread(new Runnable() {
350 <                        public void run() {
351 <                            try { start.await(); }
352 <                            catch (Exception ie) {
353 <                                threadFail("start barrier");
354 <                            }
355 <                            try { barrier.await(); }
356 <                            catch (InterruptedException ok) {}
357 <                            catch (Throwable thrown) {
358 <                                unexpectedException();
359 <                            }}});
360 <
361 <                Thread t2 = new Thread(new Runnable() {
362 <                        public void run() {
363 <                            try { start.await(); }
364 <                            catch (Exception ie) {
365 <                                threadFail("start barrier");
366 <                            }
367 <                            try { barrier.await(); }
368 <                            catch (BrokenBarrierException ok) {}
369 <                            catch (Throwable thrown) {
497 <                                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();
347 >            Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
348 >                public void realRun() throws Exception {
349 >                    start.await();
350 >                    barrier.await();
351 >                }};
352 >
353 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
354 >                public void realRun() throws Exception {
355 >                    start.await();
356 >                    barrier.await();
357 >                }};
358 >
359 >            t1.start();
360 >            t2.start();
361 >            start.await();
362 >            t1.interrupt();
363 >            awaitTermination(t1);
364 >            awaitTermination(t2);
365 >            assertTrue(barrier.isBroken());
366 >            assertEquals(0, barrier.getNumberWaiting());
367 >            barrier.reset();
368 >            assertFalse(barrier.isBroken());
369 >            assertEquals(0, barrier.getNumberWaiting());
370          }
371      }
372  
373      /**
374       * Reset of a barrier after timeout reinitializes it.
375       */
376 <    public void testResetAfterTimeout() {
377 <        try {
378 <            final CyclicBarrier start = new CyclicBarrier(3);
379 <            final CyclicBarrier barrier = new CyclicBarrier(3);
380 <            for (int i = 0; i < 2; i++) {
381 <                Thread t1 = new Thread(new Runnable() {
382 <                        public void run() {
383 <                            try { start.await(); }
384 <                            catch (Exception ie) {
385 <                                threadFail("start barrier");
386 <                            }
387 <                            try { barrier.await(MEDIUM_DELAY_MS, MILLISECONDS); }
388 <                            catch (TimeoutException ok) {}
389 <                            catch (Throwable thrown) {
390 <                                unexpectedException();
391 <                            }}});
392 <
393 <                Thread t2 = new Thread(new Runnable() {
394 <                        public void run() {
395 <                            try { start.await(); }
396 <                            catch (Exception ie) {
397 <                                threadFail("start barrier");
398 <                            }
399 <                            try { barrier.await(); }
400 <                            catch (BrokenBarrierException ok) {}
401 <                            catch (Throwable thrown) {
402 <                                unexpectedException();
403 <                            }}});
404 <
405 <                t1.start();
406 <                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();
376 >    public void testResetAfterTimeout() throws Exception {
377 >        final CyclicBarrier barrier = new CyclicBarrier(3);
378 >        for (int i = 0; i < 2; i++) {
379 >            assertEquals(0, barrier.getNumberWaiting());
380 >            Thread t1 = newStartedThread(new CheckedRunnable() {
381 >                public void realRun() throws Exception {
382 >                    try {
383 >                        barrier.await();
384 >                        shouldThrow();
385 >                    } catch (BrokenBarrierException success) {}
386 >                }});
387 >            Thread t2 = newStartedThread(new CheckedRunnable() {
388 >                public void realRun() throws Exception {
389 >                    while (barrier.getNumberWaiting() == 0)
390 >                        Thread.yield();
391 >                    long startTime = System.nanoTime();
392 >                    try {
393 >                        barrier.await(timeoutMillis(), MILLISECONDS);
394 >                        shouldThrow();
395 >                    } catch (TimeoutException success) {}
396 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
397 >                }});
398 >
399 >            awaitTermination(t1);
400 >            awaitTermination(t2);
401 >            assertEquals(0, barrier.getNumberWaiting());
402 >            assertTrue(barrier.isBroken());
403 >            assertEquals(0, barrier.getNumberWaiting());
404 >            barrier.reset();
405 >            assertFalse(barrier.isBroken());
406 >            assertEquals(0, barrier.getNumberWaiting());
407          }
408      }
409  
569
410      /**
411       * Reset of a barrier after a failed command reinitializes it.
412       */
413 <    public void testResetAfterCommandException() {
414 <        try {
413 >    public void testResetAfterCommandException() throws Exception {
414 >        final CyclicBarrier barrier =
415 >            new CyclicBarrier(3, new Runnable() {
416 >                    public void run() {
417 >                        throw new NullPointerException(); }});
418 >        for (int i = 0; i < 2; i++) {
419              final CyclicBarrier start = new CyclicBarrier(3);
420 <            final CyclicBarrier barrier =
421 <                new CyclicBarrier(3, new Runnable() {
422 <                        public void run() {
423 <                            throw new NullPointerException(); }});
424 <            for (int i = 0; i < 2; 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 (BrokenBarrierException ok) {}
433 <                            catch (Throwable thrown) {
434 <                                unexpectedException();
435 <                            }}});
436 <
437 <                Thread t2 = new Thread(new Runnable() {
438 <                        public void run() {
439 <                            try { start.await(); }
440 <                            catch (Exception ie) {
441 <                                threadFail("start barrier");
442 <                            }
443 <                            try { barrier.await(); }
444 <                            catch (BrokenBarrierException ok) {}
445 <                            catch (Throwable thrown) {
446 <                                unexpectedException();
603 <                            }}});
604 <
605 <                t1.start();
606 <                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();
420 >            Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
421 >                public void realRun() throws Exception {
422 >                    start.await();
423 >                    barrier.await();
424 >                }};
425 >
426 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
427 >                public void realRun() throws Exception {
428 >                    start.await();
429 >                    barrier.await();
430 >                }};
431 >
432 >            t1.start();
433 >            t2.start();
434 >            start.await();
435 >            while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
436 >            try {
437 >                barrier.await();
438 >                shouldThrow();
439 >            } catch (NullPointerException success) {}
440 >            awaitTermination(t1);
441 >            awaitTermination(t2);
442 >            assertTrue(barrier.isBroken());
443 >            assertEquals(0, barrier.getNumberWaiting());
444 >            barrier.reset();
445 >            assertFalse(barrier.isBroken());
446 >            assertEquals(0, barrier.getNumberWaiting());
447          }
448      }
449   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines