25 |
|
return new TestSuite(PhaserTest.class); |
26 |
|
} |
27 |
|
|
28 |
< |
/** Checks state of phaser. */ |
28 |
> |
private static final int maxParties = 65535; |
29 |
> |
|
30 |
> |
/** Checks state of unterminated phaser. */ |
31 |
|
protected void assertState(Phaser phaser, |
32 |
|
int phase, int parties, int unarrived) { |
33 |
|
assertEquals(phase, phaser.getPhase()); |
34 |
|
assertEquals(parties, phaser.getRegisteredParties()); |
35 |
|
assertEquals(unarrived, phaser.getUnarrivedParties()); |
36 |
|
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
37 |
< |
assertTrue((phaser.getPhase() >= 0) ^ phaser.isTerminated()); |
37 |
> |
assertFalse(phaser.isTerminated()); |
38 |
|
} |
39 |
|
|
40 |
|
/** Checks state of terminated phaser. */ |
41 |
< |
protected void assertTerminated(Phaser phaser, int parties, int unarrived) { |
41 |
> |
protected void assertTerminated(Phaser phaser, int maxPhase, int parties) { |
42 |
|
assertTrue(phaser.isTerminated()); |
43 |
< |
assertTrue(phaser.getPhase() < 0); |
43 |
> |
int expectedPhase = maxPhase + Integer.MIN_VALUE; |
44 |
> |
assertEquals(expectedPhase, phaser.getPhase()); |
45 |
|
assertEquals(parties, phaser.getRegisteredParties()); |
46 |
< |
assertEquals(unarrived, phaser.getUnarrivedParties()); |
47 |
< |
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
46 |
> |
assertEquals(expectedPhase, phaser.register()); |
47 |
> |
assertEquals(expectedPhase, phaser.arrive()); |
48 |
> |
assertEquals(expectedPhase, phaser.arriveAndDeregister()); |
49 |
|
} |
50 |
|
|
51 |
< |
protected void assertTerminated(Phaser phaser) { |
52 |
< |
assertTerminated(phaser, 0, 0); |
51 |
> |
protected void assertTerminated(Phaser phaser, int maxPhase) { |
52 |
> |
assertTerminated(phaser, maxPhase, 0); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
|
* Empty constructor builds a new Phaser with no parent, no registered |
57 |
|
* parties and initial phase number of 0 |
58 |
|
*/ |
59 |
< |
public void testConstructor1() { |
59 |
> |
public void testConstructorDefaultValues() { |
60 |
|
Phaser phaser = new Phaser(); |
61 |
|
assertNull(phaser.getParent()); |
62 |
+ |
assertEquals(0, phaser.getRegisteredParties()); |
63 |
|
assertEquals(0, phaser.getArrivedParties()); |
64 |
+ |
assertEquals(0, phaser.getUnarrivedParties()); |
65 |
|
assertEquals(0, phaser.getPhase()); |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* A negative party number for the constructor throws illegal argument |
70 |
< |
* exception |
69 |
> |
* Constructing with a negative number of parties throws |
70 |
> |
* IllegalArgumentException |
71 |
|
*/ |
72 |
< |
public void testConstructor2() { |
72 |
> |
public void testConstructorNegativeParties() { |
73 |
|
try { |
74 |
|
new Phaser(-1); |
75 |
|
shouldThrow(); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* The parent being input into the constructor should equal the original |
81 |
< |
* parent when being returned |
80 |
> |
* Constructing with a negative number of parties throws |
81 |
> |
* IllegalArgumentException |
82 |
|
*/ |
83 |
< |
public void testConstructor3() { |
84 |
< |
Phaser parent = new Phaser(); |
85 |
< |
assertEquals(parent, new Phaser(parent).getParent()); |
83 |
> |
public void testConstructorNegativeParties2() { |
84 |
> |
try { |
85 |
> |
new Phaser(new Phaser(), -1); |
86 |
> |
shouldThrow(); |
87 |
> |
} catch (IllegalArgumentException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* A negative party number for the constructor throws illegal argument |
92 |
< |
* exception |
91 |
> |
* Constructing with a number of parties > 65535 throws |
92 |
> |
* IllegalArgumentException |
93 |
|
*/ |
94 |
< |
public void testConstructor4() { |
94 |
> |
public void testConstructorPartiesExceedsLimit() { |
95 |
> |
new Phaser(maxParties); |
96 |
|
try { |
97 |
< |
new Phaser(new Phaser(), -1); |
97 |
> |
new Phaser(maxParties + 1); |
98 |
> |
shouldThrow(); |
99 |
> |
} catch (IllegalArgumentException success) {} |
100 |
> |
|
101 |
> |
new Phaser(new Phaser(), maxParties); |
102 |
> |
try { |
103 |
> |
new Phaser(new Phaser(), maxParties + 1); |
104 |
|
shouldThrow(); |
105 |
|
} catch (IllegalArgumentException success) {} |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
+ |
* The parent provided to the constructor should be returned from |
110 |
+ |
* a later call to getParent |
111 |
+ |
*/ |
112 |
+ |
public void testConstructor3() { |
113 |
+ |
Phaser parent = new Phaser(); |
114 |
+ |
assertSame(parent, new Phaser(parent).getParent()); |
115 |
+ |
assertNull(new Phaser(null).getParent()); |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
/** |
119 |
|
* The parent being input into the parameter should equal the original |
120 |
|
* parent when being returned |
121 |
|
*/ |
122 |
|
public void testConstructor5() { |
123 |
|
Phaser parent = new Phaser(); |
124 |
< |
assertEquals(parent, new Phaser(parent, 0).getParent()); |
124 |
> |
assertSame(parent, new Phaser(parent, 0).getParent()); |
125 |
> |
assertNull(new Phaser(null, 0).getParent()); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
< |
* register() will increment the number of unarrived parties by one and not |
130 |
< |
* affect its arrived parties |
129 |
> |
* register() will increment the number of unarrived parties by |
130 |
> |
* one and not affect its arrived parties |
131 |
|
*/ |
132 |
|
public void testRegister1() { |
133 |
|
Phaser phaser = new Phaser(); |
141 |
|
*/ |
142 |
|
public void testRegister2() { |
143 |
|
Phaser phaser = new Phaser(0); |
118 |
– |
int maxParties = (1 << 16) - 1; |
144 |
|
assertState(phaser, 0, 0, 0); |
145 |
|
assertEquals(0, phaser.bulkRegister(maxParties - 10)); |
146 |
|
assertState(phaser, 0, maxParties - 10, maxParties - 10); |
153 |
|
phaser.register(); |
154 |
|
shouldThrow(); |
155 |
|
} catch (IllegalStateException success) {} |
156 |
+ |
|
157 |
+ |
try { |
158 |
+ |
phaser.bulkRegister(Integer.MAX_VALUE); |
159 |
+ |
shouldThrow(); |
160 |
+ |
} catch (IllegalStateException success) {} |
161 |
|
} |
162 |
|
|
163 |
|
/** |
259 |
|
assertState(phaser, 0, 1, 1); |
260 |
|
} |
261 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
262 |
< |
assertTerminated(phaser); |
262 |
> |
assertTerminated(phaser, 1); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
290 |
|
public void testArrive3() { |
291 |
|
Phaser phaser = new Phaser(1); |
292 |
|
phaser.forceTermination(); |
293 |
< |
assertTerminated(phaser, 1, 1); |
293 |
> |
assertTerminated(phaser, 0, 1); |
294 |
> |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
295 |
|
assertTrue(phaser.arrive() < 0); |
296 |
|
assertTrue(phaser.register() < 0); |
297 |
|
assertTrue(phaser.arriveAndDeregister() < 0); |
332 |
|
Phaser parent = new Phaser(); |
333 |
|
Phaser child = new Phaser(parent); |
334 |
|
assertState(child, 0, 0, 0); |
335 |
< |
assertState(parent, 0, 1, 1); |
335 |
> |
assertState(parent, 0, 0, 0); |
336 |
|
assertEquals(0, child.register()); |
337 |
|
assertState(child, 0, 1, 1); |
338 |
|
assertState(parent, 0, 1, 1); |
339 |
|
assertEquals(0, child.arriveAndDeregister()); |
340 |
< |
assertTerminated(child); |
341 |
< |
assertTerminated(parent); |
340 |
> |
assertTerminated(child, 1); |
341 |
> |
assertTerminated(parent, 1); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
365 |
|
Phaser root = new Phaser(); |
366 |
|
Phaser parent = new Phaser(root); |
367 |
|
Phaser child = new Phaser(parent); |
368 |
< |
assertState(root, 0, 1, 1); |
369 |
< |
assertState(parent, 0, 1, 1); |
368 |
> |
assertState(root, 0, 0, 0); |
369 |
> |
assertState(parent, 0, 0, 0); |
370 |
|
assertState(child, 0, 0, 0); |
371 |
|
assertEquals(0, child.register()); |
372 |
|
assertState(root, 0, 1, 1); |
373 |
|
assertState(parent, 0, 1, 1); |
374 |
|
assertState(child, 0, 1, 1); |
375 |
|
assertEquals(0, child.arriveAndDeregister()); |
376 |
< |
assertTerminated(child); |
377 |
< |
assertTerminated(parent); |
378 |
< |
assertTerminated(root); |
376 |
> |
assertTerminated(child, 1); |
377 |
> |
assertTerminated(parent, 1); |
378 |
> |
assertTerminated(root, 1); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
393 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
394 |
|
assertState(phaser, 1, 1, 1); |
395 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
396 |
< |
assertTerminated(phaser); |
396 |
> |
assertTerminated(phaser, 2); |
397 |
|
awaitTermination(t, SHORT_DELAY_MS); |
398 |
|
} |
399 |
|
|
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
< |
* awaitAdvance continues waiting if interrupted |
420 |
> |
* awaitAdvance continues waiting if interrupted before waiting |
421 |
|
*/ |
422 |
< |
public void testAwaitAdvance3() throws InterruptedException { |
422 |
> |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
423 |
|
final Phaser phaser = new Phaser(); |
424 |
|
assertEquals(0, phaser.register()); |
425 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
426 |
|
|
427 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
428 |
|
public void realRun() throws InterruptedException { |
429 |
+ |
Thread.currentThread().interrupt(); |
430 |
|
assertEquals(0, phaser.register()); |
431 |
+ |
assertEquals(0, phaser.arrive()); |
432 |
+ |
threadStarted.countDown(); |
433 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
434 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
435 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
436 |
+ |
}}); |
437 |
+ |
|
438 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
439 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
440 |
+ |
assertEquals(0, phaser.arrive()); |
441 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
442 |
+ |
|
443 |
+ |
Thread.currentThread().interrupt(); |
444 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
445 |
+ |
assertTrue(Thread.interrupted()); |
446 |
+ |
} |
447 |
+ |
|
448 |
+ |
/** |
449 |
+ |
* awaitAdvance continues waiting if interrupted while waiting |
450 |
+ |
*/ |
451 |
+ |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
452 |
+ |
final Phaser phaser = new Phaser(); |
453 |
+ |
assertEquals(0, phaser.register()); |
454 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
455 |
+ |
|
456 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
457 |
+ |
public void realRun() throws InterruptedException { |
458 |
+ |
assertEquals(0, phaser.register()); |
459 |
+ |
assertEquals(0, phaser.arrive()); |
460 |
|
threadStarted.countDown(); |
461 |
< |
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
461 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
462 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
463 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
464 |
|
}}); |
465 |
+ |
|
466 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
467 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
468 |
|
t.interrupt(); |
469 |
|
assertEquals(0, phaser.arrive()); |
470 |
|
awaitTermination(t, SMALL_DELAY_MS); |
471 |
+ |
|
472 |
+ |
Thread.currentThread().interrupt(); |
473 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
474 |
+ |
assertTrue(Thread.interrupted()); |
475 |
+ |
} |
476 |
+ |
|
477 |
+ |
/** |
478 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
479 |
+ |
*/ |
480 |
+ |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
481 |
+ |
throws InterruptedException { |
482 |
+ |
final Phaser phaser = new Phaser(); |
483 |
+ |
assertEquals(0, phaser.register()); |
484 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
485 |
+ |
|
486 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
487 |
+ |
public void realRun() throws InterruptedException { |
488 |
+ |
Thread.currentThread().interrupt(); |
489 |
+ |
assertEquals(0, phaser.register()); |
490 |
+ |
threadStarted.countDown(); |
491 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
492 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
493 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
494 |
+ |
}}); |
495 |
+ |
|
496 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
497 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
498 |
+ |
Thread.currentThread().interrupt(); |
499 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
500 |
+ |
assertTrue(Thread.interrupted()); |
501 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
502 |
+ |
} |
503 |
+ |
|
504 |
+ |
/** |
505 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
506 |
+ |
*/ |
507 |
+ |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
508 |
+ |
throws InterruptedException { |
509 |
+ |
final Phaser phaser = new Phaser(); |
510 |
+ |
assertEquals(0, phaser.register()); |
511 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
512 |
+ |
|
513 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
514 |
+ |
public void realRun() throws InterruptedException { |
515 |
+ |
assertEquals(0, phaser.register()); |
516 |
+ |
threadStarted.countDown(); |
517 |
+ |
assertFalse(Thread.currentThread().isInterrupted()); |
518 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
519 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
520 |
+ |
}}); |
521 |
+ |
|
522 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
523 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
524 |
+ |
t.interrupt(); |
525 |
+ |
Thread.currentThread().interrupt(); |
526 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
527 |
+ |
assertTrue(Thread.interrupted()); |
528 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
529 |
|
} |
530 |
|
|
531 |
|
/** |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
+ |
* awaitAdvance returns the current phase in child phasers |
587 |
+ |
*/ |
588 |
+ |
public void testAwaitAdvanceTieredPhaser() throws Exception { |
589 |
+ |
final Phaser parent = new Phaser(); |
590 |
+ |
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
591 |
+ |
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
592 |
+ |
for (int i = 0; i < 3; i++) { |
593 |
+ |
zeroPartyChildren.add(new Phaser(parent, 0)); |
594 |
+ |
onePartyChildren.add(new Phaser(parent, 1)); |
595 |
+ |
} |
596 |
+ |
final List<Phaser> phasers = new ArrayList<Phaser>(); |
597 |
+ |
phasers.addAll(zeroPartyChildren); |
598 |
+ |
phasers.addAll(onePartyChildren); |
599 |
+ |
phasers.add(parent); |
600 |
+ |
for (Phaser phaser : phasers) { |
601 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
602 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
603 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
604 |
+ |
} |
605 |
+ |
|
606 |
+ |
for (Phaser child : onePartyChildren) |
607 |
+ |
assertEquals(0, child.arrive()); |
608 |
+ |
for (Phaser phaser : phasers) { |
609 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
610 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
611 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
612 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
613 |
+ |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
614 |
+ |
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
615 |
+ |
} |
616 |
+ |
|
617 |
+ |
for (Phaser child : onePartyChildren) |
618 |
+ |
assertEquals(1, child.arrive()); |
619 |
+ |
for (Phaser phaser : phasers) { |
620 |
+ |
assertEquals(-42, phaser.awaitAdvance(-42)); |
621 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
622 |
+ |
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
623 |
+ |
assertEquals(2, phaser.awaitAdvance(0)); |
624 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
625 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
626 |
+ |
assertEquals(2, phaser.awaitAdvance(1)); |
627 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
628 |
+ |
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
629 |
+ |
} |
630 |
+ |
} |
631 |
+ |
|
632 |
+ |
/** |
633 |
|
* awaitAdvance returns when the phaser is externally terminated |
634 |
|
*/ |
635 |
|
public void testAwaitAdvance6() throws InterruptedException { |
644 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
645 |
|
assertTrue(phaser.isTerminated()); |
646 |
|
assertTrue(phaser.getPhase() < 0); |
647 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
648 |
|
assertEquals(3, phaser.getRegisteredParties()); |
649 |
|
}}; |
650 |
|
threads.add(newStartedThread(r)); |
651 |
|
} |
652 |
|
threadsStarted.await(); |
653 |
|
phaser.forceTermination(); |
654 |
+ |
assertTrue(phaser.isTerminated()); |
655 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
656 |
|
for (Thread thread : threads) |
657 |
|
awaitTermination(thread, SMALL_DELAY_MS); |
486 |
– |
assertTrue(phaser.isTerminated()); |
487 |
– |
assertTrue(phaser.getPhase() < 0); |
658 |
|
assertEquals(3, phaser.getRegisteredParties()); |
659 |
|
} |
660 |
|
|
671 |
|
} |
672 |
|
|
673 |
|
/** |
504 |
– |
* Interrupted arriveAndAwaitAdvance does not throw InterruptedException |
505 |
– |
*/ |
506 |
– |
public void testArriveAndAwaitAdvance2() throws InterruptedException { |
507 |
– |
final Phaser phaser = new Phaser(2); |
508 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
509 |
– |
final AtomicBoolean advanced = new AtomicBoolean(false); |
510 |
– |
final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); |
511 |
– |
Thread t = newStartedThread(new CheckedRunnable() { |
512 |
– |
public void realRun() throws InterruptedException { |
513 |
– |
threadStarted.countDown(); |
514 |
– |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
515 |
– |
assertState(phaser, 1, 2, 2); |
516 |
– |
advanced.set(true); |
517 |
– |
assertTrue(Thread.currentThread().isInterrupted()); |
518 |
– |
while (!checkedInterruptStatus.get()) |
519 |
– |
Thread.yield(); |
520 |
– |
}}); |
521 |
– |
|
522 |
– |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
523 |
– |
t.interrupt(); |
524 |
– |
assertEquals(0, phaser.arrive()); |
525 |
– |
while (!advanced.get()) |
526 |
– |
Thread.yield(); |
527 |
– |
assertTrue(t.isInterrupted()); |
528 |
– |
checkedInterruptStatus.set(true); |
529 |
– |
awaitTermination(t, SMALL_DELAY_MS); |
530 |
– |
} |
531 |
– |
|
532 |
– |
/** |
674 |
|
* arriveAndAwaitAdvance waits for all threads to arrive, the |
675 |
|
* number of arrived parties is the same number that is accounted |
676 |
|
* for when the main thread awaitsAdvance |