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.9 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.27 by jsr166, Sat Apr 25 04:55:30 2015 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   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.util.concurrent.locks.*;
13 < import java.util.concurrent.atomic.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 < public class CyclicBarrierTest extends JSR166TestCase{
11 > import java.util.concurrent.BrokenBarrierException;
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.CyclicBarrier;
14 > import java.util.concurrent.TimeoutException;
15 > import java.util.concurrent.atomic.AtomicBoolean;
16 >
17 > import junit.framework.Test;
18 > import junit.framework.TestSuite;
19 >
20 > public class CyclicBarrierTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());
22 >        main(suite(), args);
23      }
24      public static Test suite() {
25 <        return new TestSuite(CyclicBarrierTest.class);
25 >        return new TestSuite(CyclicBarrierTest.class);
26      }
27  
28      private volatile int countAction;
# Line 26 | Line 31 | public class CyclicBarrierTest extends J
31      }
32  
33      /**
34 +     * Spin-waits till the number of waiters == numberOfWaiters.
35 +     */
36 +    void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) {
37 +        long startTime = System.nanoTime();
38 +        while (barrier.getNumberWaiting() != numberOfWaiters) {
39 +            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
40 +                fail("timed out");
41 +            Thread.yield();
42 +        }
43 +    }
44 +
45 +    /**
46       * Creating with negative parties throws IAE
47       */
48      public void testConstructor1() {
49          try {
50              new CyclicBarrier(-1, (Runnable)null);
51              shouldThrow();
52 <        } catch(IllegalArgumentException e){}
52 >        } catch (IllegalArgumentException success) {}
53      }
54  
55      /**
# Line 42 | Line 59 | public class CyclicBarrierTest extends J
59          try {
60              new CyclicBarrier(-1);
61              shouldThrow();
62 <        } catch(IllegalArgumentException e){}
62 >        } catch (IllegalArgumentException success) {}
63      }
64  
65      /**
# Line 50 | Line 67 | public class CyclicBarrierTest extends J
67       */
68      public void testGetParties() {
69          CyclicBarrier b = new CyclicBarrier(2);
70 <        assertEquals(2, b.getParties());
70 >        assertEquals(2, b.getParties());
71          assertEquals(0, b.getNumberWaiting());
72      }
73  
74      /**
75       * A 1-party barrier triggers after single await
76       */
77 <    public void testSingleParty() {
78 <        try {
79 <            CyclicBarrier b = new CyclicBarrier(1);
80 <            assertEquals(1, b.getParties());
81 <            assertEquals(0, b.getNumberWaiting());
82 <            b.await();
83 <            b.await();
67 <            assertEquals(0, b.getNumberWaiting());
68 <        }
69 <        catch(Exception e) {
70 <            unexpectedException();
71 <        }
77 >    public void testSingleParty() throws Exception {
78 >        CyclicBarrier b = new CyclicBarrier(1);
79 >        assertEquals(1, b.getParties());
80 >        assertEquals(0, b.getNumberWaiting());
81 >        b.await();
82 >        b.await();
83 >        assertEquals(0, b.getNumberWaiting());
84      }
85  
86      /**
87       * The supplied barrier action is run at barrier
88       */
89 <    public void testBarrierAction() {
90 <        try {
91 <            countAction = 0;
92 <            CyclicBarrier b = new CyclicBarrier(1, new MyAction());
93 <            assertEquals(1, b.getParties());
94 <            assertEquals(0, b.getNumberWaiting());
95 <            b.await();
96 <            b.await();
97 <            assertEquals(0, b.getNumberWaiting());
86 <            assertEquals(countAction, 2);
87 <        }
88 <        catch(Exception e) {
89 <            unexpectedException();
90 <        }
89 >    public void testBarrierAction() throws Exception {
90 >        countAction = 0;
91 >        CyclicBarrier b = new CyclicBarrier(1, new MyAction());
92 >        assertEquals(1, b.getParties());
93 >        assertEquals(0, b.getNumberWaiting());
94 >        b.await();
95 >        b.await();
96 >        assertEquals(0, b.getNumberWaiting());
97 >        assertEquals(2, countAction);
98      }
99  
100      /**
101       * A 2-party/thread barrier triggers after both threads invoke await
102       */
103 <    public void testTwoParties() {
103 >    public void testTwoParties() throws Exception {
104          final CyclicBarrier b = new CyclicBarrier(2);
105 <        Thread t = new Thread(new Runnable() {
106 <                public void run() {
107 <                    try {
108 <                        b.await();
109 <                        b.await();
110 <                        b.await();
111 <                        b.await();
112 <                    } catch(Exception e){
113 <                        threadUnexpectedException();
114 <                    }}});
115 <
116 <        try {
117 <            t.start();
111 <            b.await();
112 <            b.await();
113 <            b.await();
114 <            b.await();
115 <            t.join();
116 <        } catch(Exception e){
117 <            unexpectedException();
118 <        }
105 >        Thread t = newStartedThread(new CheckedRunnable() {
106 >            public void realRun() throws Exception {
107 >                b.await();
108 >                b.await();
109 >                b.await();
110 >                b.await();
111 >            }});
112 >
113 >        b.await();
114 >        b.await();
115 >        b.await();
116 >        b.await();
117 >        awaitTermination(t);
118      }
119  
121
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 {
129 <                        c.await();
130 <                        threadShouldThrow();
131 <                    } catch(InterruptedException success){}
132 <                    catch(Exception b){
133 <                        threadUnexpectedException();
134 <                    }
135 <                }
136 <            });
137 <        Thread t2 = new Thread(new Runnable() {
138 <                public void run() {
139 <                    try {
140 <                        c.await();
141 <                        threadShouldThrow();
142 <                    } catch(BrokenBarrierException success){
143 <                    } catch(Exception i){
146 <                        threadUnexpectedException();
147 <                    }
148 <                }
149 <            });
150 <        try {
151 <            t1.start();
152 <            t2.start();
153 <            Thread.sleep(SHORT_DELAY_MS);
154 <            t1.interrupt();
155 <            t1.join();
156 <            t2.join();
157 <        } catch(InterruptedException e){
158 <            unexpectedException();
159 <        }
126 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
127 >        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
128 >            public void realRun() throws Exception {
129 >                pleaseInterrupt.countDown();
130 >                c.await();
131 >            }};
132 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
133 >            public void realRun() throws Exception {
134 >                pleaseInterrupt.countDown();
135 >                c.await();
136 >            }};
137 >
138 >        t1.start();
139 >        t2.start();
140 >        await(pleaseInterrupt);
141 >        t1.interrupt();
142 >        awaitTermination(t1);
143 >        awaitTermination(t2);
144      }
145  
146      /**
147       * An interruption in one party causes others waiting in timed await to
148       * throw BrokenBarrierException
149       */
150 <    public void testAwait2_Interrupted_BrokenBarrier() {
150 >    public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
151          final CyclicBarrier c = new CyclicBarrier(3);
152 <        Thread t1 = new Thread(new Runnable() {
153 <                public void run() {
154 <                    try {
155 <                        c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
156 <                        threadShouldThrow();
157 <                    } catch(InterruptedException success){
158 <                    } catch(Exception b){
159 <                        threadUnexpectedException();
160 <                    }
161 <                }
162 <            });
163 <        Thread t2 = new Thread(new Runnable() {
164 <                public void run() {
165 <                    try {
166 <                        c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
167 <                        threadShouldThrow();
168 <                    } catch(BrokenBarrierException success){
169 <                    } catch(Exception i){
186 <                        threadUnexpectedException();
187 <                    }
188 <                }
189 <            });
190 <        try {
191 <            t1.start();
192 <            t2.start();
193 <            Thread.sleep(SHORT_DELAY_MS);
194 <            t1.interrupt();
195 <            t1.join();
196 <            t2.join();
197 <        } catch(InterruptedException e){
198 <            unexpectedException();
199 <        }
152 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
153 >        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
154 >            public void realRun() throws Exception {
155 >                pleaseInterrupt.countDown();
156 >                c.await(LONG_DELAY_MS, MILLISECONDS);
157 >            }};
158 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
159 >            public void realRun() throws Exception {
160 >                pleaseInterrupt.countDown();
161 >                c.await(LONG_DELAY_MS, MILLISECONDS);
162 >            }};
163 >
164 >        t1.start();
165 >        t2.start();
166 >        await(pleaseInterrupt);
167 >        t1.interrupt();
168 >        awaitTermination(t1);
169 >        awaitTermination(t2);
170      }
171  
172      /**
173       * A timeout in timed await throws TimeoutException
174       */
175 <    public void testAwait3_TimeOutException() {
175 >    public void testAwait3_TimeoutException() throws InterruptedException {
176          final CyclicBarrier c = new CyclicBarrier(2);
177 <        Thread t = new Thread(new Runnable() {
178 <                public void run() {
179 <                    try {
180 <                        c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
181 <                        threadShouldThrow();
182 <                    } catch(TimeoutException success){
183 <                    } catch(Exception b){
184 <                        threadUnexpectedException();
177 >        Thread t = newStartedThread(new CheckedRunnable() {
178 >            public void realRun() throws Exception {
179 >                long startTime = System.nanoTime();
180 >                try {
181 >                    c.await(timeoutMillis(), MILLISECONDS);
182 >                    shouldThrow();
183 >                } catch (TimeoutException success) {}
184 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
185 >            }});
186  
187 <                    }
217 <                }
218 <            });
219 <        try {
220 <            t.start();
221 <            t.join();
222 <        } catch(InterruptedException e){
223 <            unexpectedException();
224 <        }
187 >        awaitTermination(t);
188      }
189  
190      /**
191       * A timeout in one party causes others waiting in timed await to
192       * throw BrokenBarrierException
193       */
194 <    public void testAwait4_Timeout_BrokenBarrier() {
194 >    public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
195          final CyclicBarrier c = new CyclicBarrier(3);
196 <        Thread t1 = new Thread(new Runnable() {
197 <                public void run() {
198 <                    try {
199 <                        c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
200 <                        threadShouldThrow();
201 <                    } catch(TimeoutException success){
202 <                    } catch(Exception b){
203 <                        threadUnexpectedException();
204 <                    }
205 <                }
206 <            });
207 <        Thread t2 = new Thread(new Runnable() {
208 <                public void run() {
209 <                    try {
210 <                        c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
211 <                        threadShouldThrow();
212 <                    } catch(BrokenBarrierException success){
213 <                    } catch(Exception i){
214 <                        threadUnexpectedException();
215 <                    }
253 <                }
254 <            });
255 <        try {
256 <            t1.start();
257 <            t2.start();
258 <            t1.join();
259 <            t2.join();
260 <        } catch(InterruptedException e){
261 <            unexpectedException();
262 <        }
196 >        Thread t1 = newStartedThread(new CheckedRunnable() {
197 >            public void realRun() throws Exception {
198 >                try {
199 >                    c.await(LONG_DELAY_MS, MILLISECONDS);
200 >                    shouldThrow();
201 >                } catch (BrokenBarrierException success) {}
202 >            }});
203 >        Thread t2 = newStartedThread(new CheckedRunnable() {
204 >            public void realRun() throws Exception {
205 >                awaitNumberWaiting(c, 1);
206 >                long startTime = System.nanoTime();
207 >                try {
208 >                    c.await(timeoutMillis(), MILLISECONDS);
209 >                    shouldThrow();
210 >                } catch (TimeoutException success) {}
211 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
212 >            }});
213 >
214 >        awaitTermination(t1);
215 >        awaitTermination(t2);
216      }
217  
218      /**
219       * A timeout in one party causes others waiting in await to
220       * throw BrokenBarrierException
221       */
222 <    public void testAwait5_Timeout_BrokenBarrier() {
222 >    public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
223          final CyclicBarrier c = new CyclicBarrier(3);
224 <        Thread t1 = new Thread(new Runnable() {
225 <                public void run() {
226 <                    try {
227 <                        c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
228 <                        threadShouldThrow();
229 <                    } catch(TimeoutException success){
230 <                    } catch(Exception b){
231 <                        threadUnexpectedException();
232 <                    }
233 <                }
234 <            });
235 <        Thread t2 = new Thread(new Runnable() {
236 <                public void run() {
237 <                    try {
238 <                        c.await();
239 <                        threadShouldThrow();
240 <                    } catch(BrokenBarrierException success){
241 <                    } catch(Exception i){
242 <                        threadUnexpectedException();
243 <                    }
291 <                }
292 <            });
293 <        try {
294 <            t1.start();
295 <            t2.start();
296 <            t1.join();
297 <            t2.join();
298 <        } catch(InterruptedException e){
299 <            unexpectedException();
300 <        }
224 >        Thread t1 = newStartedThread(new CheckedRunnable() {
225 >            public void realRun() throws Exception {
226 >                try {
227 >                    c.await();
228 >                    shouldThrow();
229 >                } catch (BrokenBarrierException success) {}
230 >            }});
231 >        Thread t2 = newStartedThread(new CheckedRunnable() {
232 >            public void realRun() throws Exception {
233 >                awaitNumberWaiting(c, 1);
234 >                long startTime = System.nanoTime();
235 >                try {
236 >                    c.await(timeoutMillis(), MILLISECONDS);
237 >                    shouldThrow();
238 >                } catch (TimeoutException success) {}
239 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
240 >            }});
241 >
242 >        awaitTermination(t1);
243 >        awaitTermination(t2);
244      }
245  
246      /**
247       * A reset of an active barrier causes waiting threads to throw
248       * BrokenBarrierException
249       */
250 <    public void testReset_BrokenBarrier() {
250 >    public void testReset_BrokenBarrier() throws InterruptedException {
251          final CyclicBarrier c = new CyclicBarrier(3);
252 <        Thread t1 = new Thread(new Runnable() {
253 <                public void run() {
254 <                    try {
255 <                        c.await();
256 <                        threadShouldThrow();
257 <                    } catch(BrokenBarrierException success){}
258 <                    catch(Exception b){
259 <                        threadUnexpectedException();
260 <                    }
261 <                }
262 <            });
263 <        Thread t2 = new Thread(new Runnable() {
264 <                public void run() {
265 <                    try {
266 <                        c.await();
267 <                        threadShouldThrow();
268 <                    } catch(BrokenBarrierException success){
269 <                    } catch(Exception i){
270 <                        threadUnexpectedException();
271 <                    }
329 <                }
330 <            });
331 <        try {
332 <            t1.start();
333 <            t2.start();
334 <            Thread.sleep(SHORT_DELAY_MS);
335 <            c.reset();
336 <            t1.join();
337 <            t2.join();
338 <        } catch(InterruptedException e){
339 <            unexpectedException();
340 <        }
252 >        final CountDownLatch pleaseReset = new CountDownLatch(2);
253 >        Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
254 >            public void realRun() throws Exception {
255 >                pleaseReset.countDown();
256 >                c.await();
257 >            }};
258 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
259 >            public void realRun() throws Exception {
260 >                pleaseReset.countDown();
261 >                c.await();
262 >            }};
263 >
264 >        t1.start();
265 >        t2.start();
266 >        await(pleaseReset);
267 >
268 >        awaitNumberWaiting(c, 2);
269 >        c.reset();
270 >        awaitTermination(t1);
271 >        awaitTermination(t2);
272      }
273  
274      /**
275       * A reset before threads enter barrier does not throw
276       * BrokenBarrierException
277       */
278 <    public void testReset_NoBrokenBarrier() {
278 >    public void testReset_NoBrokenBarrier() throws Exception {
279          final CyclicBarrier c = new CyclicBarrier(3);
280 <        Thread t1 = new Thread(new Runnable() {
281 <                public void run() {
282 <                    try {
283 <                        c.await();
284 <                    } catch(Exception b){
285 <                        threadUnexpectedException();
286 <                    }
287 <                }
288 <            });
289 <        Thread t2 = new Thread(new Runnable() {
290 <                public void run() {
291 <                    try {
292 <                        c.await();
293 <                    } catch(Exception i){
363 <                        threadUnexpectedException();
364 <                    }
365 <                }
366 <            });
367 <        try {
368 <            c.reset();
369 <            t1.start();
370 <            t2.start();
371 <            c.await();
372 <            t1.join();
373 <            t2.join();
374 <        } catch(Exception e){
375 <            unexpectedException();
376 <        }
280 >        c.reset();
281 >
282 >        Thread t1 = newStartedThread(new CheckedRunnable() {
283 >            public void realRun() throws Exception {
284 >                c.await();
285 >            }});
286 >        Thread t2 = newStartedThread(new CheckedRunnable() {
287 >            public void realRun() throws Exception {
288 >                c.await();
289 >            }});
290 >
291 >        c.await();
292 >        awaitTermination(t1);
293 >        awaitTermination(t2);
294      }
295  
296      /**
297       * All threads block while a barrier is broken.
298       */
299 <    public void testReset_Leakage() {
300 <        try {
301 <            final CyclicBarrier c = new CyclicBarrier(2);
302 <            final AtomicBoolean done = new AtomicBoolean();
303 <            Thread t = new Thread() {
304 <                    public void run() {
305 <                        while (!done.get()) {
306 <                            try {
307 <                                while (c.isBroken())
308 <                                    c.reset();
309 <
310 <                                c.await();
394 <                                threadFail("await should not return");
395 <                            }
396 <                            catch (BrokenBarrierException e) {
397 <                            }
398 <                            catch (InterruptedException ie) {
399 <                            }
400 <                        }
299 >    public void testReset_Leakage() throws InterruptedException {
300 >        final CyclicBarrier c = new CyclicBarrier(2);
301 >        final AtomicBoolean done = new AtomicBoolean();
302 >        Thread t = newStartedThread(new CheckedRunnable() {
303 >            public void realRun() {
304 >                while (!done.get()) {
305 >                    try {
306 >                        while (c.isBroken())
307 >                            c.reset();
308 >
309 >                        c.await();
310 >                        shouldThrow();
311                      }
312 <                };
312 >                    catch (BrokenBarrierException ok) {}
313 >                    catch (InterruptedException ok) {}
314 >                }}});
315  
316 <            t.start();
317 <            for( int i = 0; i < 4; i++) {
406 <                Thread.sleep(SHORT_DELAY_MS);
407 <                t.interrupt();
408 <            }
409 <            done.set(true);
316 >        for (int i = 0; i < 4; i++) {
317 >            delay(timeoutMillis());
318              t.interrupt();
319          }
320 <        catch (Exception ex) {
321 <            unexpectedException();
322 <        }
320 >        done.set(true);
321 >        t.interrupt();
322 >        awaitTermination(t);
323      }
324  
325      /**
326       * Reset of a non-broken barrier does not break barrier
327       */
328 <    public void testResetWithoutBreakage() {
329 <        try {
328 >    public void testResetWithoutBreakage() throws Exception {
329 >        final CyclicBarrier barrier = new CyclicBarrier(3);
330 >        for (int i = 0; i < 3; i++) {
331              final CyclicBarrier start = new CyclicBarrier(3);
332 <            final CyclicBarrier barrier = new CyclicBarrier(3);
333 <            for (int i = 0; i < 3; i++) {
334 <                Thread t1 = new Thread(new Runnable() {
335 <                        public void run() {
336 <                            try { start.await(); }
337 <                            catch (Exception ie) {
338 <                                threadFail("start barrier");
339 <                            }
340 <                            try { barrier.await(); }
341 <                            catch (Throwable thrown) {
342 <                                unexpectedException();
343 <                            }}});
344 <
345 <                Thread t2 = new Thread(new Runnable() {
346 <                        public void run() {
347 <                            try { start.await(); }
348 <                            catch (Exception ie) {
349 <                                threadFail("start barrier");
350 <                            }
351 <                            try { barrier.await(); }
352 <                            catch (Throwable thrown) {
444 <                                unexpectedException();
445 <                            }}});
446 <
447 <
448 <                t1.start();
449 <                t2.start();
450 <                try { start.await(); }
451 <                catch (Exception ie) { threadFail("start barrier"); }
452 <                barrier.await();
453 <                t1.join();
454 <                t2.join();
455 <                assertFalse(barrier.isBroken());
456 <                assertEquals(0, barrier.getNumberWaiting());
457 <                if (i == 1) barrier.reset();
458 <                assertFalse(barrier.isBroken());
459 <                assertEquals(0, barrier.getNumberWaiting());
460 <            }
461 <        }
462 <        catch (Exception ex) {
463 <            unexpectedException();
332 >            Thread t1 = newStartedThread(new CheckedRunnable() {
333 >                public void realRun() throws Exception {
334 >                    start.await();
335 >                    barrier.await();
336 >                }});
337 >
338 >            Thread t2 = newStartedThread(new CheckedRunnable() {
339 >                public void realRun() throws Exception {
340 >                    start.await();
341 >                    barrier.await();
342 >                }});
343 >
344 >            start.await();
345 >            barrier.await();
346 >            awaitTermination(t1);
347 >            awaitTermination(t2);
348 >            assertFalse(barrier.isBroken());
349 >            assertEquals(0, barrier.getNumberWaiting());
350 >            if (i == 1) barrier.reset();
351 >            assertFalse(barrier.isBroken());
352 >            assertEquals(0, barrier.getNumberWaiting());
353          }
354      }
355  
356      /**
357       * Reset of a barrier after interruption reinitializes it.
358       */
359 <    public void testResetAfterInterrupt() {
360 <        try {
359 >    public void testResetAfterInterrupt() throws Exception {
360 >        final CyclicBarrier barrier = new CyclicBarrier(3);
361 >        for (int i = 0; i < 2; i++) {
362              final CyclicBarrier start = new CyclicBarrier(3);
363 <            final CyclicBarrier barrier = new CyclicBarrier(3);
364 <            for (int i = 0; i < 2; i++) {
365 <                Thread t1 = new Thread(new Runnable() {
366 <                        public void run() {
367 <                            try { start.await(); }
368 <                            catch (Exception ie) {
369 <                                threadFail("start barrier");
370 <                            }
371 <                            try { barrier.await(); }
372 <                            catch(InterruptedException ok) {}
373 <                            catch (Throwable thrown) {
374 <                                unexpectedException();
375 <                            }}});
376 <
377 <                Thread t2 = new Thread(new Runnable() {
378 <                        public void run() {
379 <                            try { start.await(); }
380 <                            catch (Exception ie) {
381 <                                threadFail("start barrier");
382 <                            }
383 <                            try { barrier.await(); }
384 <                            catch(BrokenBarrierException ok) {}
385 <                            catch (Throwable thrown) {
496 <                                unexpectedException();
497 <                            }}});
498 <
499 <                t1.start();
500 <                t2.start();
501 <                try { start.await(); }
502 <                catch (Exception ie) { threadFail("start barrier"); }
503 <                t1.interrupt();
504 <                t1.join();
505 <                t2.join();
506 <                assertTrue(barrier.isBroken());
507 <                assertEquals(0, barrier.getNumberWaiting());
508 <                barrier.reset();
509 <                assertFalse(barrier.isBroken());
510 <                assertEquals(0, barrier.getNumberWaiting());
511 <            }
512 <        }
513 <        catch (Exception ex) {
514 <            unexpectedException();
363 >            Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
364 >                public void realRun() throws Exception {
365 >                    start.await();
366 >                    barrier.await();
367 >                }};
368 >
369 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
370 >                public void realRun() throws Exception {
371 >                    start.await();
372 >                    barrier.await();
373 >                }};
374 >
375 >            t1.start();
376 >            t2.start();
377 >            start.await();
378 >            t1.interrupt();
379 >            awaitTermination(t1);
380 >            awaitTermination(t2);
381 >            assertTrue(barrier.isBroken());
382 >            assertEquals(0, barrier.getNumberWaiting());
383 >            barrier.reset();
384 >            assertFalse(barrier.isBroken());
385 >            assertEquals(0, barrier.getNumberWaiting());
386          }
387      }
388  
389      /**
390       * Reset of a barrier after timeout reinitializes it.
391       */
392 <    public void testResetAfterTimeout() {
393 <        try {
394 <            final CyclicBarrier start = new CyclicBarrier(3);
395 <            final CyclicBarrier barrier = new CyclicBarrier(3);
396 <            for (int i = 0; i < 2; i++) {
397 <                Thread t1 = new Thread(new Runnable() {
398 <                        public void run() {
399 <                            try { start.await(); }
400 <                            catch (Exception ie) {
401 <                                threadFail("start barrier");
402 <                            }
403 <                            try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); }
404 <                            catch(TimeoutException ok) {}
405 <                            catch (Throwable thrown) {
406 <                                unexpectedException();
407 <                            }}});
408 <
409 <                Thread t2 = new Thread(new Runnable() {
410 <                        public void run() {
411 <                            try { start.await(); }
412 <                            catch (Exception ie) {
413 <                                threadFail("start barrier");
414 <                            }
415 <                            try { barrier.await(); }
416 <                            catch(BrokenBarrierException ok) {}
417 <                            catch (Throwable thrown) {
418 <                                unexpectedException();
419 <                            }}});
420 <
421 <                t1.start();
551 <                t2.start();
552 <                try { start.await(); }
553 <                catch (Exception ie) { threadFail("start barrier"); }
554 <                t1.join();
555 <                t2.join();
556 <                assertTrue(barrier.isBroken());
557 <                assertEquals(0, barrier.getNumberWaiting());
558 <                barrier.reset();
559 <                assertFalse(barrier.isBroken());
560 <                assertEquals(0, barrier.getNumberWaiting());
561 <            }
562 <        }
563 <        catch (Exception ex) {
564 <            unexpectedException();
392 >    public void testResetAfterTimeout() throws Exception {
393 >        final CyclicBarrier barrier = new CyclicBarrier(3);
394 >        for (int i = 0; i < 2; i++) {
395 >            assertEquals(0, barrier.getNumberWaiting());
396 >            Thread t1 = newStartedThread(new CheckedRunnable() {
397 >                public void realRun() throws Exception {
398 >                    try {
399 >                        barrier.await();
400 >                        shouldThrow();
401 >                    } catch (BrokenBarrierException success) {}
402 >                }});
403 >            Thread t2 = newStartedThread(new CheckedRunnable() {
404 >                public void realRun() throws Exception {
405 >                    awaitNumberWaiting(barrier, 1);
406 >                    long startTime = System.nanoTime();
407 >                    try {
408 >                        barrier.await(timeoutMillis(), MILLISECONDS);
409 >                        shouldThrow();
410 >                    } catch (TimeoutException success) {}
411 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
412 >                }});
413 >
414 >            awaitTermination(t1);
415 >            awaitTermination(t2);
416 >            assertEquals(0, barrier.getNumberWaiting());
417 >            assertTrue(barrier.isBroken());
418 >            assertEquals(0, barrier.getNumberWaiting());
419 >            barrier.reset();
420 >            assertFalse(barrier.isBroken());
421 >            assertEquals(0, barrier.getNumberWaiting());
422          }
423      }
424  
568
425      /**
426       * Reset of a barrier after a failed command reinitializes it.
427       */
428 <    public void testResetAfterCommandException() {
429 <        try {
428 >    public void testResetAfterCommandException() throws Exception {
429 >        final CyclicBarrier barrier =
430 >            new CyclicBarrier(3, new Runnable() {
431 >                    public void run() {
432 >                        throw new NullPointerException(); }});
433 >        for (int i = 0; i < 2; i++) {
434              final CyclicBarrier start = new CyclicBarrier(3);
435 <            final CyclicBarrier barrier =
436 <                new CyclicBarrier(3, new Runnable() {
437 <                        public void run() {
438 <                            throw new NullPointerException(); }});
439 <            for (int i = 0; i < 2; i++) {
440 <                Thread t1 = new Thread(new Runnable() {
441 <                        public void run() {
442 <                            try { start.await(); }
443 <                            catch (Exception ie) {
444 <                                threadFail("start barrier");
445 <                            }
446 <                            try { barrier.await(); }
447 <                            catch(BrokenBarrierException ok) {}
448 <                            catch (Throwable thrown) {
449 <                                unexpectedException();
450 <                            }}});
451 <
452 <                Thread t2 = new Thread(new Runnable() {
453 <                        public void run() {
454 <                            try { start.await(); }
455 <                            catch (Exception ie) {
456 <                                threadFail("start barrier");
457 <                            }
458 <                            try { barrier.await(); }
459 <                            catch(BrokenBarrierException ok) {}
460 <                            catch (Throwable thrown) {
461 <                                unexpectedException();
602 <                            }}});
603 <
604 <                t1.start();
605 <                t2.start();
606 <                try { start.await(); }
607 <                catch (Exception ie) { threadFail("start barrier"); }
608 <                while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
609 <                try { barrier.await(); }
610 <                catch (Exception ok) { }
611 <                t1.join();
612 <                t2.join();
613 <                assertTrue(barrier.isBroken());
614 <                assertEquals(0, barrier.getNumberWaiting());
615 <                barrier.reset();
616 <                assertFalse(barrier.isBroken());
617 <                assertEquals(0, barrier.getNumberWaiting());
618 <            }
619 <        }
620 <        catch (Exception ex) {
621 <            unexpectedException();
435 >            Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
436 >                public void realRun() throws Exception {
437 >                    start.await();
438 >                    barrier.await();
439 >                }};
440 >
441 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
442 >                public void realRun() throws Exception {
443 >                    start.await();
444 >                    barrier.await();
445 >                }};
446 >
447 >            t1.start();
448 >            t2.start();
449 >            start.await();
450 >            awaitNumberWaiting(barrier, 2);
451 >            try {
452 >                barrier.await();
453 >                shouldThrow();
454 >            } catch (NullPointerException success) {}
455 >            awaitTermination(t1);
456 >            awaitTermination(t2);
457 >            assertTrue(barrier.isBroken());
458 >            assertEquals(0, barrier.getNumberWaiting());
459 >            barrier.reset();
460 >            assertFalse(barrier.isBroken());
461 >            assertEquals(0, barrier.getNumberWaiting());
462          }
463      }
464   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines