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 { |
342 |
– |
Phaser phaser = new Phaser(); |
343 |
|
phaser.arriveAndDeregister(); |
344 |
|
shouldThrow(); |
345 |
|
} catch (IllegalStateException success) {} |
523 |
|
}}); |
524 |
|
|
525 |
|
await(pleaseArrive); |
526 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
526 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
527 |
|
assertEquals(0, phaser.arrive()); |
528 |
|
awaitTermination(t); |
529 |
|
|
551 |
|
}}); |
552 |
|
|
553 |
|
await(pleaseArrive); |
554 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
554 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
555 |
|
t.interrupt(); |
556 |
|
assertEquals(0, phaser.arrive()); |
557 |
|
awaitTermination(t); |
567 |
|
public void testArriveAndAwaitAdvanceAfterInterrupt() { |
568 |
|
final Phaser phaser = new Phaser(); |
569 |
|
assertEquals(0, phaser.register()); |
570 |
< |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
570 |
> |
final CountDownLatch pleaseArrive = new CountDownLatch(1); |
571 |
|
|
572 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
573 |
|
public void realRun() { |
574 |
|
Thread.currentThread().interrupt(); |
575 |
|
assertEquals(0, phaser.register()); |
576 |
< |
pleaseInterrupt.countDown(); |
576 |
> |
pleaseArrive.countDown(); |
577 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
578 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
579 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
579 |
> |
assertTrue(Thread.interrupted()); |
580 |
|
}}); |
581 |
|
|
582 |
< |
await(pleaseInterrupt); |
583 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
582 |
> |
await(pleaseArrive); |
583 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
584 |
|
Thread.currentThread().interrupt(); |
585 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
586 |
|
assertTrue(Thread.interrupted()); |
601 |
|
assertFalse(Thread.currentThread().isInterrupted()); |
602 |
|
pleaseInterrupt.countDown(); |
603 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
604 |
< |
assertTrue(Thread.currentThread().isInterrupted()); |
604 |
> |
assertTrue(Thread.interrupted()); |
605 |
|
}}); |
606 |
|
|
607 |
|
await(pleaseInterrupt); |
608 |
< |
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); |
608 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
609 |
|
t.interrupt(); |
610 |
|
Thread.currentThread().interrupt(); |
611 |
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
620 |
|
public void testAwaitAdvance4() { |
621 |
|
final Phaser phaser = new Phaser(4); |
622 |
|
final AtomicInteger count = new AtomicInteger(0); |
623 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
623 |
> |
List<Thread> threads = new ArrayList<>(); |
624 |
|
for (int i = 0; i < 4; i++) |
625 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
626 |
|
public void realRun() { |
627 |
|
for (int k = 0; k < 3; k++) { |
628 |
< |
assertEquals(2*k+1, phaser.arriveAndAwaitAdvance()); |
628 |
> |
assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance()); |
629 |
|
count.incrementAndGet(); |
630 |
< |
assertEquals(2*k+1, phaser.arrive()); |
631 |
< |
assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); |
632 |
< |
assertEquals(4*(k+1), count.get()); |
630 |
> |
assertEquals(2 * k + 1, phaser.arrive()); |
631 |
> |
assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1)); |
632 |
> |
assertEquals(4 * (k + 1), count.get()); |
633 |
|
}}})); |
634 |
|
|
635 |
|
for (Thread thread : threads) |
644 |
|
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
645 |
|
assertEquals(1, phaser.getPhase()); |
646 |
|
assertEquals(1, phaser.register()); |
647 |
< |
List<Thread> threads = new ArrayList<Thread>(); |
647 |
> |
List<Thread> threads = new ArrayList<>(); |
648 |
|
for (int i = 0; i < 8; i++) { |
649 |
|
final CountDownLatch latch = new CountDownLatch(1); |
650 |
|
final boolean goesFirst = ((i & 1) == 0); |
672 |
|
*/ |
673 |
|
public void testAwaitAdvanceTieredPhaser() throws Exception { |
674 |
|
final Phaser parent = new Phaser(); |
675 |
< |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
676 |
< |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
675 |
> |
final List<Phaser> zeroPartyChildren = new ArrayList<>(3); |
676 |
> |
final List<Phaser> onePartyChildren = new ArrayList<>(3); |
677 |
|
for (int i = 0; i < 3; i++) { |
678 |
|
zeroPartyChildren.add(new Phaser(parent, 0)); |
679 |
|
onePartyChildren.add(new Phaser(parent, 1)); |
680 |
|
} |
681 |
< |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
681 |
> |
final List<Phaser> phasers = new ArrayList<>(); |
682 |
|
phasers.addAll(zeroPartyChildren); |
683 |
|
phasers.addAll(onePartyChildren); |
684 |
|
phasers.add(parent); |
685 |
|
for (Phaser phaser : phasers) { |
686 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
687 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
688 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
688 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
689 |
|
} |
690 |
|
|
691 |
|
for (Phaser child : onePartyChildren) |
693 |
|
for (Phaser phaser : phasers) { |
694 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
695 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
696 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
696 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
|
assertEquals(1, phaser.awaitAdvance(0)); |
698 |
|
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
699 |
< |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
699 |
> |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); |
700 |
|
} |
701 |
|
|
702 |
|
for (Phaser child : onePartyChildren) |
704 |
|
for (Phaser phaser : phasers) { |
705 |
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
706 |
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
707 |
< |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
707 |
> |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); |
708 |
|
assertEquals(2, phaser.awaitAdvance(0)); |
709 |
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
710 |
< |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
710 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); |
711 |
|
assertEquals(2, phaser.awaitAdvance(1)); |
712 |
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
713 |
< |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
713 |
> |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS)); |
714 |
|
} |
715 |
|
} |
716 |
|
|
720 |
|
public void testAwaitAdvance6() { |
721 |
|
final Phaser phaser = new Phaser(3); |
722 |
|
final CountDownLatch pleaseForceTermination = new CountDownLatch(2); |
723 |
< |
final List<Thread> threads = new ArrayList<Thread>(); |
723 |
> |
final List<Thread> threads = new ArrayList<>(); |
724 |
|
for (int i = 0; i < 2; i++) { |
725 |
|
Runnable r = new CheckedRunnable() { |
726 |
|
public void realRun() { |
748 |
|
* unarrived parties |
749 |
|
*/ |
750 |
|
public void testArriveAndAwaitAdvance1() { |
751 |
+ |
Phaser phaser = new Phaser(); |
752 |
|
try { |
752 |
– |
Phaser phaser = new Phaser(); |
753 |
|
phaser.arriveAndAwaitAdvance(); |
754 |
|
shouldThrow(); |
755 |
|
} catch (IllegalStateException success) {} |
764 |
|
final Phaser phaser = new Phaser(1); |
765 |
|
final int THREADS = 3; |
766 |
|
final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); |
767 |
< |
final List<Thread> threads = new ArrayList<Thread>(); |
767 |
> |
final List<Thread> threads = new ArrayList<>(); |
768 |
|
for (int i = 0; i < THREADS; i++) |
769 |
|
threads.add(newStartedThread(new CheckedRunnable() { |
770 |
|
public void realRun() { |
780 |
|
assertEquals(THREADS, phaser.getArrivedParties()); |
781 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
782 |
|
for (Thread thread : threads) |
783 |
< |
waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS); |
783 |
> |
assertThreadBlocks(thread, Thread.State.WAITING); |
784 |
|
for (Thread thread : threads) |
785 |
|
assertTrue(thread.isAlive()); |
786 |
|
assertState(phaser, 0, THREADS + 1, 1); |