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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines