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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines