25 |
|
return new TestSuite(PhaserTest.class); |
26 |
|
} |
27 |
|
|
28 |
+ |
private static final int maxParties = 65535; |
29 |
+ |
|
30 |
|
/** Checks state of phaser. */ |
31 |
|
protected void assertState(Phaser phaser, |
32 |
|
int phase, int parties, int unarrived) { |
54 |
|
* Empty constructor builds a new Phaser with no parent, no registered |
55 |
|
* parties and initial phase number of 0 |
56 |
|
*/ |
57 |
< |
public void testConstructor1() { |
57 |
> |
public void testConstructorDefaultValues() { |
58 |
|
Phaser phaser = new Phaser(); |
59 |
|
assertNull(phaser.getParent()); |
60 |
+ |
assertEquals(0, phaser.getRegisteredParties()); |
61 |
|
assertEquals(0, phaser.getArrivedParties()); |
62 |
+ |
assertEquals(0, phaser.getUnarrivedParties()); |
63 |
|
assertEquals(0, phaser.getPhase()); |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* A negative party number for the constructor throws illegal argument |
68 |
< |
* exception |
67 |
> |
* Constructing with a negative number of parties throws |
68 |
> |
* IllegalArgumentException |
69 |
|
*/ |
70 |
< |
public void testConstructor2() { |
70 |
> |
public void testConstructorNegativeParties() { |
71 |
|
try { |
72 |
|
new Phaser(-1); |
73 |
|
shouldThrow(); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* The parent being input into the constructor should equal the original |
79 |
< |
* parent when being returned |
78 |
> |
* Constructing with a negative number of parties throws |
79 |
> |
* IllegalArgumentException |
80 |
|
*/ |
81 |
< |
public void testConstructor3() { |
82 |
< |
Phaser parent = new Phaser(); |
83 |
< |
assertEquals(parent, new Phaser(parent).getParent()); |
81 |
> |
public void testConstructorNegativeParties2() { |
82 |
> |
try { |
83 |
> |
new Phaser(new Phaser(), -1); |
84 |
> |
shouldThrow(); |
85 |
> |
} catch (IllegalArgumentException success) {} |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* A negative party number for the constructor throws illegal argument |
90 |
< |
* exception |
89 |
> |
* Constructing with a number of parties > 65535 throws |
90 |
> |
* IllegalArgumentException |
91 |
|
*/ |
92 |
< |
public void testConstructor4() { |
92 |
> |
public void testConstructorPartiesExceedsLimit() { |
93 |
> |
new Phaser(maxParties); |
94 |
|
try { |
95 |
< |
new Phaser(new Phaser(), -1); |
95 |
> |
new Phaser(maxParties + 1); |
96 |
> |
shouldThrow(); |
97 |
> |
} catch (IllegalArgumentException success) {} |
98 |
> |
|
99 |
> |
new Phaser(new Phaser(), maxParties); |
100 |
> |
try { |
101 |
> |
new Phaser(new Phaser(), maxParties + 1); |
102 |
|
shouldThrow(); |
103 |
|
} catch (IllegalArgumentException success) {} |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
+ |
* The parent provided to the constructor should be returned from |
108 |
+ |
* a later call to getParent |
109 |
+ |
*/ |
110 |
+ |
public void testConstructor3() { |
111 |
+ |
Phaser parent = new Phaser(); |
112 |
+ |
assertSame(parent, new Phaser(parent).getParent()); |
113 |
+ |
assertNull(new Phaser(null).getParent()); |
114 |
+ |
} |
115 |
+ |
|
116 |
+ |
/** |
117 |
|
* The parent being input into the parameter should equal the original |
118 |
|
* parent when being returned |
119 |
|
*/ |
120 |
|
public void testConstructor5() { |
121 |
|
Phaser parent = new Phaser(); |
122 |
< |
assertEquals(parent, new Phaser(parent, 0).getParent()); |
122 |
> |
assertSame(parent, new Phaser(parent, 0).getParent()); |
123 |
> |
assertNull(new Phaser(null, 0).getParent()); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* register() will increment the number of unarrived parties by one and not |
128 |
< |
* affect its arrived parties |
127 |
> |
* register() will increment the number of unarrived parties by |
128 |
> |
* one and not affect its arrived parties |
129 |
|
*/ |
130 |
|
public void testRegister1() { |
131 |
|
Phaser phaser = new Phaser(); |
139 |
|
*/ |
140 |
|
public void testRegister2() { |
141 |
|
Phaser phaser = new Phaser(0); |
118 |
– |
int maxParties = (1 << 16) - 1; |
142 |
|
assertState(phaser, 0, 0, 0); |
143 |
|
assertEquals(0, phaser.bulkRegister(maxParties - 10)); |
144 |
|
assertState(phaser, 0, maxParties - 10, maxParties - 10); |
151 |
|
phaser.register(); |
152 |
|
shouldThrow(); |
153 |
|
} catch (IllegalStateException success) {} |
154 |
+ |
|
155 |
+ |
try { |
156 |
+ |
phaser.bulkRegister(Integer.MAX_VALUE); |
157 |
+ |
shouldThrow(); |
158 |
+ |
} catch (IllegalStateException success) {} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
258 |
|
} |
259 |
|
assertEquals(0, phaser.arriveAndDeregister()); |
260 |
|
assertTerminated(phaser); |
261 |
+ |
assertEquals(1, phaser.getPhase() + Integer.MIN_VALUE); |
262 |
|
} |
263 |
|
|
264 |
|
/** |
290 |
|
Phaser phaser = new Phaser(1); |
291 |
|
phaser.forceTermination(); |
292 |
|
assertTerminated(phaser, 1, 1); |
293 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
294 |
|
assertTrue(phaser.arrive() < 0); |
295 |
|
assertTrue(phaser.register() < 0); |
296 |
|
assertTrue(phaser.arriveAndDeregister() < 0); |
331 |
|
Phaser parent = new Phaser(); |
332 |
|
Phaser child = new Phaser(parent); |
333 |
|
assertState(child, 0, 0, 0); |
334 |
< |
assertState(parent, 0, 1, 1); |
334 |
> |
assertState(parent, 0, 0, 0); |
335 |
|
assertEquals(0, child.register()); |
336 |
|
assertState(child, 0, 1, 1); |
337 |
|
assertState(parent, 0, 1, 1); |
364 |
|
Phaser root = new Phaser(); |
365 |
|
Phaser parent = new Phaser(root); |
366 |
|
Phaser child = new Phaser(parent); |
367 |
< |
assertState(root, 0, 1, 1); |
368 |
< |
assertState(parent, 0, 1, 1); |
367 |
> |
assertState(root, 0, 0, 0); |
368 |
> |
assertState(parent, 0, 0, 0); |
369 |
|
assertState(child, 0, 0, 0); |
370 |
|
assertEquals(0, child.register()); |
371 |
|
assertState(root, 0, 1, 1); |
375 |
|
assertTerminated(child); |
376 |
|
assertTerminated(parent); |
377 |
|
assertTerminated(root); |
378 |
+ |
assertEquals(1, root.getPhase() + Integer.MIN_VALUE); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
394 |
|
assertState(phaser, 1, 1, 1); |
395 |
|
assertEquals(1, phaser.arriveAndDeregister()); |
396 |
|
assertTerminated(phaser); |
397 |
+ |
assertEquals(2, phaser.getPhase() + Integer.MIN_VALUE); |
398 |
|
awaitTermination(t, SHORT_DELAY_MS); |
399 |
|
} |
400 |
|
|
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* awaitAdvance continues waiting if interrupted |
421 |
> |
* awaitAdvance continues waiting if interrupted before waiting |
422 |
> |
*/ |
423 |
> |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
424 |
> |
final Phaser phaser = new Phaser(); |
425 |
> |
assertEquals(0, phaser.register()); |
426 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
427 |
> |
|
428 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
429 |
> |
public void realRun() throws InterruptedException { |
430 |
> |
Thread.currentThread().interrupt(); |
431 |
> |
assertEquals(0, phaser.register()); |
432 |
> |
assertEquals(0, phaser.arrive()); |
433 |
> |
threadStarted.countDown(); |
434 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
435 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
436 |
> |
assertTrue(Thread.currentThread().isInterrupted()); |
437 |
> |
}}); |
438 |
> |
|
439 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
440 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
441 |
> |
assertEquals(0, phaser.arrive()); |
442 |
> |
awaitTermination(t, SMALL_DELAY_MS); |
443 |
> |
|
444 |
> |
Thread.currentThread().interrupt(); |
445 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
446 |
> |
assertTrue(Thread.interrupted()); |
447 |
> |
} |
448 |
> |
|
449 |
> |
/** |
450 |
> |
* awaitAdvance continues waiting if interrupted while waiting |
451 |
|
*/ |
452 |
< |
public void testAwaitAdvance3() throws InterruptedException { |
452 |
> |
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
453 |
|
final Phaser phaser = new Phaser(); |
454 |
|
assertEquals(0, phaser.register()); |
455 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
457 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
458 |
|
public void realRun() throws InterruptedException { |
459 |
|
assertEquals(0, phaser.register()); |
460 |
+ |
assertEquals(0, phaser.arrive()); |
461 |
|
threadStarted.countDown(); |
462 |
< |
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
462 |
> |
assertFalse(Thread.currentThread().isInterrupted()); |
463 |
> |
assertEquals(1, phaser.awaitAdvance(0)); |
464 |
|
assertTrue(Thread.currentThread().isInterrupted()); |
465 |
|
}}); |
466 |
+ |
|
467 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
468 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
469 |
|
t.interrupt(); |
470 |
|
assertEquals(0, phaser.arrive()); |
471 |
|
awaitTermination(t, SMALL_DELAY_MS); |
472 |
+ |
|
473 |
+ |
Thread.currentThread().interrupt(); |
474 |
+ |
assertEquals(1, phaser.awaitAdvance(0)); |
475 |
+ |
assertTrue(Thread.interrupted()); |
476 |
+ |
} |
477 |
+ |
|
478 |
+ |
/** |
479 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
480 |
+ |
*/ |
481 |
+ |
public void testArriveAndAwaitAdvanceAfterInterrupt() |
482 |
+ |
throws InterruptedException { |
483 |
+ |
final Phaser phaser = new Phaser(); |
484 |
+ |
assertEquals(0, phaser.register()); |
485 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
486 |
+ |
|
487 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
488 |
+ |
public void realRun() throws InterruptedException { |
489 |
+ |
Thread.currentThread().interrupt(); |
490 |
+ |
assertEquals(0, phaser.register()); |
491 |
+ |
threadStarted.countDown(); |
492 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
493 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
494 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
495 |
+ |
}}); |
496 |
+ |
|
497 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
498 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
499 |
+ |
Thread.currentThread().interrupt(); |
500 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
501 |
+ |
assertTrue(Thread.interrupted()); |
502 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
503 |
+ |
} |
504 |
+ |
|
505 |
+ |
/** |
506 |
+ |
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
507 |
+ |
*/ |
508 |
+ |
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
509 |
+ |
throws InterruptedException { |
510 |
+ |
final Phaser phaser = new Phaser(); |
511 |
+ |
assertEquals(0, phaser.register()); |
512 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
513 |
+ |
|
514 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
515 |
+ |
public void realRun() throws InterruptedException { |
516 |
+ |
assertEquals(0, phaser.register()); |
517 |
+ |
threadStarted.countDown(); |
518 |
+ |
assertFalse(Thread.currentThread().isInterrupted()); |
519 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
520 |
+ |
assertTrue(Thread.currentThread().isInterrupted()); |
521 |
+ |
}}); |
522 |
+ |
|
523 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
524 |
+ |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
525 |
+ |
t.interrupt(); |
526 |
+ |
Thread.currentThread().interrupt(); |
527 |
+ |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
528 |
+ |
assertTrue(Thread.interrupted()); |
529 |
+ |
awaitTermination(t, SMALL_DELAY_MS); |
530 |
|
} |
531 |
|
|
532 |
|
/** |
598 |
|
assertTrue(phaser.awaitAdvance(0) < 0); |
599 |
|
assertTrue(phaser.isTerminated()); |
600 |
|
assertTrue(phaser.getPhase() < 0); |
601 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
602 |
|
assertEquals(3, phaser.getRegisteredParties()); |
603 |
|
}}; |
604 |
|
threads.add(newStartedThread(r)); |
605 |
|
} |
606 |
|
threadsStarted.await(); |
607 |
|
phaser.forceTermination(); |
608 |
+ |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
609 |
|
for (Thread thread : threads) |
610 |
|
awaitTermination(thread, SMALL_DELAY_MS); |
611 |
|
assertTrue(phaser.isTerminated()); |
626 |
|
} |
627 |
|
|
628 |
|
/** |
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 |
– |
/** |
629 |
|
* arriveAndAwaitAdvance waits for all threads to arrive, the |
630 |
|
* number of arrived parties is the same number that is accounted |
631 |
|
* for when the main thread awaitsAdvance |