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.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.concurrent.BrokenBarrierException; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.CyclicBarrier; |
14 |
+ |
import java.util.concurrent.ExecutorService; |
15 |
+ |
import java.util.concurrent.Executors; |
16 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
17 |
+ |
import java.util.concurrent.TimeoutException; |
18 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
19 |
+ |
|
20 |
+ |
import junit.framework.Test; |
21 |
+ |
import junit.framework.TestSuite; |
22 |
+ |
|
23 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
|
return new TestSuite(CyclicBarrierTest.class); |
29 |
|
} |
30 |
|
|
31 |
< |
private volatile int countAction; |
32 |
< |
private class MyAction implements Runnable { |
33 |
< |
public void run() { ++countAction; } |
31 |
> |
/** |
32 |
> |
* Spin-waits till the number of waiters == numberOfWaiters. |
33 |
> |
*/ |
34 |
> |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
35 |
> |
long startTime = System.nanoTime(); |
36 |
> |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
37 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
38 |
> |
fail("timed out"); |
39 |
> |
Thread.yield(); |
40 |
> |
} |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* Creating with negative parties throws IAE |
44 |
> |
* Creating with negative parties throws IllegalArgumentException |
45 |
|
*/ |
46 |
|
public void testConstructor1() { |
47 |
|
try { |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* Creating with negative parties and no action throws IAE |
54 |
> |
* Creating with negative parties and no action throws |
55 |
> |
* IllegalArgumentException |
56 |
|
*/ |
57 |
|
public void testConstructor2() { |
58 |
|
try { |
86 |
|
* The supplied barrier action is run at barrier |
87 |
|
*/ |
88 |
|
public void testBarrierAction() throws Exception { |
89 |
< |
countAction = 0; |
90 |
< |
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
89 |
> |
final AtomicInteger count = new AtomicInteger(0); |
90 |
> |
final Runnable incCount = new Runnable() { public void run() { |
91 |
> |
count.getAndIncrement(); }}; |
92 |
> |
CyclicBarrier b = new CyclicBarrier(1, incCount); |
93 |
|
assertEquals(1, b.getParties()); |
94 |
|
assertEquals(0, b.getNumberWaiting()); |
95 |
|
b.await(); |
96 |
|
b.await(); |
97 |
|
assertEquals(0, b.getNumberWaiting()); |
98 |
< |
assertEquals(countAction, 2); |
98 |
> |
assertEquals(2, count.get()); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
203 |
|
}}); |
204 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
205 |
|
public void realRun() throws Exception { |
206 |
< |
while (c.getNumberWaiting() == 0) |
190 |
< |
Thread.yield(); |
206 |
> |
awaitNumberWaiting(c, 1); |
207 |
|
long startTime = System.nanoTime(); |
208 |
|
try { |
209 |
|
c.await(timeoutMillis(), MILLISECONDS); |
231 |
|
}}); |
232 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
233 |
|
public void realRun() throws Exception { |
234 |
< |
while (c.getNumberWaiting() == 0) |
219 |
< |
Thread.yield(); |
234 |
> |
awaitNumberWaiting(c, 1); |
235 |
|
long startTime = System.nanoTime(); |
236 |
|
try { |
237 |
|
c.await(timeoutMillis(), MILLISECONDS); |
265 |
|
t1.start(); |
266 |
|
t2.start(); |
267 |
|
await(pleaseReset); |
268 |
+ |
|
269 |
+ |
awaitNumberWaiting(c, 2); |
270 |
|
c.reset(); |
271 |
|
awaitTermination(t1); |
272 |
|
awaitTermination(t2); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
281 |
– |
* All threads block while a barrier is broken. |
282 |
– |
*/ |
283 |
– |
public void testReset_Leakage() throws InterruptedException { |
284 |
– |
final CyclicBarrier c = new CyclicBarrier(2); |
285 |
– |
final AtomicBoolean done = new AtomicBoolean(); |
286 |
– |
Thread t = newStartedThread(new CheckedRunnable() { |
287 |
– |
public void realRun() { |
288 |
– |
while (!done.get()) { |
289 |
– |
try { |
290 |
– |
while (c.isBroken()) |
291 |
– |
c.reset(); |
292 |
– |
|
293 |
– |
c.await(); |
294 |
– |
shouldThrow(); |
295 |
– |
} |
296 |
– |
catch (BrokenBarrierException ok) {} |
297 |
– |
catch (InterruptedException ok) {} |
298 |
– |
}}}); |
299 |
– |
|
300 |
– |
for (int i = 0; i < 4; i++) { |
301 |
– |
delay(timeoutMillis()); |
302 |
– |
t.interrupt(); |
303 |
– |
} |
304 |
– |
done.set(true); |
305 |
– |
t.interrupt(); |
306 |
– |
awaitTermination(t); |
307 |
– |
} |
308 |
– |
|
309 |
– |
/** |
298 |
|
* Reset of a non-broken barrier does not break barrier |
299 |
|
*/ |
300 |
|
public void testResetWithoutBreakage() throws Exception { |
374 |
|
}}); |
375 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
376 |
|
public void realRun() throws Exception { |
377 |
< |
while (barrier.getNumberWaiting() == 0) |
390 |
< |
Thread.yield(); |
377 |
> |
awaitNumberWaiting(barrier, 1); |
378 |
|
long startTime = System.nanoTime(); |
379 |
|
try { |
380 |
|
barrier.await(timeoutMillis(), MILLISECONDS); |
419 |
|
t1.start(); |
420 |
|
t2.start(); |
421 |
|
start.await(); |
422 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
422 |
> |
awaitNumberWaiting(barrier, 2); |
423 |
|
try { |
424 |
|
barrier.await(); |
425 |
|
shouldThrow(); |
433 |
|
assertEquals(0, barrier.getNumberWaiting()); |
434 |
|
} |
435 |
|
} |
436 |
+ |
|
437 |
+ |
/** |
438 |
+ |
* There can be more threads calling await() than parties, as long as each |
439 |
+ |
* task only calls await once and the task count is a multiple of parties. |
440 |
+ |
*/ |
441 |
+ |
public void testMoreTasksThanParties() throws Exception { |
442 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
443 |
+ |
final int parties = rnd.nextInt(1, 5); |
444 |
+ |
final int nTasks = rnd.nextInt(1, 5) * parties; |
445 |
+ |
final AtomicInteger tripCount = new AtomicInteger(0); |
446 |
+ |
final AtomicInteger awaitCount = new AtomicInteger(0); |
447 |
+ |
final CyclicBarrier barrier = |
448 |
+ |
new CyclicBarrier(parties, () -> tripCount.getAndIncrement()); |
449 |
+ |
final ExecutorService e = Executors.newFixedThreadPool(nTasks); |
450 |
+ |
final Runnable awaiter = () -> { |
451 |
+ |
try { |
452 |
+ |
if (randomBoolean()) |
453 |
+ |
barrier.await(); |
454 |
+ |
else |
455 |
+ |
barrier.await(LONG_DELAY_MS, MILLISECONDS); |
456 |
+ |
awaitCount.getAndIncrement(); |
457 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); }}; |
458 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
459 |
+ |
for (int i = nTasks; i--> 0; ) |
460 |
+ |
e.execute(awaiter); |
461 |
+ |
} |
462 |
+ |
assertEquals(nTasks / parties, tripCount.get()); |
463 |
+ |
assertEquals(nTasks, awaitCount.get()); |
464 |
+ |
assertEquals(0, barrier.getNumberWaiting()); |
465 |
+ |
} |
466 |
|
} |