ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.22
Committed: Sun May 29 13:16:40 2011 UTC (12 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.21: +1 -0 lines
Log Message:
Ensure thread not interrupted before start of testReset_BrokenBarrier

File Contents

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