use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testConstructorPartiesExceedsLimit.
/**
* Constructing with a number of parties > 65535 throws
* IllegalArgumentException
*/
public void testConstructorPartiesExceedsLimit() {
new Phaser(maxParties);
try {
new Phaser(maxParties + 1);
shouldThrow();
} catch (IllegalArgumentException success) {
}
new Phaser(new Phaser(), maxParties);
try {
new Phaser(new Phaser(), maxParties + 1);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testRegisterEmptySubPhaser.
/**
* register on a subphaser that is currently empty succeeds, even
* in the presence of another non-empty subphaser
*/
public void testRegisterEmptySubPhaser() {
Phaser root = new Phaser();
Phaser child1 = new Phaser(root, 1);
Phaser child2 = new Phaser(root, 0);
assertEquals(0, child2.register());
assertState(root, 0, 2, 2);
assertState(child1, 0, 1, 1);
assertState(child2, 0, 1, 1);
assertEquals(0, child2.arriveAndDeregister());
assertState(root, 0, 1, 1);
assertState(child1, 0, 1, 1);
assertState(child2, 0, 0, 0);
assertEquals(0, child2.register());
assertEquals(0, child2.arriveAndDeregister());
assertState(root, 0, 1, 1);
assertState(child1, 0, 1, 1);
assertState(child2, 0, 0, 0);
assertEquals(0, child1.arriveAndDeregister());
assertTerminated(root, 1);
assertTerminated(child1, 1);
assertTerminated(child2, 1);
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testAwaitAdvanceTieredPhaser.
/**
* awaitAdvance returns the current phase in child phasers
*/
public void testAwaitAdvanceTieredPhaser() throws Exception {
final Phaser parent = new Phaser();
final List<Phaser> zeroPartyChildren = new ArrayList<>(3);
final List<Phaser> onePartyChildren = new ArrayList<>(3);
for (int i = 0; i < 3; i++) {
zeroPartyChildren.add(new Phaser(parent, 0));
onePartyChildren.add(new Phaser(parent, 1));
}
final List<Phaser> phasers = new ArrayList<>();
phasers.addAll(zeroPartyChildren);
phasers.addAll(onePartyChildren);
phasers.add(parent);
for (Phaser phaser : phasers) {
assertEquals(-42, phaser.awaitAdvance(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
}
for (Phaser child : onePartyChildren) assertEquals(0, child.arrive());
for (Phaser phaser : phasers) {
assertEquals(-42, phaser.awaitAdvance(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
assertEquals(1, phaser.awaitAdvance(0));
assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
}
for (Phaser child : onePartyChildren) assertEquals(1, child.arrive());
for (Phaser phaser : phasers) {
assertEquals(-42, phaser.awaitAdvance(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
assertEquals(2, phaser.awaitAdvance(0));
assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
assertEquals(2, phaser.awaitAdvance(1));
assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS));
}
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testConstructor3.
/**
* The parent provided to the constructor should be returned from
* a later call to getParent
*/
public void testConstructor3() {
Phaser parent = new Phaser();
assertSame(parent, new Phaser(parent).getParent());
assertNull(new Phaser(null).getParent());
}
use of java8.util.concurrent.Phaser in project streamsupport by stefan-zobel.
the class PhaserTest method testConstructorDefaultValues.
/**
* Empty constructor builds a new Phaser with no parent, no registered
* parties and initial phase number of 0
*/
public void testConstructorDefaultValues() {
Phaser phaser = new Phaser();
assertNull(phaser.getParent());
assertEquals(0, phaser.getRegisteredParties());
assertEquals(0, phaser.getArrivedParties());
assertEquals(0, phaser.getUnarrivedParties());
assertEquals(0, phaser.getPhase());
}
Aggregations