ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.26
Committed: Wed Dec 31 19:05:42 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +5 -3 lines
Log Message:
no wildcard imports

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