ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.31
Committed: Fri Feb 1 19:14:23 2019 UTC (5 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +33 -0 lines
Log Message:
add testMoreTasksThanParties (inspired by JDK-8218157)

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