1 |
|
/* |
2 |
|
* 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 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import junit.framework.*; |
9 |
|
import java.util.ArrayList; |
10 |
|
import java.util.List; |
11 |
< |
import java.util.concurrent.atomic.AtomicInteger; |
12 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
12 |
< |
import java.util.concurrent.*; |
11 |
> |
import java.util.concurrent.Phaser; |
12 |
> |
import java.util.concurrent.CountDownLatch; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
< |
import junit.framework.Test; |
16 |
< |
import junit.framework.TestSuite; |
15 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
17 |
|
|
18 |
|
public class PhaserTest extends JSR166TestCase { |
19 |
|
|
27 |
|
|
28 |
|
private static final int maxParties = 65535; |
29 |
|
|
30 |
< |
/** Checks state of phaser. */ |
30 |
> |
/** Checks state of unterminated phaser. */ |
31 |
|
protected void assertState(Phaser phaser, |
32 |
|
int phase, int parties, int unarrived) { |
33 |
|
assertEquals(phase, phaser.getPhase()); |
34 |
|
assertEquals(parties, phaser.getRegisteredParties()); |
35 |
|
assertEquals(unarrived, phaser.getUnarrivedParties()); |
36 |
|
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
37 |
< |
assertTrue((phaser.getPhase() >= 0) ^ phaser.isTerminated()); |
37 |
> |
assertFalse(phaser.isTerminated()); |
38 |
|
} |
39 |
|
|
40 |
|
/** Checks state of terminated phaser. */ |
41 |
< |
protected void assertTerminated(Phaser phaser, int parties, int unarrived) { |
41 |
> |
protected void assertTerminated(Phaser phaser, int maxPhase, int parties) { |
42 |
|
assertTrue(phaser.isTerminated()); |
43 |
< |
assertTrue(phaser.getPhase() < 0); |
43 |
> |
int expectedPhase = maxPhase + Integer.MIN_VALUE; |
44 |
> |
assertEquals(expectedPhase, phaser.getPhase()); |
45 |
|
assertEquals(parties, phaser.getRegisteredParties()); |
46 |
< |
assertEquals(unarrived, phaser.getUnarrivedParties()); |
47 |
< |
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
46 |
> |
assertEquals(expectedPhase, phaser.register()); |
47 |
> |
assertEquals(expectedPhase, phaser.arrive()); |
48 |
> |
assertEquals(expectedPhase, phaser.arriveAndDeregister()); |
49 |
|
} |
50 |
|
|
51 |
< |
protected void assertTerminated(Phaser phaser) { |
52 |
< |
assertTerminated(phaser, 0, 0); |
51 |
> |
protected void assertTerminated(Phaser phaser, int maxPhase) { |
52 |
> |
assertTerminated(phaser, maxPhase, 0); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
158 |
|
phaser.bulkRegister(Integer.MAX_VALUE); |
159 |
|
shouldThrow(); |
160 |
|
} catch (IllegalStateException success) {} |
161 |
+ |
|
162 |
+ |
assertEquals(0, phaser.bulkRegister(0)); |
163 |
+ |
assertState(phaser, 0, maxParties, maxParties); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
204 |
|
*/ |
205 |
|
public void testBulkRegister2() { |
206 |
|
Phaser phaser = new Phaser(); |
207 |
+ |
assertEquals(0, phaser.bulkRegister(0)); |
208 |
+ |
assertState(phaser, 0, 0, 0); |
209 |
|
assertEquals(0, phaser.bulkRegister(20)); |
210 |
|
assertState(phaser, 0, 20, 20); |
211 |
|
} |
264 |
|
assertState(phaser, 0, 1, 1); |
265 |
|
} |
266 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
267 |
< |
assertTerminated(phaser); |
267 |
> |
assertTerminated(phaser, 1); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
295 |
|
public void testArrive3() { |
296 |
|
Phaser phaser = new Phaser(1); |
297 |
|
phaser.forceTermination(); |
298 |
< |
assertTerminated(phaser, 1, 1); |
298 |
> |
assertTerminated(phaser, 0, 1); |
299 |
> |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
300 |
|
assertTrue(phaser.arrive() < 0); |
301 |
|
assertTrue(phaser.register() < 0); |
302 |
|
assertTrue(phaser.arriveAndDeregister() < 0); |
337 |
|
Phaser parent = new Phaser(); |
338 |
|
Phaser child = new Phaser(parent); |
339 |
|
assertState(child, 0, 0, 0); |
340 |
< |
assertState(parent, 0, 1, 1); |
340 |
> |
assertState(parent, 0, 0, 0); |
341 |
|
assertEquals(0, child.register()); |
342 |
|
assertState(child, 0, 1, 1); |
343 |
|
assertState(parent, 0, 1, 1); |
344 |
|
assertEquals(0, child.arriveAndDeregister()); |
345 |
< |
assertTerminated(child); |
346 |
< |
assertTerminated(parent); |
345 |
> |
assertTerminated(child, 1); |
346 |
> |
assertTerminated(parent, 1); |
347 |
|
} |
348 |
|
|
349 |
|
/** |
370 |
|
Phaser root = new Phaser(); |
371 |
|
Phaser parent = new Phaser(root); |
372 |
|
Phaser child = new Phaser(parent); |
373 |
< |
assertState(root, 0, 1, 1); |
374 |
< |
assertState(parent, 0, 1, 1); |
373 |
> |
assertState(root, 0, 0, 0); |
374 |
> |
assertState(parent, 0, 0, 0); |
375 |
|
assertState(child, 0, 0, 0); |
376 |
|
assertEquals(0, child.register()); |
377 |
|
assertState(root, 0, 1, 1); |
378 |
|
assertState(parent, 0, 1, 1); |
379 |
|
assertState(child, 0, 1, 1); |
380 |
|
assertEquals(0, child.arriveAndDeregister()); |
381 |
< |
assertTerminated(child); |
382 |
< |
assertTerminated(parent); |
383 |
< |
assertTerminated(root); |
381 |
> |
assertTerminated(child, 1); |
382 |
> |
assertTerminated(parent, 1); |
383 |
> |
assertTerminated(root, 1); |
384 |
|
} |
385 |
|
|
386 |
|
/** |
398 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
399 |
|
assertState(phaser, 1, 1, 1); |
400 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
401 |
< |
assertTerminated(phaser); |
401 |
> |
assertTerminated(phaser, 2); |
402 |
|
awaitTermination(t, SHORT_DELAY_MS); |
403 |
|
} |
404 |
|
|
588 |
|
} |
589 |
|
|
590 |
|
/** |
591 |
+ |
* awaitAdvance returns the current phase in child phasers |
592 |
+ |
*/ |
593 |
+ |
public void testAwaitAdvanceTieredPhaser() throws Exception { |
594 |
+ |
final Phaser parent = new Phaser(); |
595 |
+ |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
596 |
+ |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
597 |
+ |
for (int i = 0; i < 3; i++) { |
598 |
+ |
zeroPartyChildren.add(new Phaser(parent, 0)); |
599 |
+ |
onePartyChildren.add(new Phaser(parent, 1)); |
600 |
+ |
} |
601 |
+ |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
602 |
+ |
phasers.addAll(zeroPartyChildren); |
603 |
+ |
phasers.addAll(onePartyChildren); |
604 |
+ |
phasers.add(parent); |
605 |
+ |
for (Phaser phaser : phasers) { |
606 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
607 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
608 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
609 |
+ |
} |
610 |
+ |
|
611 |
+ |
for (Phaser child : onePartyChildren) |
612 |
+ |
assertEquals(0, child.arrive()); |
613 |
+ |
for (Phaser phaser : phasers) { |
614 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
615 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
616 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
617 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
618 |
+ |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
619 |
+ |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
620 |
+ |
} |
621 |
+ |
|
622 |
+ |
for (Phaser child : onePartyChildren) |
623 |
+ |
assertEquals(1, child.arrive()); |
624 |
+ |
for (Phaser phaser : phasers) { |
625 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
626 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
627 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
628 |
+ |
assertEquals(2, phaser.awaitAdvance(0)); |
629 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
630 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
631 |
+ |
assertEquals(2, phaser.awaitAdvance(1)); |
632 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
633 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
634 |
+ |
} |
635 |
+ |
} |
636 |
+ |
|
637 |
+ |
/** |
638 |
|
* awaitAdvance returns when the phaser is externally terminated |
639 |
|
*/ |
640 |
|
public void testAwaitAdvance6() throws InterruptedException { |
649 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
650 |
|
assertTrue(phaser.isTerminated()); |
651 |
|
assertTrue(phaser.getPhase() < 0); |
652 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
653 |
|
assertEquals(3, phaser.getRegisteredParties()); |
654 |
|
}}; |
655 |
|
threads.add(newStartedThread(r)); |
656 |
|
} |
657 |
|
threadsStarted.await(); |
658 |
|
phaser.forceTermination(); |
659 |
+ |
assertTrue(phaser.isTerminated()); |
660 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
661 |
|
for (Thread thread : threads) |
662 |
|
awaitTermination(thread, SMALL_DELAY_MS); |
605 |
– |
assertTrue(phaser.isTerminated()); |
606 |
– |
assertTrue(phaser.getPhase() < 0); |
663 |
|
assertEquals(3, phaser.getRegisteredParties()); |
664 |
|
} |
665 |
|
|