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.TimeoutException; |
15 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
17 |
+ |
|
18 |
+ |
import junit.framework.Test; |
19 |
+ |
import junit.framework.TestSuite; |
20 |
+ |
|
21 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(CyclicBarrierTest.class); |
27 |
|
} |
28 |
|
|
29 |
< |
private volatile int countAction; |
30 |
< |
private class MyAction implements Runnable { |
31 |
< |
public void run() { ++countAction; } |
29 |
> |
/** |
30 |
> |
* Spin-waits till the number of waiters == numberOfWaiters. |
31 |
> |
*/ |
32 |
> |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
33 |
> |
long startTime = System.nanoTime(); |
34 |
> |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
35 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
36 |
> |
fail("timed out"); |
37 |
> |
Thread.yield(); |
38 |
> |
} |
39 |
|
} |
40 |
|
|
41 |
|
/** |
42 |
< |
* Creating with negative parties throws IAE |
42 |
> |
* Creating with negative parties throws IllegalArgumentException |
43 |
|
*/ |
44 |
|
public void testConstructor1() { |
45 |
|
try { |
49 |
|
} |
50 |
|
|
51 |
|
/** |
52 |
< |
* Creating with negative parties and no action throws IAE |
52 |
> |
* Creating with negative parties and no action throws |
53 |
> |
* IllegalArgumentException |
54 |
|
*/ |
55 |
|
public void testConstructor2() { |
56 |
|
try { |
84 |
|
* The supplied barrier action is run at barrier |
85 |
|
*/ |
86 |
|
public void testBarrierAction() throws Exception { |
87 |
< |
countAction = 0; |
88 |
< |
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
87 |
> |
final AtomicInteger count = new AtomicInteger(0); |
88 |
> |
final Runnable incCount = new Runnable() { public void run() { |
89 |
> |
count.getAndIncrement(); }}; |
90 |
> |
CyclicBarrier b = new CyclicBarrier(1, incCount); |
91 |
|
assertEquals(1, b.getParties()); |
92 |
|
assertEquals(0, b.getNumberWaiting()); |
93 |
|
b.await(); |
94 |
|
b.await(); |
95 |
|
assertEquals(0, b.getNumberWaiting()); |
96 |
< |
assertEquals(countAction, 2); |
96 |
> |
assertEquals(2, count.get()); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
201 |
|
}}); |
202 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
203 |
|
public void realRun() throws Exception { |
204 |
< |
while (c.getNumberWaiting() == 0) |
190 |
< |
Thread.yield(); |
204 |
> |
awaitNumberWaiting(c, 1); |
205 |
|
long startTime = System.nanoTime(); |
206 |
|
try { |
207 |
|
c.await(timeoutMillis(), MILLISECONDS); |
229 |
|
}}); |
230 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
231 |
|
public void realRun() throws Exception { |
232 |
< |
while (c.getNumberWaiting() == 0) |
219 |
< |
Thread.yield(); |
232 |
> |
awaitNumberWaiting(c, 1); |
233 |
|
long startTime = System.nanoTime(); |
234 |
|
try { |
235 |
|
c.await(timeoutMillis(), MILLISECONDS); |
263 |
|
t1.start(); |
264 |
|
t2.start(); |
265 |
|
await(pleaseReset); |
266 |
+ |
|
267 |
+ |
awaitNumberWaiting(c, 2); |
268 |
|
c.reset(); |
269 |
|
awaitTermination(t1); |
270 |
|
awaitTermination(t2); |
401 |
|
}}); |
402 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
403 |
|
public void realRun() throws Exception { |
404 |
< |
while (barrier.getNumberWaiting() == 0) |
390 |
< |
Thread.yield(); |
404 |
> |
awaitNumberWaiting(barrier, 1); |
405 |
|
long startTime = System.nanoTime(); |
406 |
|
try { |
407 |
|
barrier.await(timeoutMillis(), MILLISECONDS); |
446 |
|
t1.start(); |
447 |
|
t2.start(); |
448 |
|
start.await(); |
449 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
449 |
> |
awaitNumberWaiting(barrier, 2); |
450 |
|
try { |
451 |
|
barrier.await(); |
452 |
|
shouldThrow(); |