Java Code Examples for java.util.concurrent.Phaser#arrive()

The following examples show how to use java.util.concurrent.Phaser#arrive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhaseOverflow.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 2
Source File: PhaseOverflow.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 3
Source File: PhaseOverflow.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 4
Source File: PhaseOverflow.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 5
Source File: PhaseOverflow.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void testTiered() throws Throwable {
    Phaser root = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 6
Source File: PhaserTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * the phase number increments correctly when tripping the barrier
 */
public void testPhaseIncrement1() {
    for (int size = 1; size < nine; size++) {
        final Phaser phaser = new Phaser(size);
        for (int index = 0; index <= (1 << size); index++) {
            int phase = phaser.arrive();
            assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
        }
    }
}
 
Example 7
Source File: PhaseOverflow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 8
Source File: PhaseOverflow.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 9
Source File: PhaseOverflow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testTiered() throws Throwable {
    Phaser root = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 10
Source File: PhaseOverflow.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 11
Source File: PhaseOverflow.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 12
Source File: PhaseOverflow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 13
Source File: PhaserTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * the phase number increments correctly when tripping the barrier
 */
public void testPhaseIncrement1() {
    for (int size = 1; size < nine; size++) {
        final Phaser phaser = new Phaser(size);
        for (int index = 0; index <= (1 << size); index++) {
            int phase = phaser.arrive();
            assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
        }
    }
}
 
Example 14
Source File: PhaseOverflow.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testTiered() throws Throwable {
    Phaser root = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 15
Source File: PhaseOverflow.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void testLeaf() throws Throwable {
    Phaser phaser = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
    phaser.register();
    checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
    phaser.arrive();
    checkState(phaser, Integer.MAX_VALUE, 1, 1);
    phaser.arrive();
    checkState(phaser, 0, 1, 1);
    phaser.arrive();
    checkState(phaser, 1, 1, 1);
}
 
Example 16
Source File: PhaseOverflow.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 17
Source File: PhaseOverflow.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void xtestTiered() throws Throwable {
    Phaser root = new Phaser();
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 18
Source File: PhaseOverflow.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void testTiered() throws Throwable {
    Phaser root = new Phaser();
    // this is extremely dependent on internal representation
    stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
    checkState(root, Integer.MAX_VALUE - 1, 0, 0);
    Phaser p1 = new Phaser(root, 1);
    checkState(root, Integer.MAX_VALUE - 1, 1, 1);
    checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
    Phaser p2 = new Phaser(root, 2);
    checkState(root, Integer.MAX_VALUE - 1, 2, 2);
    checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
    int ph = Integer.MAX_VALUE - 1;
    for (int k = 0; k < 5; k++) {
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
        p1.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 2);
        p2.arrive();
        checkState(root, ph, 2, 1);
        checkState(p1, ph, 1, 0);
        checkState(p2, ph, 2, 1);
        p2.arrive();
        ph = phaseInc(ph);
        checkState(root, ph, 2, 2);
        checkState(p1, ph, 1, 1);
        checkState(p2, ph, 2, 2);
    }
    equal(3, ph);
}
 
Example 19
Source File: ManagedExecutorTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that the ManagedExecutor implementation starts 2 async tasks/actions, and no more,
 * when maxAsync is configured to 2.
 *
 * @throws ExecutionException indicates test failure
 * @throws InterruptedException indicates test failure
 * @throws TimeoutException indicates test failure
 */
@Test
public void maxAsync2() throws ExecutionException, InterruptedException, TimeoutException {
    ManagedExecutor executor = ManagedExecutor.builder()
            .maxAsync(2)
            .propagated()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();

    Phaser barrier = new Phaser(2);
    try {
        // Use up both maxAsync slots on blocking operations and wait for them to start
        Future<Integer> future1 = executor.submit(() -> barrier.awaitAdvance(barrier.arriveAndAwaitAdvance()));
        CompletableFuture<Integer> future2 = executor.supplyAsync(() -> barrier.awaitAdvance(barrier.arriveAndAwaitAdvance()));
        barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS);

        // This data structure holds the results of tasks which shouldn't be able to run yet
        LinkedBlockingQueue<String> results = new LinkedBlockingQueue<String>();

        // Submit additional tasks/actions for async execution.
        // These should queue, but otherwise be unable to start yet due to maxAsync=2.
        CompletableFuture<Void> future3 = executor.runAsync(() -> results.offer("Result3"));
        CompletableFuture<Boolean> future4 = executor.supplyAsync(() -> results.offer("Result4"));
        Future<Boolean> future5 = executor.submit(() -> results.offer("Result5"));
        CompletableFuture<Boolean> future6 = executor.completedFuture("6")
                .thenApplyAsync(s -> results.offer("Result" + s));

        // Detect whether any of the above tasks/actions run within the next 5 seconds
        Assert.assertNull(results.poll(5, TimeUnit.SECONDS),
                "Should not be able start more than 2 async tasks when maxAsync is 2.");

        // unblock and allow tasks to finish
        barrier.arrive();
        barrier.arrive(); // there are 2 parties in each phase

        Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "None of the queued tasks ran.");
        Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 1 of the queued tasks ran.");
        Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 2 of the queued tasks ran.");
        Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 3 of the queued tasks ran.");

        Assert.assertEquals(future1.get(), Integer.valueOf(2), "Unexpected result of first task.");
        Assert.assertEquals(future2.get(), Integer.valueOf(2), "Unexpected result of second task.");
        Assert.assertNull(future3.join(), "Unexpected result of third task.");
        Assert.assertEquals(future4.join(), Boolean.TRUE, "Unexpected result of fourth task.");
        Assert.assertEquals(future5.get(), Boolean.TRUE, "Unexpected result of fifth task.");
        Assert.assertEquals(future6.get(), Boolean.TRUE, "Unexpected result of sixth task.");
    }
    finally {
        barrier.forceTermination();
        executor.shutdownNow();
    }
}
 
Example 20
Source File: MPConfigTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that MicroProfile config defaults the maxAsync attribute and honors the explicitly specified
 * maxQueued attribute, when only the propagated and maxQueued attributes are specified by the application.
 *
 * @throws ExecutionException indicates test failure
 * @throws InterruptedException indicates test failure
 * @throws TimeoutException indicates test failure
 */
@Test(dependsOnMethods = "beanInjected")
public void explicitlySpecifyMaxQueued5()
        throws ExecutionException, InterruptedException, TimeoutException {

    // Expected config is maxAsync=1; maxQueued=5; propagated={}; cleared=Remaining
    Assert.assertNotNull(producedExecutor, "Injection failure. Cannot run test.");

    Phaser barrier = new Phaser(1);
    try {
        // First, use up the single maxAsync slot with a blocking task and wait for it to start
        producedExecutor.submit(() -> barrier.awaitAdvanceInterruptibly(barrier.arrive() + 1));
        barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS);

        // Use up queue positions
        CompletableFuture<String> future1 = producedExecutor.supplyAsync(() -> "Q_1");
        CompletableFuture<String> future2 = producedExecutor.supplyAsync(() -> "Q_2");
        CompletableFuture<String> future3 = producedExecutor.supplyAsync(() -> "Q_3");
        Future<String> future4 = producedExecutor.submit(() -> "Q_4");
        Future<String> future5 = producedExecutor.submit(() -> "Q_5");

        // Sixth attempt to queue a task must be rejected
        try {
            Future<String> future6 = producedExecutor.submit(() -> "Q_6");
            Assert.fail("Exceeded maxQueued of 5. Future for 6th queued task/action is " + future6);
        }
        catch (RejectedExecutionException x) {
            // test passes
        }

        // unblock and allow tasks to finish
        barrier.arrive();

        Assert.assertEquals(future1.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_1",
                "Unexpected result of first task.");

        Assert.assertEquals(future2.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_2",
                "Unexpected result of second task.");

        Assert.assertEquals(future3.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_3",
                "Unexpected result of third task.");

        Assert.assertEquals(future4.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_4",
                "Unexpected result of fourth task.");

        Assert.assertEquals(future5.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Q_5",
                "Unexpected result of fifth task.");
    }
    finally {
        barrier.forceTermination();
    } 
}