ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.20
Committed: Fri May 27 20:07:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +14 -20 lines
Log Message:
performance and robustness improvements to queue tests

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.14 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 dl 1.1 }
104    
105 dl 1.3 /**
106 dl 1.4 * An interruption in one party causes others waiting in await to
107     * throw BrokenBarrierException
108 dl 1.3 */
109 jsr166 1.14 public void testAwait1_Interrupted_BrokenBarrier() throws Exception {
110 dl 1.1 final CyclicBarrier c = new CyclicBarrier(3);
111 jsr166 1.14 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 dl 1.18 delay(SHORT_DELAY_MS);
123 jsr166 1.14 t1.interrupt();
124     t1.join();
125     t2.join();
126 dl 1.1 }
127    
128 dl 1.3 /**
129 dl 1.4 * An interruption in one party causes others waiting in timed await to
130     * throw BrokenBarrierException
131 dl 1.3 */
132 jsr166 1.14 public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
133 dl 1.8 final CyclicBarrier c = new CyclicBarrier(3);
134 jsr166 1.14 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 dl 1.18 delay(SHORT_DELAY_MS);
146 jsr166 1.14 t1.interrupt();
147     t1.join();
148     t2.join();
149 dl 1.1 }
150 jsr166 1.9
151 dl 1.3 /**
152 dl 1.4 * A timeout in timed await throws TimeoutException
153 dl 1.3 */
154 jsr166 1.19 public void testAwait3_TimeoutException() throws InterruptedException {
155 dl 1.1 final CyclicBarrier c = new CyclicBarrier(2);
156 jsr166 1.14 Thread t = new ThreadShouldThrow(TimeoutException.class) {
157     public void realRun() throws Exception {
158     c.await(SHORT_DELAY_MS, MILLISECONDS);
159     }};
160 jsr166 1.9
161 jsr166 1.14 t.start();
162     t.join();
163 dl 1.5 }
164    
165     /**
166     * A timeout in one party causes others waiting in timed await to
167     * throw BrokenBarrierException
168     */
169 jsr166 1.14 public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
170 dl 1.8 final CyclicBarrier c = new CyclicBarrier(3);
171 jsr166 1.14 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 dl 1.5 }
185    
186     /**
187     * A timeout in one party causes others waiting in await to
188     * throw BrokenBarrierException
189     */
190 jsr166 1.14 public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
191 dl 1.8 final CyclicBarrier c = new CyclicBarrier(3);
192 jsr166 1.14 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 dl 1.1 }
206 jsr166 1.9
207 dl 1.4 /**
208     * A reset of an active barrier causes waiting threads to throw
209     * BrokenBarrierException
210     */
211 jsr166 1.14 public void testReset_BrokenBarrier() throws InterruptedException {
212 dl 1.4 final CyclicBarrier c = new CyclicBarrier(3);
213 jsr166 1.14 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 dl 1.18 delay(SHORT_DELAY_MS);
225 jsr166 1.14 c.reset();
226     t1.join();
227     t2.join();
228 dl 1.4 }
229    
230     /**
231     * A reset before threads enter barrier does not throw
232     * BrokenBarrierException
233     */
234 jsr166 1.14 public void testReset_NoBrokenBarrier() throws Exception {
235 dl 1.4 final CyclicBarrier c = new CyclicBarrier(3);
236 jsr166 1.14 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 dl 1.4 }
252    
253 dl 1.8 /**
254     * All threads block while a barrier is broken.
255     */
256 jsr166 1.14 public void testReset_Leakage() throws InterruptedException {
257     final CyclicBarrier c = new CyclicBarrier(2);
258     final AtomicBoolean done = new AtomicBoolean();
259 jsr166 1.20 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     shouldThrow();
268 dl 1.8 }
269 jsr166 1.20 catch (BrokenBarrierException ok) {}
270     catch (InterruptedException ok) {}
271     }}});
272 jsr166 1.9
273 jsr166 1.14 t.start();
274     for (int i = 0; i < 4; i++) {
275 jsr166 1.20 delay(timeoutMillis());
276 dl 1.8 t.interrupt();
277     }
278 jsr166 1.14 done.set(true);
279     t.interrupt();
280 jsr166 1.20 awaitTermination(t);
281 dl 1.8 }
282    
283     /**
284     * Reset of a non-broken barrier does not break barrier
285     */
286 jsr166 1.14 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     start.await();
305     barrier.await();
306     t1.join();
307     t2.join();
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 dl 1.8 }
314     }
315 jsr166 1.9
316 dl 1.8 /**
317     * Reset of a barrier after interruption reinitializes it.
318     */
319 jsr166 1.14 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     start.await();
338     t1.interrupt();
339     t1.join();
340     t2.join();
341     assertTrue(barrier.isBroken());
342     assertEquals(0, barrier.getNumberWaiting());
343     barrier.reset();
344     assertFalse(barrier.isBroken());
345     assertEquals(0, barrier.getNumberWaiting());
346 dl 1.8 }
347     }
348 jsr166 1.9
349 dl 1.8 /**
350     * Reset of a barrier after timeout reinitializes it.
351     */
352 jsr166 1.14 public void testResetAfterTimeout() throws Exception {
353 jsr166 1.16 final CyclicBarrier start = new CyclicBarrier(2);
354 jsr166 1.14 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 jsr166 1.16 barrier.await(SHORT_DELAY_MS, MILLISECONDS);
360 jsr166 1.14 }};
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 dl 1.8 }
378     }
379    
380     /**
381     * Reset of a barrier after a failed command reinitializes it.
382     */
383 jsr166 1.14 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 dl 1.8 }
418     }
419 dl 1.1 }