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 |
|
|
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 |
|
} |
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 { |