5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.List; |
11 |
– |
import java.util.concurrent.Phaser; |
12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.Phaser; |
14 |
|
import java.util.concurrent.TimeoutException; |
14 |
– |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
|
import java.util.concurrent.atomic.AtomicInteger; |
16 |
|
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
import junit.framework.TestSuite; |
19 |
+ |
|
20 |
|
public class PhaserTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
|
26 |
|
public static Test suite() { |
302 |
|
public void testArrive2() { |
303 |
|
final Phaser phaser = new Phaser(); |
304 |
|
assertEquals(0, phaser.register()); |
305 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
305 |
> |
List<Thread> threads = new ArrayList<>(); |
306 |
|
for (int i = 0; i < 10; i++) { |
307 |
|
assertEquals(0, phaser.register()); |
308 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
338 |
|
* registered or unarrived parties would become negative |
339 |
|
*/ |
340 |
|
public void testArriveAndDeregister1() { |
341 |
+ |
Phaser phaser = new Phaser(); |
342 |
|
try { |
339 |
– |
Phaser phaser = new Phaser(); |
343 |
|
phaser.arriveAndDeregister(); |
344 |
|
shouldThrow(); |
345 |
|
} catch (IllegalStateException success) {} |
476 |
|
|
477 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
478 |
|
public void realRun() throws TimeoutException { |
479 |
+ |
long startTime = System.nanoTime(); |
480 |
+ |
|
481 |
|
Thread.currentThread().interrupt(); |
482 |
|
try { |
483 |
< |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
483 |
> |
phaser.awaitAdvanceInterruptibly(0, randomTimeout(), randomTimeUnit()); |
484 |
|
shouldThrow(); |
485 |
|
} catch (InterruptedException success) {} |
486 |
|
assertFalse(Thread.interrupted()); |
487 |
|
|
488 |
|
pleaseInterrupt.countDown(); |
489 |
|
try { |
490 |
< |
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); |
490 |
> |
phaser.awaitAdvanceInterruptibly(0, LONG_DELAY_MS, MILLISECONDS); |
491 |
|
shouldThrow(); |
492 |
|
} catch (InterruptedException success) {} |
493 |
|
assertFalse(Thread.interrupted()); |
494 |
+ |
|
495 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
496 |
|
}}); |
497 |
|
|
498 |
|
await(pleaseInterrupt); |
499 |
|
assertState(phaser, 0, 1, 1); |
500 |
< |
assertThreadsStayAlive(t1, t2); |
500 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
501 |
> |
assertThreadBlocks(t2, Thread.State.TIMED_WAITING); |
502 |
|
t1.interrupt(); |
503 |
|
t2.interrupt(); |
504 |
|
awaitTermination(t1); |
528 |
|
}}); |
529 |
|
|
530 |
|
await(pleaseArrive); |
531 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
531 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
532 |
|
assertEquals(0, phaser.arrive()); |
533 |
|
awaitTermination(t); |
534 |
|
|
556 |
|
}}); |
557 |
|
|
558 |
|
await(pleaseArrive); |
559 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
559 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
560 |
|
t.interrupt(); |
561 |
|
assertEquals(0, phaser.arrive()); |
562 |
|
awaitTermination(t); |
572 |
|
public void testArriveAndAwaitAdvanceAfterInterrupt() { |
573 |
|
final Phaser phaser = new Phaser(); |
574 |
|
assertEquals(0, phaser.register()); |
575 |
< |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
575 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
576 |
|
|
577 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
578 |
|
public void realRun() { |
579 |
|
Thread.currentThread().interrupt(); |
580 |
|
assertEquals(0, phaser.register()); |
581 |
< |
pleaseInterrupt.countDown(); |
581 |
> |
pleaseArrive.countDown(); |
582 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
583 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
584 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
584 |
> |
assertTrue(Thread.interrupted()); |
585 |
|
}}); |
586 |
|
|
587 |
< |
await(pleaseInterrupt); |
588 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
587 |
> |
await(pleaseArrive); |
588 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
589 |
|
Thread.currentThread().interrupt(); |
590 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
591 |
|
assertTrue(Thread.interrupted()); |
606 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
607 |
|
pleaseInterrupt.countDown(); |
608 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
609 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
609 |
> |
assertTrue(Thread.interrupted()); |
610 |
|
}}); |
611 |
|
|
612 |
|
await(pleaseInterrupt); |
613 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
613 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
614 |
|
t.interrupt(); |
615 |
|
Thread.currentThread().interrupt(); |
616 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
625 |
|
public void testAwaitAdvance4() { |
626 |
|
final Phaser phaser = new Phaser(4); |
627 |
|
final AtomicInteger count = new AtomicInteger(0); |
628 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
628 |
> |
List<Thread> threads = new ArrayList<>(); |
629 |
|
for (int i = 0; i < 4; i++) |
630 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
631 |
|
public void realRun() { |
632 |
|
for (int k = 0; k < 3; k++) { |
633 |
< |
assertEquals(2*k+1, phaser.arriveAndAwaitAdvance()); |
633 |
> |
assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance()); |
634 |
|
count.incrementAndGet(); |
635 |
< |
assertEquals(2*k+1, phaser.arrive()); |
636 |
< |
assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); |
637 |
< |
assertEquals(4*(k+1), count.get()); |
635 |
> |
assertEquals(2 * k + 1, phaser.arrive()); |
636 |
> |
assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1)); |
637 |
> |
assertEquals(4 * (k + 1), count.get()); |
638 |
|
}}})); |
639 |
|
|
640 |
|
for (Thread thread : threads) |
649 |
|
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
650 |
|
assertEquals(1, phaser.getPhase()); |
651 |
|
assertEquals(1, phaser.register()); |
652 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
652 |
> |
List<Thread> threads = new ArrayList<>(); |
653 |
|
for (int i = 0; i < 8; i++) { |
654 |
|
final CountDownLatch latch = new CountDownLatch(1); |
655 |
|
final boolean goesFirst = ((i & 1) == 0); |
677 |
|
*/ |
678 |
|
public void testAwaitAdvanceTieredPhaser() throws Exception { |
679 |
|
final Phaser parent = new Phaser(); |
680 |
< |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
681 |
< |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
680 |
> |
final List<Phaser> zeroPartyChildren = new ArrayList<>(3); |
681 |
> |
final List<Phaser> onePartyChildren = new ArrayList<>(3); |
682 |
|
for (int i = 0; i < 3; i++) { |
683 |
|
zeroPartyChildren.add(new Phaser(parent, 0)); |
684 |
|
onePartyChildren.add(new Phaser(parent, 1)); |
685 |
|
} |
686 |
< |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
686 |
> |
final List<Phaser> phasers = new ArrayList<>(); |
687 |
|
phasers.addAll(zeroPartyChildren); |
688 |
|
phasers.addAll(onePartyChildren); |
689 |
|
phasers.add(parent); |
690 |
|
for (Phaser phaser : phasers) { |
691 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
692 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
693 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
693 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
694 |
|
} |
695 |
|
|
696 |
|
for (Phaser child : onePartyChildren) |
698 |
|
for (Phaser phaser : phasers) { |
699 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
700 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
701 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
701 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
702 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
703 |
|
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
704 |
< |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
704 |
> |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
|
} |
706 |
|
|
707 |
|
for (Phaser child : onePartyChildren) |
709 |
|
for (Phaser phaser : phasers) { |
710 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
711 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
712 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
712 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
713 |
|
assertEquals(2, phaser.awaitAdvance(0)); |
714 |
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
715 |
< |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
715 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); |
716 |
|
assertEquals(2, phaser.awaitAdvance(1)); |
717 |
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
718 |
< |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
718 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS)); |
719 |
|
} |
720 |
|
} |
721 |
|
|
725 |
|
public void testAwaitAdvance6() { |
726 |
|
final Phaser phaser = new Phaser(3); |
727 |
|
final CountDownLatch pleaseForceTermination = new CountDownLatch(2); |
728 |
< |
final List<Thread> threads = new ArrayList<Thread>(); |
728 |
> |
final List<Thread> threads = new ArrayList<>(); |
729 |
|
for (int i = 0; i < 2; i++) { |
730 |
|
Runnable r = new CheckedRunnable() { |
731 |
|
public void realRun() { |
753 |
|
* unarrived parties |
754 |
|
*/ |
755 |
|
public void testArriveAndAwaitAdvance1() { |
756 |
+ |
Phaser phaser = new Phaser(); |
757 |
|
try { |
749 |
– |
Phaser phaser = new Phaser(); |
758 |
|
phaser.arriveAndAwaitAdvance(); |
759 |
|
shouldThrow(); |
760 |
|
} catch (IllegalStateException success) {} |
769 |
|
final Phaser phaser = new Phaser(1); |
770 |
|
final int THREADS = 3; |
771 |
|
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); |
772 |
< |
final List<Thread> threads = new ArrayList<Thread>(); |
772 |
> |
final List<Thread> threads = new ArrayList<>(); |
773 |
|
for (int i = 0; i < THREADS; i++) |
774 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
775 |
|
public void realRun() { |
785 |
|
assertEquals(THREADS, phaser.getArrivedParties()); |
786 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
787 |
|
for (Thread thread : threads) |
788 |
< |
waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS); |
788 |
> |
assertThreadBlocks(thread, Thread.State.WAITING); |
789 |
|
for (Thread thread : threads) |
790 |
|
assertTrue(thread.isAlive()); |
791 |
|
assertState(phaser, 0, THREADS + 1, 1); |