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.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.20 by jsr166, Fri May 27 20:07:24 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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/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.*;
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;
25      private class MyAction implements Runnable {
26          public void run() { ++countAction; }
27      }
28 <    
28 >
29      /**
30 <     *
30 >     * Creating with negative parties throws IAE
31       */
32      public void testConstructor1() {
33          try {
34              new CyclicBarrier(-1, (Runnable)null);
35              shouldThrow();
36 <        } catch(IllegalArgumentException e){}
36 >        } catch (IllegalArgumentException success) {}
37      }
38  
39      /**
40 <     *
40 >     * Creating with negative parties and no action throws IAE
41       */
42      public void testConstructor2() {
43          try {
44              new CyclicBarrier(-1);
45              shouldThrow();
46 <        } catch(IllegalArgumentException e){}
46 >        } catch (IllegalArgumentException success) {}
47      }
48  
49      /**
50 <     *
50 >     * getParties returns the number of parties given in constructor
51       */
52 <    public void testConstructor3() {
52 >    public void testGetParties() {
53          CyclicBarrier b = new CyclicBarrier(2);
54 <        assertEquals(2, b.getParties());
54 >        assertEquals(2, b.getParties());
55          assertEquals(0, b.getNumberWaiting());
56      }
57  
58      /**
59 <     *
59 >     * A 1-party barrier triggers after single await
60       */
61 <    public void testSingleParty() {
62 <        try {
63 <            CyclicBarrier b = new CyclicBarrier(1);
64 <            assertEquals(1, b.getParties());
65 <            assertEquals(0, b.getNumberWaiting());
66 <            b.await();
67 <            b.await();
64 <            assertEquals(0, b.getNumberWaiting());
65 <        }
66 <        catch(Exception e) {
67 <            unexpectedException();
68 <        }
61 >    public void testSingleParty() throws Exception {
62 >        CyclicBarrier b = new CyclicBarrier(1);
63 >        assertEquals(1, b.getParties());
64 >        assertEquals(0, b.getNumberWaiting());
65 >        b.await();
66 >        b.await();
67 >        assertEquals(0, b.getNumberWaiting());
68      }
69 <    
69 >
70      /**
71 <     *
71 >     * The supplied barrier action is run at barrier
72       */
73 <    public void testBarrierAction() {
74 <        try {
75 <            countAction = 0;
76 <            CyclicBarrier b = new CyclicBarrier(1, new MyAction());
77 <            assertEquals(1, b.getParties());
78 <            assertEquals(0, b.getNumberWaiting());
79 <            b.await();
80 <            b.await();
81 <            assertEquals(0, b.getNumberWaiting());
83 <            assertEquals(countAction, 2);
84 <        }
85 <        catch(Exception e) {
86 <            unexpectedException();
87 <        }
73 >    public void testBarrierAction() throws Exception {
74 >        countAction = 0;
75 >        CyclicBarrier b = new CyclicBarrier(1, new MyAction());
76 >        assertEquals(1, b.getParties());
77 >        assertEquals(0, b.getNumberWaiting());
78 >        b.await();
79 >        b.await();
80 >        assertEquals(0, b.getNumberWaiting());
81 >        assertEquals(countAction, 2);
82      }
83  
90
84      /**
85 <     *
85 >     * A 2-party/thread barrier triggers after both threads invoke await
86       */
87 <    public void testTwoParties() {
87 >    public void testTwoParties() throws Exception {
88          final CyclicBarrier b = new CyclicBarrier(2);
89 <        Thread t = new Thread(new Runnable() {
90 <                public void run() {
91 <                    try {
92 <                        b.await();
93 <                        b.await();
94 <                        b.await();
95 <                        b.await();
96 <                    } catch(Exception e){
97 <                        threadUnexpectedException();
98 <                    }}});
89 >        Thread t = new Thread(new CheckedRunnable() {
90 >            public void realRun() throws Exception {
91 >                b.await();
92 >                b.await();
93 >                b.await();
94 >                b.await();
95 >            }});
96 >
97 >        t.start();
98 >        b.await();
99 >        b.await();
100 >        b.await();
101 >        b.await();
102 >        t.join();
103 >    }
104  
105 <        try {
106 <            t.start();
107 <            b.await();
108 <            b.await();
109 <            b.await();
110 <            b.await();
111 <            t.join();
112 <        } catch(Exception e){
113 <            unexpectedException();
114 <        }
105 >    /**
106 >     * An interruption in one party causes others waiting in await to
107 >     * throw BrokenBarrierException
108 >     */
109 >    public void testAwait1_Interrupted_BrokenBarrier() throws Exception {
110 >        final CyclicBarrier c = new CyclicBarrier(3);
111 >        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
112 >            public void realRun() throws Exception {
113 >                c.await();
114 >            }};
115 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
116 >            public void realRun() throws Exception {
117 >                c.await();
118 >            }};
119 >
120 >        t1.start();
121 >        t2.start();
122 >        delay(SHORT_DELAY_MS);
123 >        t1.interrupt();
124 >        t1.join();
125 >        t2.join();
126      }
127  
128 +    /**
129 +     * An interruption in one party causes others waiting in timed await to
130 +     * throw BrokenBarrierException
131 +     */
132 +    public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
133 +        final CyclicBarrier c = new CyclicBarrier(3);
134 +        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
135 +            public void realRun() throws Exception {
136 +                c.await(LONG_DELAY_MS, MILLISECONDS);
137 +            }};
138 +        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
139 +            public void realRun() throws Exception {
140 +                c.await(LONG_DELAY_MS, MILLISECONDS);
141 +            }};
142 +
143 +        t1.start();
144 +        t2.start();
145 +        delay(SHORT_DELAY_MS);
146 +        t1.interrupt();
147 +        t1.join();
148 +        t2.join();
149 +    }
150  
151      /**
152 <     *
152 >     * A timeout in timed await throws TimeoutException
153       */
154 <    public void testAwait1_Interrupted_BrokenBarrier() {
154 >    public void testAwait3_TimeoutException() throws InterruptedException {
155 >        final CyclicBarrier c = new CyclicBarrier(2);
156 >        Thread t = new ThreadShouldThrow(TimeoutException.class) {
157 >            public void realRun() throws Exception {
158 >                c.await(SHORT_DELAY_MS, MILLISECONDS);
159 >            }};
160 >
161 >        t.start();
162 >        t.join();
163 >    }
164 >
165 >    /**
166 >     * A timeout in one party causes others waiting in timed await to
167 >     * throw BrokenBarrierException
168 >     */
169 >    public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
170          final CyclicBarrier c = new CyclicBarrier(3);
171 <        Thread t1 = new Thread(new Runnable() {
172 <                public void run() {
173 <                    try {
174 <                        c.await();
175 <                        threadShouldThrow();
176 <                    } catch(InterruptedException success){}                
177 <                    catch(Exception b){
178 <                        threadFail("should throw IE");
179 <                    }
180 <                }
181 <            });
182 <        Thread t2 = new Thread(new Runnable() {
183 <                public void run() {
171 >        Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
172 >            public void realRun() throws Exception {
173 >                c.await(SHORT_DELAY_MS, MILLISECONDS);
174 >            }};
175 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
176 >            public void realRun() throws Exception {
177 >                c.await(MEDIUM_DELAY_MS, MILLISECONDS);
178 >            }};
179 >
180 >        t1.start();
181 >        t2.start();
182 >        t1.join();
183 >        t2.join();
184 >    }
185 >
186 >    /**
187 >     * A timeout in one party causes others waiting in await to
188 >     * throw BrokenBarrierException
189 >     */
190 >    public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
191 >        final CyclicBarrier c = new CyclicBarrier(3);
192 >        Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
193 >            public void realRun() throws Exception {
194 >                c.await(SHORT_DELAY_MS, MILLISECONDS);
195 >            }};
196 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
197 >            public void realRun() throws Exception {
198 >                c.await();
199 >            }};
200 >
201 >        t1.start();
202 >        t2.start();
203 >        t1.join();
204 >        t2.join();
205 >    }
206 >
207 >    /**
208 >     * A reset of an active barrier causes waiting threads to throw
209 >     * BrokenBarrierException
210 >     */
211 >    public void testReset_BrokenBarrier() throws InterruptedException {
212 >        final CyclicBarrier c = new CyclicBarrier(3);
213 >        Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
214 >            public void realRun() throws Exception {
215 >                c.await();
216 >            }};
217 >        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
218 >            public void realRun() throws Exception {
219 >                c.await();
220 >            }};
221 >
222 >        t1.start();
223 >        t2.start();
224 >        delay(SHORT_DELAY_MS);
225 >        c.reset();
226 >        t1.join();
227 >        t2.join();
228 >    }
229 >
230 >    /**
231 >     * A reset before threads enter barrier does not throw
232 >     * BrokenBarrierException
233 >     */
234 >    public void testReset_NoBrokenBarrier() throws Exception {
235 >        final CyclicBarrier c = new CyclicBarrier(3);
236 >        Thread t1 = new Thread(new CheckedRunnable() {
237 >            public void realRun() throws Exception {
238 >                c.await();
239 >            }});
240 >        Thread t2 = new Thread(new CheckedRunnable() {
241 >            public void realRun() throws Exception {
242 >                c.await();
243 >            }});
244 >
245 >        c.reset();
246 >        t1.start();
247 >        t2.start();
248 >        c.await();
249 >        t1.join();
250 >        t2.join();
251 >    }
252 >
253 >    /**
254 >     * All threads block while a barrier is broken.
255 >     */
256 >    public void testReset_Leakage() throws InterruptedException {
257 >        final CyclicBarrier c = new CyclicBarrier(2);
258 >        final AtomicBoolean done = new AtomicBoolean();
259 >        Thread t = new Thread(new CheckedRunnable() {
260 >            public void realRun() {
261 >                while (!done.get()) {
262                      try {
263 +                        while (c.isBroken())
264 +                            c.reset();
265 +
266                          c.await();
267 <                        threadShouldThrow();                        
141 <                    } catch(BrokenBarrierException success){
142 <                    } catch(Exception i){
143 <                        threadFail("should throw BBE");
267 >                        shouldThrow();
268                      }
269 <                }
270 <            });
271 <        try {
269 >                    catch (BrokenBarrierException ok) {}
270 >                    catch (InterruptedException ok) {}
271 >                }}});
272 >
273 >        t.start();
274 >        for (int i = 0; i < 4; i++) {
275 >            delay(timeoutMillis());
276 >            t.interrupt();
277 >        }
278 >        done.set(true);
279 >        t.interrupt();
280 >        awaitTermination(t);
281 >    }
282 >
283 >    /**
284 >     * Reset of a non-broken barrier does not break barrier
285 >     */
286 >    public void testResetWithoutBreakage() throws Exception {
287 >        final CyclicBarrier start = new CyclicBarrier(3);
288 >        final CyclicBarrier barrier = new CyclicBarrier(3);
289 >        for (int i = 0; i < 3; i++) {
290 >            Thread t1 = new Thread(new CheckedRunnable() {
291 >                public void realRun() throws Exception {
292 >                    start.await();
293 >                    barrier.await();
294 >                }});
295 >
296 >            Thread t2 = new Thread(new CheckedRunnable() {
297 >                public void realRun() throws Exception {
298 >                    start.await();
299 >                    barrier.await();
300 >                }});
301 >
302              t1.start();
303              t2.start();
304 <            Thread.sleep(SHORT_DELAY_MS);
305 <            t1.interrupt();
306 <            t1.join();
304 >            start.await();
305 >            barrier.await();
306 >            t1.join();
307              t2.join();
308 <        } catch(InterruptedException e){
309 <            unexpectedException();
308 >            assertFalse(barrier.isBroken());
309 >            assertEquals(0, barrier.getNumberWaiting());
310 >            if (i == 1) barrier.reset();
311 >            assertFalse(barrier.isBroken());
312 >            assertEquals(0, barrier.getNumberWaiting());
313          }
314      }
315  
316      /**
317 <     *
317 >     * Reset of a barrier after interruption reinitializes it.
318       */
319 <    public void testAwait2_Interrupted_BrokenBarrier() {
320 <      final CyclicBarrier c = new CyclicBarrier(3);
321 <        Thread t1 = new Thread(new Runnable() {
322 <                public void run() {
323 <                    try {
324 <                        c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
325 <                        threadShouldThrow();
326 <                    } catch(InterruptedException success){
327 <                    } catch(Exception b){
328 <                        threadFail("should throw IE");
329 <                    }
330 <                }
331 <            });
332 <        Thread t2 = new Thread(new Runnable() {
333 <                public void run() {
334 <                    try {
178 <                        c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
179 <                        threadShouldThrow();                        
180 <                    } catch(BrokenBarrierException success){
181 <                    } catch(Exception i){
182 <                        threadFail("should throw BBE");
183 <                    }
184 <                }
185 <            });
186 <        try {
319 >    public void testResetAfterInterrupt() throws Exception {
320 >        final CyclicBarrier start = new CyclicBarrier(3);
321 >        final CyclicBarrier barrier = new CyclicBarrier(3);
322 >        for (int i = 0; i < 2; i++) {
323 >            Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
324 >                public void realRun() throws Exception {
325 >                    start.await();
326 >                    barrier.await();
327 >                }};
328 >
329 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
330 >                public void realRun() throws Exception {
331 >                    start.await();
332 >                    barrier.await();
333 >                }};
334 >
335              t1.start();
336              t2.start();
337 <            Thread.sleep(SHORT_DELAY_MS);
337 >            start.await();
338              t1.interrupt();
339 <            t1.join();
339 >            t1.join();
340              t2.join();
341 <        } catch(InterruptedException e){
342 <            unexpectedException();
341 >            assertTrue(barrier.isBroken());
342 >            assertEquals(0, barrier.getNumberWaiting());
343 >            barrier.reset();
344 >            assertFalse(barrier.isBroken());
345 >            assertEquals(0, barrier.getNumberWaiting());
346          }
347      }
348 <    
348 >
349      /**
350 <     *
350 >     * Reset of a barrier after timeout reinitializes it.
351       */
352 <    public void testAwait3_TimeOutException() {
353 <        final CyclicBarrier c = new CyclicBarrier(2);
354 <        Thread t = new Thread(new Runnable() {
355 <                public void run() {
356 <                    try {
357 <                        c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
358 <                        threadShouldThrow();
359 <                    } catch(TimeoutException success){
360 <                    } catch(Exception b){
361 <                        threadFail("should throw TOE");
362 <                        
363 <                    }
364 <                }
365 <            });
366 <        try {
367 <            t.start();
368 <            t.join();
369 <        } catch(InterruptedException e){
370 <            unexpectedException();
352 >    public void testResetAfterTimeout() throws Exception {
353 >        final CyclicBarrier start = new CyclicBarrier(2);
354 >        final CyclicBarrier barrier = new CyclicBarrier(3);
355 >        for (int i = 0; i < 2; i++) {
356 >            Thread t1 = new ThreadShouldThrow(TimeoutException.class) {
357 >                    public void realRun() throws Exception {
358 >                        start.await();
359 >                        barrier.await(SHORT_DELAY_MS, MILLISECONDS);
360 >                    }};
361 >
362 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
363 >                public void realRun() throws Exception {
364 >                    start.await();
365 >                    barrier.await();
366 >                }};
367 >
368 >            t1.start();
369 >            t2.start();
370 >            t1.join();
371 >            t2.join();
372 >            assertTrue(barrier.isBroken());
373 >            assertEquals(0, barrier.getNumberWaiting());
374 >            barrier.reset();
375 >            assertFalse(barrier.isBroken());
376 >            assertEquals(0, barrier.getNumberWaiting());
377 >        }
378 >    }
379 >
380 >    /**
381 >     * Reset of a barrier after a failed command reinitializes it.
382 >     */
383 >    public void testResetAfterCommandException() throws Exception {
384 >        final CyclicBarrier start = new CyclicBarrier(3);
385 >        final CyclicBarrier barrier =
386 >            new CyclicBarrier(3, new Runnable() {
387 >                    public void run() {
388 >                        throw new NullPointerException(); }});
389 >        for (int i = 0; i < 2; i++) {
390 >            Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
391 >                public void realRun() throws Exception {
392 >                    start.await();
393 >                    barrier.await();
394 >                }};
395 >
396 >            Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
397 >                public void realRun() throws Exception {
398 >                    start.await();
399 >                    barrier.await();
400 >                }};
401 >
402 >            t1.start();
403 >            t2.start();
404 >            start.await();
405 >            while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
406 >            try {
407 >                barrier.await();
408 >                shouldThrow();
409 >            } catch (NullPointerException success) {}
410 >            t1.join();
411 >            t2.join();
412 >            assertTrue(barrier.isBroken());
413 >            assertEquals(0, barrier.getNumberWaiting());
414 >            barrier.reset();
415 >            assertFalse(barrier.isBroken());
416 >            assertEquals(0, barrier.getNumberWaiting());
417          }
418      }
222    
419   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines