ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.35
Committed: Wed Sep 21 12:33:56 2011 UTC (12 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.34: +1 -2 lines
Log Message:
Enable test case for multiple subphaser register/deregister

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.31 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 * Other contributors include John Vint
6     */
7    
8 jsr166 1.32 import junit.framework.*;
9 dl 1.1 import java.util.ArrayList;
10     import java.util.List;
11 jsr166 1.32 import java.util.concurrent.Phaser;
12     import java.util.concurrent.CountDownLatch;
13 jsr166 1.33 import java.util.concurrent.TimeoutException;
14 jsr166 1.16 import static java.util.concurrent.TimeUnit.MILLISECONDS;
15 jsr166 1.19 import static java.util.concurrent.TimeUnit.NANOSECONDS;
16 jsr166 1.32 import java.util.concurrent.atomic.AtomicBoolean;
17     import java.util.concurrent.atomic.AtomicInteger;
18 dl 1.1
19     public class PhaserTest extends JSR166TestCase {
20    
21     public static void main(String[] args) {
22     junit.textui.TestRunner.run(suite());
23     }
24    
25     public static Test suite() {
26     return new TestSuite(PhaserTest.class);
27     }
28    
29 jsr166 1.21 private static final int maxParties = 65535;
30 jsr166 1.22
31 jsr166 1.28 /** Checks state of unterminated phaser. */
32 jsr166 1.19 protected void assertState(Phaser phaser,
33     int phase, int parties, int unarrived) {
34     assertEquals(phase, phaser.getPhase());
35     assertEquals(parties, phaser.getRegisteredParties());
36     assertEquals(unarrived, phaser.getUnarrivedParties());
37     assertEquals(parties - unarrived, phaser.getArrivedParties());
38 jsr166 1.28 assertFalse(phaser.isTerminated());
39 jsr166 1.19 }
40    
41     /** Checks state of terminated phaser. */
42 jsr166 1.28 protected void assertTerminated(Phaser phaser, int maxPhase, int parties) {
43 jsr166 1.19 assertTrue(phaser.isTerminated());
44 jsr166 1.25 int expectedPhase = maxPhase + Integer.MIN_VALUE;
45 dl 1.26 assertEquals(expectedPhase, phaser.getPhase());
46     assertEquals(parties, phaser.getRegisteredParties());
47 jsr166 1.25 assertEquals(expectedPhase, phaser.register());
48     assertEquals(expectedPhase, phaser.arrive());
49     assertEquals(expectedPhase, phaser.arriveAndDeregister());
50 jsr166 1.19 }
51    
52 jsr166 1.25 protected void assertTerminated(Phaser phaser, int maxPhase) {
53 jsr166 1.28 assertTerminated(phaser, maxPhase, 0);
54 jsr166 1.19 }
55    
56 dl 1.1 /**
57     * Empty constructor builds a new Phaser with no parent, no registered
58     * parties and initial phase number of 0
59     */
60 jsr166 1.21 public void testConstructorDefaultValues() {
61 dl 1.1 Phaser phaser = new Phaser();
62     assertNull(phaser.getParent());
63 jsr166 1.21 assertEquals(0, phaser.getRegisteredParties());
64 dl 1.1 assertEquals(0, phaser.getArrivedParties());
65 jsr166 1.21 assertEquals(0, phaser.getUnarrivedParties());
66 dl 1.1 assertEquals(0, phaser.getPhase());
67     }
68    
69     /**
70 jsr166 1.21 * Constructing with a negative number of parties throws
71     * IllegalArgumentException
72 dl 1.1 */
73 jsr166 1.21 public void testConstructorNegativeParties() {
74 dl 1.1 try {
75     new Phaser(-1);
76 jsr166 1.6 shouldThrow();
77 jsr166 1.8 } catch (IllegalArgumentException success) {}
78 dl 1.1 }
79    
80     /**
81 jsr166 1.21 * Constructing with a negative number of parties throws
82     * IllegalArgumentException
83 dl 1.1 */
84 jsr166 1.21 public void testConstructorNegativeParties2() {
85     try {
86     new Phaser(new Phaser(), -1);
87     shouldThrow();
88     } catch (IllegalArgumentException success) {}
89 dl 1.1 }
90    
91     /**
92 jsr166 1.21 * Constructing with a number of parties > 65535 throws
93     * IllegalArgumentException
94 dl 1.1 */
95 jsr166 1.21 public void testConstructorPartiesExceedsLimit() {
96     new Phaser(maxParties);
97 dl 1.1 try {
98 jsr166 1.21 new Phaser(maxParties + 1);
99     shouldThrow();
100     } catch (IllegalArgumentException success) {}
101    
102     new Phaser(new Phaser(), maxParties);
103     try {
104     new Phaser(new Phaser(), maxParties + 1);
105 dl 1.1 shouldThrow();
106 jsr166 1.8 } catch (IllegalArgumentException success) {}
107 dl 1.1 }
108    
109     /**
110 jsr166 1.21 * The parent provided to the constructor should be returned from
111     * a later call to getParent
112     */
113     public void testConstructor3() {
114     Phaser parent = new Phaser();
115     assertSame(parent, new Phaser(parent).getParent());
116     assertNull(new Phaser(null).getParent());
117     }
118    
119     /**
120 dl 1.1 * The parent being input into the parameter should equal the original
121     * parent when being returned
122     */
123     public void testConstructor5() {
124     Phaser parent = new Phaser();
125 jsr166 1.21 assertSame(parent, new Phaser(parent, 0).getParent());
126     assertNull(new Phaser(null, 0).getParent());
127 dl 1.1 }
128    
129     /**
130 jsr166 1.21 * register() will increment the number of unarrived parties by
131     * one and not affect its arrived parties
132 dl 1.1 */
133     public void testRegister1() {
134     Phaser phaser = new Phaser();
135 jsr166 1.19 assertState(phaser, 0, 0, 0);
136     assertEquals(0, phaser.register());
137     assertState(phaser, 0, 1, 1);
138 dl 1.1 }
139    
140     /**
141 jsr166 1.4 * Registering more than 65536 parties causes IllegalStateException
142 dl 1.1 */
143     public void testRegister2() {
144     Phaser phaser = new Phaser(0);
145 jsr166 1.19 assertState(phaser, 0, 0, 0);
146     assertEquals(0, phaser.bulkRegister(maxParties - 10));
147     assertState(phaser, 0, maxParties - 10, maxParties - 10);
148     for (int i = 0; i < 10; i++) {
149     assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i);
150     assertEquals(0, phaser.register());
151 dl 1.1 }
152 jsr166 1.19 assertState(phaser, 0, maxParties, maxParties);
153 dl 1.1 try {
154     phaser.register();
155     shouldThrow();
156 jsr166 1.8 } catch (IllegalStateException success) {}
157 jsr166 1.21
158     try {
159     phaser.bulkRegister(Integer.MAX_VALUE);
160     shouldThrow();
161     } catch (IllegalStateException success) {}
162 jsr166 1.30
163     assertEquals(0, phaser.bulkRegister(0));
164     assertState(phaser, 0, maxParties, maxParties);
165 dl 1.1 }
166    
167     /**
168 jsr166 1.34 * register() correctly returns the current barrier phase number
169     * when invoked
170 dl 1.1 */
171     public void testRegister3() {
172     Phaser phaser = new Phaser();
173     assertEquals(0, phaser.register());
174 jsr166 1.19 assertEquals(0, phaser.arrive());
175 dl 1.1 assertEquals(1, phaser.register());
176 jsr166 1.19 assertState(phaser, 1, 2, 2);
177 dl 1.1 }
178    
179     /**
180 jsr166 1.34 * register causes the next arrive to not increment the phase
181     * rather retain the phase number
182 dl 1.1 */
183     public void testRegister4() {
184     Phaser phaser = new Phaser(1);
185 jsr166 1.19 assertEquals(0, phaser.arrive());
186     assertEquals(1, phaser.register());
187     assertEquals(1, phaser.arrive());
188     assertState(phaser, 1, 2, 1);
189 dl 1.1 }
190    
191     /**
192 jsr166 1.34 * register on a subphaser that is currently empty succeeds, even
193     * in the presence of another non-empty subphaser
194     */
195 dl 1.35 public void testRegisterEmptySubPhaser() {
196 jsr166 1.34 Phaser root = new Phaser();
197     Phaser child1 = new Phaser(root, 1);
198     Phaser child2 = new Phaser(root, 0);
199     assertEquals(0, child2.register());
200     assertEquals(0, child2.arriveAndDeregister());
201     assertEquals(0, child2.register());
202     assertEquals(0, child2.arriveAndDeregister());
203     assertState(child2, 0, 0, 0);
204     }
205    
206     /**
207 dl 1.1 * Invoking bulkRegister with a negative parameter throws an
208 jsr166 1.4 * IllegalArgumentException
209 dl 1.1 */
210     public void testBulkRegister1() {
211     try {
212     new Phaser().bulkRegister(-1);
213     shouldThrow();
214 jsr166 1.8 } catch (IllegalArgumentException success) {}
215 dl 1.1 }
216    
217     /**
218 jsr166 1.34 * bulkRegister should correctly record the number of unarrived
219     * parties with the number of parties being registered
220 dl 1.1 */
221     public void testBulkRegister2() {
222     Phaser phaser = new Phaser();
223 jsr166 1.30 assertEquals(0, phaser.bulkRegister(0));
224     assertState(phaser, 0, 0, 0);
225 jsr166 1.19 assertEquals(0, phaser.bulkRegister(20));
226     assertState(phaser, 0, 20, 20);
227 dl 1.1 }
228    
229     /**
230 jsr166 1.4 * Registering with a number of parties greater than or equal to 1<<16
231     * throws IllegalStateException.
232 dl 1.1 */
233     public void testBulkRegister3() {
234 jsr166 1.19 assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1));
235    
236 dl 1.1 try {
237     new Phaser().bulkRegister(1 << 16);
238     shouldThrow();
239 jsr166 1.8 } catch (IllegalStateException success) {}
240 jsr166 1.19
241     try {
242     new Phaser(2).bulkRegister((1 << 16) - 2);
243     shouldThrow();
244     } catch (IllegalStateException success) {}
245 dl 1.1 }
246    
247     /**
248     * the phase number increments correctly when tripping the barrier
249     */
250     public void testPhaseIncrement1() {
251     for (int size = 1; size < nine; size++) {
252     final Phaser phaser = new Phaser(size);
253     for (int index = 0; index <= (1 << size); index++) {
254     int phase = phaser.arrive();
255     assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
256     }
257     }
258     }
259    
260     /**
261 jsr166 1.19 * arrive() on a registered phaser increments phase.
262 dl 1.1 */
263     public void testArrive1() {
264     Phaser phaser = new Phaser(1);
265 jsr166 1.19 assertState(phaser, 0, 1, 1);
266     assertEquals(0, phaser.arrive());
267     assertState(phaser, 1, 1, 1);
268     }
269    
270     /**
271     * arriveAndDeregister does not wait for others to arrive at barrier
272     */
273 jsr166 1.33 public void testArriveAndDeregister() {
274 jsr166 1.19 final Phaser phaser = new Phaser(1);
275     for (int i = 0; i < 10; i++) {
276     assertState(phaser, 0, 1, 1);
277     assertEquals(0, phaser.register());
278     assertState(phaser, 0, 2, 2);
279     assertEquals(0, phaser.arriveAndDeregister());
280     assertState(phaser, 0, 1, 1);
281     }
282     assertEquals(0, phaser.arriveAndDeregister());
283 jsr166 1.25 assertTerminated(phaser, 1);
284 dl 1.1 }
285    
286     /**
287 jsr166 1.7 * arriveAndDeregister does not wait for others to arrive at barrier
288 dl 1.1 */
289 jsr166 1.33 public void testArrive2() {
290 jsr166 1.19 final Phaser phaser = new Phaser();
291     assertEquals(0, phaser.register());
292 jsr166 1.7 List<Thread> threads = new ArrayList<Thread>();
293 jsr166 1.19 for (int i = 0; i < 10; i++) {
294     assertEquals(0, phaser.register());
295 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
296 jsr166 1.33 public void realRun() {
297 jsr166 1.19 assertEquals(0, phaser.arriveAndDeregister());
298 jsr166 1.7 }}));
299 jsr166 1.19 }
300 dl 1.1
301 jsr166 1.7 for (Thread thread : threads)
302 jsr166 1.33 awaitTermination(thread);
303 jsr166 1.19 assertState(phaser, 0, 1, 1);
304     assertEquals(0, phaser.arrive());
305     assertState(phaser, 1, 1, 1);
306 dl 1.1 }
307    
308     /**
309 jsr166 1.3 * arrive() returns a negative number if the Phaser is terminated
310 dl 1.1 */
311     public void testArrive3() {
312     Phaser phaser = new Phaser(1);
313     phaser.forceTermination();
314 jsr166 1.28 assertTerminated(phaser, 0, 1);
315 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
316 dl 1.1 assertTrue(phaser.arrive() < 0);
317 jsr166 1.19 assertTrue(phaser.register() < 0);
318     assertTrue(phaser.arriveAndDeregister() < 0);
319     assertTrue(phaser.awaitAdvance(1) < 0);
320     assertTrue(phaser.getPhase() < 0);
321 dl 1.1 }
322    
323     /**
324     * arriveAndDeregister() throws IllegalStateException if number of
325 jsr166 1.3 * registered or unarrived parties would become negative
326 dl 1.1 */
327     public void testArriveAndDeregister1() {
328     try {
329     Phaser phaser = new Phaser();
330     phaser.arriveAndDeregister();
331     shouldThrow();
332 jsr166 1.8 } catch (IllegalStateException success) {}
333 dl 1.1 }
334    
335     /**
336 jsr166 1.19 * arriveAndDeregister reduces the number of arrived parties
337 dl 1.1 */
338 jsr166 1.19 public void testArriveAndDeregister2() {
339 dl 1.1 final Phaser phaser = new Phaser(1);
340 jsr166 1.19 assertEquals(0, phaser.register());
341     assertEquals(0, phaser.arrive());
342     assertState(phaser, 0, 2, 1);
343     assertEquals(0, phaser.arriveAndDeregister());
344     assertState(phaser, 1, 1, 1);
345 dl 1.1 }
346    
347     /**
348 jsr166 1.19 * arriveAndDeregister arrives at the barrier on a phaser with a parent and
349 dl 1.1 * when a deregistration occurs and causes the phaser to have zero parties
350     * its parent will be deregistered as well
351     */
352 jsr166 1.19 public void testArriveAndDeregister3() {
353 dl 1.1 Phaser parent = new Phaser();
354 jsr166 1.19 Phaser child = new Phaser(parent);
355     assertState(child, 0, 0, 0);
356 dl 1.23 assertState(parent, 0, 0, 0);
357 jsr166 1.19 assertEquals(0, child.register());
358     assertState(child, 0, 1, 1);
359     assertState(parent, 0, 1, 1);
360     assertEquals(0, child.arriveAndDeregister());
361 jsr166 1.25 assertTerminated(child, 1);
362     assertTerminated(parent, 1);
363 dl 1.1 }
364    
365     /**
366     * arriveAndDeregister deregisters one party from its parent when
367 jsr166 1.19 * the number of parties of child is zero after deregistration
368 dl 1.1 */
369 jsr166 1.19 public void testArriveAndDeregister4() {
370 dl 1.1 Phaser parent = new Phaser();
371 jsr166 1.19 Phaser child = new Phaser(parent);
372     assertEquals(0, parent.register());
373     assertEquals(0, child.register());
374     assertState(child, 0, 1, 1);
375     assertState(parent, 0, 2, 2);
376     assertEquals(0, child.arriveAndDeregister());
377     assertState(child, 0, 0, 0);
378     assertState(parent, 0, 1, 1);
379 dl 1.1 }
380    
381     /**
382     * arriveAndDeregister deregisters one party from its parent when
383     * the number of parties of root is nonzero after deregistration.
384     */
385     public void testArriveAndDeregister5() {
386 jsr166 1.19 Phaser root = new Phaser();
387     Phaser parent = new Phaser(root);
388 dl 1.1 Phaser child = new Phaser(parent);
389 dl 1.23 assertState(root, 0, 0, 0);
390     assertState(parent, 0, 0, 0);
391 jsr166 1.19 assertState(child, 0, 0, 0);
392     assertEquals(0, child.register());
393     assertState(root, 0, 1, 1);
394     assertState(parent, 0, 1, 1);
395     assertState(child, 0, 1, 1);
396     assertEquals(0, child.arriveAndDeregister());
397 jsr166 1.25 assertTerminated(child, 1);
398     assertTerminated(parent, 1);
399     assertTerminated(root, 1);
400 dl 1.1 }
401    
402     /**
403     * arriveAndDeregister returns the phase in which it leaves the
404     * phaser in after deregistration
405     */
406 jsr166 1.33 public void testArriveAndDeregister6() {
407 dl 1.1 final Phaser phaser = new Phaser(2);
408 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
409 jsr166 1.9 public void realRun() {
410 jsr166 1.19 assertEquals(0, phaser.arrive());
411 jsr166 1.7 }});
412 jsr166 1.19 assertEquals(1, phaser.arriveAndAwaitAdvance());
413     assertState(phaser, 1, 2, 2);
414     assertEquals(1, phaser.arriveAndDeregister());
415     assertState(phaser, 1, 1, 1);
416     assertEquals(1, phaser.arriveAndDeregister());
417 jsr166 1.25 assertTerminated(phaser, 2);
418 jsr166 1.33 awaitTermination(t);
419 dl 1.1 }
420    
421     /**
422     * awaitAdvance succeeds upon advance
423     */
424     public void testAwaitAdvance1() {
425     final Phaser phaser = new Phaser(1);
426 jsr166 1.19 assertEquals(0, phaser.arrive());
427     assertEquals(1, phaser.awaitAdvance(0));
428 dl 1.1 }
429    
430     /**
431     * awaitAdvance with a negative parameter will return without affecting the
432     * phaser
433     */
434     public void testAwaitAdvance2() {
435 jsr166 1.6 Phaser phaser = new Phaser();
436 jsr166 1.19 assertTrue(phaser.awaitAdvance(-1) < 0);
437     assertState(phaser, 0, 0, 0);
438 dl 1.1 }
439    
440     /**
441 jsr166 1.33 * awaitAdvanceInterruptibly blocks interruptibly
442     */
443     public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
444     final Phaser phaser = new Phaser(1);
445     final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
446    
447     Thread t1 = newStartedThread(new CheckedRunnable() {
448     public void realRun() {
449     Thread.currentThread().interrupt();
450     try {
451     phaser.awaitAdvanceInterruptibly(0);
452     shouldThrow();
453     } catch (InterruptedException success) {}
454     assertFalse(Thread.interrupted());
455    
456     pleaseInterrupt.countDown();
457     try {
458     phaser.awaitAdvanceInterruptibly(0);
459     shouldThrow();
460     } catch (InterruptedException success) {}
461     assertFalse(Thread.interrupted());
462     }});
463    
464     Thread t2 = newStartedThread(new CheckedRunnable() {
465     public void realRun() throws TimeoutException {
466     Thread.currentThread().interrupt();
467     try {
468     phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
469     shouldThrow();
470     } catch (InterruptedException success) {}
471     assertFalse(Thread.interrupted());
472    
473     pleaseInterrupt.countDown();
474     try {
475     phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
476     shouldThrow();
477     } catch (InterruptedException success) {}
478     assertFalse(Thread.interrupted());
479     }});
480    
481     await(pleaseInterrupt);
482     assertState(phaser, 0, 1, 1);
483     assertThreadsStayAlive(t1, t2);
484     t1.interrupt();
485     t2.interrupt();
486     awaitTermination(t1);
487     awaitTermination(t2);
488     assertState(phaser, 0, 1, 1);
489     assertEquals(0, phaser.arrive());
490     assertState(phaser, 1, 1, 1);
491     }
492    
493     /**
494 jsr166 1.20 * awaitAdvance continues waiting if interrupted before waiting
495 dl 1.1 */
496 jsr166 1.33 public void testAwaitAdvanceAfterInterrupt() {
497 dl 1.1 final Phaser phaser = new Phaser();
498 jsr166 1.19 assertEquals(0, phaser.register());
499 jsr166 1.33 final CountDownLatch pleaseArrive = new CountDownLatch(1);
500 dl 1.1
501 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
502 jsr166 1.33 public void realRun() {
503 jsr166 1.20 Thread.currentThread().interrupt();
504 jsr166 1.19 assertEquals(0, phaser.register());
505 jsr166 1.20 assertEquals(0, phaser.arrive());
506 jsr166 1.33 pleaseArrive.countDown();
507 jsr166 1.20 assertTrue(Thread.currentThread().isInterrupted());
508     assertEquals(1, phaser.awaitAdvance(0));
509 jsr166 1.33 assertTrue(Thread.interrupted());
510 jsr166 1.5 }});
511 jsr166 1.20
512 jsr166 1.33 await(pleaseArrive);
513     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
514 jsr166 1.20 assertEquals(0, phaser.arrive());
515 jsr166 1.33 awaitTermination(t);
516 jsr166 1.20
517     Thread.currentThread().interrupt();
518     assertEquals(1, phaser.awaitAdvance(0));
519     assertTrue(Thread.interrupted());
520     }
521    
522     /**
523 jsr166 1.33 * awaitAdvance continues waiting if interrupted while waiting
524 jsr166 1.20 */
525 jsr166 1.33 public void testAwaitAdvanceBeforeInterrupt() {
526 jsr166 1.20 final Phaser phaser = new Phaser();
527     assertEquals(0, phaser.register());
528 jsr166 1.33 final CountDownLatch pleaseArrive = new CountDownLatch(1);
529 jsr166 1.20
530     Thread t = newStartedThread(new CheckedRunnable() {
531 jsr166 1.33 public void realRun() {
532 jsr166 1.20 assertEquals(0, phaser.register());
533     assertEquals(0, phaser.arrive());
534     assertFalse(Thread.currentThread().isInterrupted());
535 jsr166 1.33 pleaseArrive.countDown();
536 jsr166 1.20 assertEquals(1, phaser.awaitAdvance(0));
537 jsr166 1.33 assertTrue(Thread.interrupted());
538 jsr166 1.20 }});
539    
540 jsr166 1.33 await(pleaseArrive);
541     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
542 jsr166 1.7 t.interrupt();
543 jsr166 1.19 assertEquals(0, phaser.arrive());
544 jsr166 1.33 awaitTermination(t);
545 jsr166 1.20
546     Thread.currentThread().interrupt();
547     assertEquals(1, phaser.awaitAdvance(0));
548     assertTrue(Thread.interrupted());
549     }
550    
551     /**
552     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
553     */
554 jsr166 1.33 public void testArriveAndAwaitAdvanceAfterInterrupt() {
555 jsr166 1.20 final Phaser phaser = new Phaser();
556     assertEquals(0, phaser.register());
557 jsr166 1.33 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
558 jsr166 1.20
559     Thread t = newStartedThread(new CheckedRunnable() {
560 jsr166 1.33 public void realRun() {
561 jsr166 1.20 Thread.currentThread().interrupt();
562     assertEquals(0, phaser.register());
563 jsr166 1.33 pleaseInterrupt.countDown();
564 jsr166 1.20 assertTrue(Thread.currentThread().isInterrupted());
565     assertEquals(1, phaser.arriveAndAwaitAdvance());
566     assertTrue(Thread.currentThread().isInterrupted());
567     }});
568    
569 jsr166 1.33 await(pleaseInterrupt);
570     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
571 jsr166 1.20 Thread.currentThread().interrupt();
572     assertEquals(1, phaser.arriveAndAwaitAdvance());
573     assertTrue(Thread.interrupted());
574 jsr166 1.33 awaitTermination(t);
575 jsr166 1.20 }
576    
577     /**
578     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
579     */
580 jsr166 1.33 public void testArriveAndAwaitAdvanceBeforeInterrupt() {
581 jsr166 1.20 final Phaser phaser = new Phaser();
582     assertEquals(0, phaser.register());
583 jsr166 1.33 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
584 jsr166 1.20
585     Thread t = newStartedThread(new CheckedRunnable() {
586 jsr166 1.33 public void realRun() {
587 jsr166 1.20 assertEquals(0, phaser.register());
588     assertFalse(Thread.currentThread().isInterrupted());
589 jsr166 1.33 pleaseInterrupt.countDown();
590 jsr166 1.20 assertEquals(1, phaser.arriveAndAwaitAdvance());
591     assertTrue(Thread.currentThread().isInterrupted());
592     }});
593    
594 jsr166 1.33 await(pleaseInterrupt);
595     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
596 jsr166 1.20 t.interrupt();
597     Thread.currentThread().interrupt();
598     assertEquals(1, phaser.arriveAndAwaitAdvance());
599     assertTrue(Thread.interrupted());
600 jsr166 1.33 awaitTermination(t);
601 dl 1.1 }
602    
603     /**
604     * awaitAdvance atomically waits for all parties within the same phase to
605     * complete before continuing
606     */
607 jsr166 1.33 public void testAwaitAdvance4() {
608 jsr166 1.5 final Phaser phaser = new Phaser(4);
609 jsr166 1.19 final AtomicInteger count = new AtomicInteger(0);
610 jsr166 1.5 List<Thread> threads = new ArrayList<Thread>();
611 jsr166 1.19 for (int i = 0; i < 4; i++)
612 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
613 jsr166 1.9 public void realRun() {
614 jsr166 1.19 for (int k = 0; k < 3; k++) {
615     assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
616     count.incrementAndGet();
617     assertEquals(2*k+1, phaser.arrive());
618     assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
619     assertEquals(count.get(), 4*(k+1));
620     }}}));
621    
622 jsr166 1.5 for (Thread thread : threads)
623 jsr166 1.33 awaitTermination(thread);
624 dl 1.1 }
625    
626     /**
627     * awaitAdvance returns the current phase
628     */
629 jsr166 1.33 public void testAwaitAdvance5() {
630 dl 1.1 final Phaser phaser = new Phaser(1);
631 jsr166 1.19 assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
632     assertEquals(1, phaser.getPhase());
633     assertEquals(1, phaser.register());
634 jsr166 1.7 List<Thread> threads = new ArrayList<Thread>();
635     for (int i = 0; i < 8; i++) {
636 jsr166 1.18 final CountDownLatch latch = new CountDownLatch(1);
637     final boolean goesFirst = ((i & 1) == 0);
638 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
639 jsr166 1.33 public void realRun() {
640 jsr166 1.18 if (goesFirst)
641     latch.countDown();
642     else
643 jsr166 1.33 await(latch);
644 dl 1.1 phaser.arrive();
645 jsr166 1.7 }}));
646 jsr166 1.18 if (goesFirst)
647 jsr166 1.33 await(latch);
648 jsr166 1.18 else
649     latch.countDown();
650 jsr166 1.19 assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
651     assertEquals(i + 2, phaser.getPhase());
652 dl 1.1 }
653 jsr166 1.7 for (Thread thread : threads)
654 jsr166 1.33 awaitTermination(thread);
655 dl 1.1 }
656    
657     /**
658 jsr166 1.29 * awaitAdvance returns the current phase in child phasers
659     */
660     public void testAwaitAdvanceTieredPhaser() throws Exception {
661     final Phaser parent = new Phaser();
662     final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3);
663     final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3);
664     for (int i = 0; i < 3; i++) {
665     zeroPartyChildren.add(new Phaser(parent, 0));
666     onePartyChildren.add(new Phaser(parent, 1));
667     }
668     final List<Phaser> phasers = new ArrayList<Phaser>();
669     phasers.addAll(zeroPartyChildren);
670     phasers.addAll(onePartyChildren);
671     phasers.add(parent);
672     for (Phaser phaser : phasers) {
673     assertEquals(-42, phaser.awaitAdvance(-42));
674     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
675     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
676     }
677    
678     for (Phaser child : onePartyChildren)
679     assertEquals(0, child.arrive());
680     for (Phaser phaser : phasers) {
681     assertEquals(-42, phaser.awaitAdvance(-42));
682     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
683     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
684     assertEquals(1, phaser.awaitAdvance(0));
685     assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
686     assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
687     }
688    
689     for (Phaser child : onePartyChildren)
690     assertEquals(1, child.arrive());
691     for (Phaser phaser : phasers) {
692     assertEquals(-42, phaser.awaitAdvance(-42));
693     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
694     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
695     assertEquals(2, phaser.awaitAdvance(0));
696     assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
697     assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
698     assertEquals(2, phaser.awaitAdvance(1));
699     assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
700     assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
701     }
702     }
703    
704     /**
705 dl 1.1 * awaitAdvance returns when the phaser is externally terminated
706     */
707 jsr166 1.33 public void testAwaitAdvance6() {
708 dl 1.1 final Phaser phaser = new Phaser(3);
709 jsr166 1.33 final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
710 jsr166 1.18 final List<Thread> threads = new ArrayList<Thread>();
711     for (int i = 0; i < 2; i++) {
712     Runnable r = new CheckedRunnable() {
713     public void realRun() {
714 jsr166 1.19 assertEquals(0, phaser.arrive());
715 jsr166 1.33 pleaseForceTermination.countDown();
716 jsr166 1.19 assertTrue(phaser.awaitAdvance(0) < 0);
717 jsr166 1.18 assertTrue(phaser.isTerminated());
718 jsr166 1.19 assertTrue(phaser.getPhase() < 0);
719 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
720 jsr166 1.19 assertEquals(3, phaser.getRegisteredParties());
721 jsr166 1.18 }};
722     threads.add(newStartedThread(r));
723     }
724 jsr166 1.33 await(pleaseForceTermination);
725 dl 1.1 phaser.forceTermination();
726 jsr166 1.27 assertTrue(phaser.isTerminated());
727 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
728 jsr166 1.18 for (Thread thread : threads)
729 jsr166 1.33 awaitTermination(thread);
730 jsr166 1.19 assertEquals(3, phaser.getRegisteredParties());
731 dl 1.1 }
732    
733     /**
734     * arriveAndAwaitAdvance throws IllegalStateException with no
735     * unarrived parties
736     */
737     public void testArriveAndAwaitAdvance1() {
738     try {
739     Phaser phaser = new Phaser();
740     phaser.arriveAndAwaitAdvance();
741     shouldThrow();
742 jsr166 1.8 } catch (IllegalStateException success) {}
743 dl 1.1 }
744    
745     /**
746     * arriveAndAwaitAdvance waits for all threads to arrive, the
747     * number of arrived parties is the same number that is accounted
748     * for when the main thread awaitsAdvance
749     */
750 jsr166 1.33 public void testArriveAndAwaitAdvance3() {
751 dl 1.1 final Phaser phaser = new Phaser(1);
752 jsr166 1.19 final int THREADS = 3;
753 jsr166 1.33 final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
754 jsr166 1.7 final List<Thread> threads = new ArrayList<Thread>();
755 jsr166 1.19 for (int i = 0; i < THREADS; i++)
756 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
757 jsr166 1.33 public void realRun() {
758 jsr166 1.19 assertEquals(0, phaser.register());
759 jsr166 1.33 pleaseArrive.countDown();
760 jsr166 1.19 assertEquals(1, phaser.arriveAndAwaitAdvance());
761     }}));
762    
763 jsr166 1.33 await(pleaseArrive);
764     long startTime = System.nanoTime();
765 jsr166 1.19 while (phaser.getArrivedParties() < THREADS)
766     Thread.yield();
767     assertEquals(THREADS, phaser.getArrivedParties());
768 jsr166 1.33 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
769     for (Thread thread : threads)
770     waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS);
771 jsr166 1.19 for (Thread thread : threads)
772     assertTrue(thread.isAlive());
773     assertState(phaser, 0, THREADS + 1, 1);
774 dl 1.12 phaser.arriveAndAwaitAdvance();
775 jsr166 1.7 for (Thread thread : threads)
776 jsr166 1.33 awaitTermination(thread);
777 jsr166 1.19 assertState(phaser, 1, THREADS + 1, THREADS + 1);
778 dl 1.1 }
779    
780     }