ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.34
Committed: Mon Jun 27 04:07:51 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +22 -6 lines
Log Message:
add failing test XXXXtestRegisterEmptySubPhaser

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     * XXXX broken (hangs) as of 2011-06-26
195     */
196     public void XXXXtestRegisterEmptySubPhaser() {
197     Phaser root = new Phaser();
198     Phaser child1 = new Phaser(root, 1);
199     Phaser child2 = new Phaser(root, 0);
200     assertEquals(0, child2.register());
201     assertEquals(0, child2.arriveAndDeregister());
202     assertEquals(0, child2.register());
203     assertEquals(0, child2.arriveAndDeregister());
204     assertState(child2, 0, 0, 0);
205     }
206    
207     /**
208 dl 1.1 * Invoking bulkRegister with a negative parameter throws an
209 jsr166 1.4 * IllegalArgumentException
210 dl 1.1 */
211     public void testBulkRegister1() {
212     try {
213     new Phaser().bulkRegister(-1);
214     shouldThrow();
215 jsr166 1.8 } catch (IllegalArgumentException success) {}
216 dl 1.1 }
217    
218     /**
219 jsr166 1.34 * bulkRegister should correctly record the number of unarrived
220     * parties with the number of parties being registered
221 dl 1.1 */
222     public void testBulkRegister2() {
223     Phaser phaser = new Phaser();
224 jsr166 1.30 assertEquals(0, phaser.bulkRegister(0));
225     assertState(phaser, 0, 0, 0);
226 jsr166 1.19 assertEquals(0, phaser.bulkRegister(20));
227     assertState(phaser, 0, 20, 20);
228 dl 1.1 }
229    
230     /**
231 jsr166 1.4 * Registering with a number of parties greater than or equal to 1<<16
232     * throws IllegalStateException.
233 dl 1.1 */
234     public void testBulkRegister3() {
235 jsr166 1.19 assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1));
236    
237 dl 1.1 try {
238     new Phaser().bulkRegister(1 << 16);
239     shouldThrow();
240 jsr166 1.8 } catch (IllegalStateException success) {}
241 jsr166 1.19
242     try {
243     new Phaser(2).bulkRegister((1 << 16) - 2);
244     shouldThrow();
245     } catch (IllegalStateException success) {}
246 dl 1.1 }
247    
248     /**
249     * the phase number increments correctly when tripping the barrier
250     */
251     public void testPhaseIncrement1() {
252     for (int size = 1; size < nine; size++) {
253     final Phaser phaser = new Phaser(size);
254     for (int index = 0; index <= (1 << size); index++) {
255     int phase = phaser.arrive();
256     assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0);
257     }
258     }
259     }
260    
261     /**
262 jsr166 1.19 * arrive() on a registered phaser increments phase.
263 dl 1.1 */
264     public void testArrive1() {
265     Phaser phaser = new Phaser(1);
266 jsr166 1.19 assertState(phaser, 0, 1, 1);
267     assertEquals(0, phaser.arrive());
268     assertState(phaser, 1, 1, 1);
269     }
270    
271     /**
272     * arriveAndDeregister does not wait for others to arrive at barrier
273     */
274 jsr166 1.33 public void testArriveAndDeregister() {
275 jsr166 1.19 final Phaser phaser = new Phaser(1);
276     for (int i = 0; i < 10; i++) {
277     assertState(phaser, 0, 1, 1);
278     assertEquals(0, phaser.register());
279     assertState(phaser, 0, 2, 2);
280     assertEquals(0, phaser.arriveAndDeregister());
281     assertState(phaser, 0, 1, 1);
282     }
283     assertEquals(0, phaser.arriveAndDeregister());
284 jsr166 1.25 assertTerminated(phaser, 1);
285 dl 1.1 }
286    
287     /**
288 jsr166 1.7 * arriveAndDeregister does not wait for others to arrive at barrier
289 dl 1.1 */
290 jsr166 1.33 public void testArrive2() {
291 jsr166 1.19 final Phaser phaser = new Phaser();
292     assertEquals(0, phaser.register());
293 jsr166 1.7 List<Thread> threads = new ArrayList<Thread>();
294 jsr166 1.19 for (int i = 0; i < 10; i++) {
295     assertEquals(0, phaser.register());
296 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
297 jsr166 1.33 public void realRun() {
298 jsr166 1.19 assertEquals(0, phaser.arriveAndDeregister());
299 jsr166 1.7 }}));
300 jsr166 1.19 }
301 dl 1.1
302 jsr166 1.7 for (Thread thread : threads)
303 jsr166 1.33 awaitTermination(thread);
304 jsr166 1.19 assertState(phaser, 0, 1, 1);
305     assertEquals(0, phaser.arrive());
306     assertState(phaser, 1, 1, 1);
307 dl 1.1 }
308    
309     /**
310 jsr166 1.3 * arrive() returns a negative number if the Phaser is terminated
311 dl 1.1 */
312     public void testArrive3() {
313     Phaser phaser = new Phaser(1);
314     phaser.forceTermination();
315 jsr166 1.28 assertTerminated(phaser, 0, 1);
316 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
317 dl 1.1 assertTrue(phaser.arrive() < 0);
318 jsr166 1.19 assertTrue(phaser.register() < 0);
319     assertTrue(phaser.arriveAndDeregister() < 0);
320     assertTrue(phaser.awaitAdvance(1) < 0);
321     assertTrue(phaser.getPhase() < 0);
322 dl 1.1 }
323    
324     /**
325     * arriveAndDeregister() throws IllegalStateException if number of
326 jsr166 1.3 * registered or unarrived parties would become negative
327 dl 1.1 */
328     public void testArriveAndDeregister1() {
329     try {
330     Phaser phaser = new Phaser();
331     phaser.arriveAndDeregister();
332     shouldThrow();
333 jsr166 1.8 } catch (IllegalStateException success) {}
334 dl 1.1 }
335    
336     /**
337 jsr166 1.19 * arriveAndDeregister reduces the number of arrived parties
338 dl 1.1 */
339 jsr166 1.19 public void testArriveAndDeregister2() {
340 dl 1.1 final Phaser phaser = new Phaser(1);
341 jsr166 1.19 assertEquals(0, phaser.register());
342     assertEquals(0, phaser.arrive());
343     assertState(phaser, 0, 2, 1);
344     assertEquals(0, phaser.arriveAndDeregister());
345     assertState(phaser, 1, 1, 1);
346 dl 1.1 }
347    
348     /**
349 jsr166 1.19 * arriveAndDeregister arrives at the barrier on a phaser with a parent and
350 dl 1.1 * when a deregistration occurs and causes the phaser to have zero parties
351     * its parent will be deregistered as well
352     */
353 jsr166 1.19 public void testArriveAndDeregister3() {
354 dl 1.1 Phaser parent = new Phaser();
355 jsr166 1.19 Phaser child = new Phaser(parent);
356     assertState(child, 0, 0, 0);
357 dl 1.23 assertState(parent, 0, 0, 0);
358 jsr166 1.19 assertEquals(0, child.register());
359     assertState(child, 0, 1, 1);
360     assertState(parent, 0, 1, 1);
361     assertEquals(0, child.arriveAndDeregister());
362 jsr166 1.25 assertTerminated(child, 1);
363     assertTerminated(parent, 1);
364 dl 1.1 }
365    
366     /**
367     * arriveAndDeregister deregisters one party from its parent when
368 jsr166 1.19 * the number of parties of child is zero after deregistration
369 dl 1.1 */
370 jsr166 1.19 public void testArriveAndDeregister4() {
371 dl 1.1 Phaser parent = new Phaser();
372 jsr166 1.19 Phaser child = new Phaser(parent);
373     assertEquals(0, parent.register());
374     assertEquals(0, child.register());
375     assertState(child, 0, 1, 1);
376     assertState(parent, 0, 2, 2);
377     assertEquals(0, child.arriveAndDeregister());
378     assertState(child, 0, 0, 0);
379     assertState(parent, 0, 1, 1);
380 dl 1.1 }
381    
382     /**
383     * arriveAndDeregister deregisters one party from its parent when
384     * the number of parties of root is nonzero after deregistration.
385     */
386     public void testArriveAndDeregister5() {
387 jsr166 1.19 Phaser root = new Phaser();
388     Phaser parent = new Phaser(root);
389 dl 1.1 Phaser child = new Phaser(parent);
390 dl 1.23 assertState(root, 0, 0, 0);
391     assertState(parent, 0, 0, 0);
392 jsr166 1.19 assertState(child, 0, 0, 0);
393     assertEquals(0, child.register());
394     assertState(root, 0, 1, 1);
395     assertState(parent, 0, 1, 1);
396     assertState(child, 0, 1, 1);
397     assertEquals(0, child.arriveAndDeregister());
398 jsr166 1.25 assertTerminated(child, 1);
399     assertTerminated(parent, 1);
400     assertTerminated(root, 1);
401 dl 1.1 }
402    
403     /**
404     * arriveAndDeregister returns the phase in which it leaves the
405     * phaser in after deregistration
406     */
407 jsr166 1.33 public void testArriveAndDeregister6() {
408 dl 1.1 final Phaser phaser = new Phaser(2);
409 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
410 jsr166 1.9 public void realRun() {
411 jsr166 1.19 assertEquals(0, phaser.arrive());
412 jsr166 1.7 }});
413 jsr166 1.19 assertEquals(1, phaser.arriveAndAwaitAdvance());
414     assertState(phaser, 1, 2, 2);
415     assertEquals(1, phaser.arriveAndDeregister());
416     assertState(phaser, 1, 1, 1);
417     assertEquals(1, phaser.arriveAndDeregister());
418 jsr166 1.25 assertTerminated(phaser, 2);
419 jsr166 1.33 awaitTermination(t);
420 dl 1.1 }
421    
422     /**
423     * awaitAdvance succeeds upon advance
424     */
425     public void testAwaitAdvance1() {
426     final Phaser phaser = new Phaser(1);
427 jsr166 1.19 assertEquals(0, phaser.arrive());
428     assertEquals(1, phaser.awaitAdvance(0));
429 dl 1.1 }
430    
431     /**
432     * awaitAdvance with a negative parameter will return without affecting the
433     * phaser
434     */
435     public void testAwaitAdvance2() {
436 jsr166 1.6 Phaser phaser = new Phaser();
437 jsr166 1.19 assertTrue(phaser.awaitAdvance(-1) < 0);
438     assertState(phaser, 0, 0, 0);
439 dl 1.1 }
440    
441     /**
442 jsr166 1.33 * awaitAdvanceInterruptibly blocks interruptibly
443     */
444     public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
445     final Phaser phaser = new Phaser(1);
446     final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
447    
448     Thread t1 = newStartedThread(new CheckedRunnable() {
449     public void realRun() {
450     Thread.currentThread().interrupt();
451     try {
452     phaser.awaitAdvanceInterruptibly(0);
453     shouldThrow();
454     } catch (InterruptedException success) {}
455     assertFalse(Thread.interrupted());
456    
457     pleaseInterrupt.countDown();
458     try {
459     phaser.awaitAdvanceInterruptibly(0);
460     shouldThrow();
461     } catch (InterruptedException success) {}
462     assertFalse(Thread.interrupted());
463     }});
464    
465     Thread t2 = newStartedThread(new CheckedRunnable() {
466     public void realRun() throws TimeoutException {
467     Thread.currentThread().interrupt();
468     try {
469     phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
470     shouldThrow();
471     } catch (InterruptedException success) {}
472     assertFalse(Thread.interrupted());
473    
474     pleaseInterrupt.countDown();
475     try {
476     phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
477     shouldThrow();
478     } catch (InterruptedException success) {}
479     assertFalse(Thread.interrupted());
480     }});
481    
482     await(pleaseInterrupt);
483     assertState(phaser, 0, 1, 1);
484     assertThreadsStayAlive(t1, t2);
485     t1.interrupt();
486     t2.interrupt();
487     awaitTermination(t1);
488     awaitTermination(t2);
489     assertState(phaser, 0, 1, 1);
490     assertEquals(0, phaser.arrive());
491     assertState(phaser, 1, 1, 1);
492     }
493    
494     /**
495 jsr166 1.20 * awaitAdvance continues waiting if interrupted before waiting
496 dl 1.1 */
497 jsr166 1.33 public void testAwaitAdvanceAfterInterrupt() {
498 dl 1.1 final Phaser phaser = new Phaser();
499 jsr166 1.19 assertEquals(0, phaser.register());
500 jsr166 1.33 final CountDownLatch pleaseArrive = new CountDownLatch(1);
501 dl 1.1
502 jsr166 1.7 Thread t = newStartedThread(new CheckedRunnable() {
503 jsr166 1.33 public void realRun() {
504 jsr166 1.20 Thread.currentThread().interrupt();
505 jsr166 1.19 assertEquals(0, phaser.register());
506 jsr166 1.20 assertEquals(0, phaser.arrive());
507 jsr166 1.33 pleaseArrive.countDown();
508 jsr166 1.20 assertTrue(Thread.currentThread().isInterrupted());
509     assertEquals(1, phaser.awaitAdvance(0));
510 jsr166 1.33 assertTrue(Thread.interrupted());
511 jsr166 1.5 }});
512 jsr166 1.20
513 jsr166 1.33 await(pleaseArrive);
514     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
515 jsr166 1.20 assertEquals(0, phaser.arrive());
516 jsr166 1.33 awaitTermination(t);
517 jsr166 1.20
518     Thread.currentThread().interrupt();
519     assertEquals(1, phaser.awaitAdvance(0));
520     assertTrue(Thread.interrupted());
521     }
522    
523     /**
524 jsr166 1.33 * awaitAdvance continues waiting if interrupted while waiting
525 jsr166 1.20 */
526 jsr166 1.33 public void testAwaitAdvanceBeforeInterrupt() {
527 jsr166 1.20 final Phaser phaser = new Phaser();
528     assertEquals(0, phaser.register());
529 jsr166 1.33 final CountDownLatch pleaseArrive = new CountDownLatch(1);
530 jsr166 1.20
531     Thread t = newStartedThread(new CheckedRunnable() {
532 jsr166 1.33 public void realRun() {
533 jsr166 1.20 assertEquals(0, phaser.register());
534     assertEquals(0, phaser.arrive());
535     assertFalse(Thread.currentThread().isInterrupted());
536 jsr166 1.33 pleaseArrive.countDown();
537 jsr166 1.20 assertEquals(1, phaser.awaitAdvance(0));
538 jsr166 1.33 assertTrue(Thread.interrupted());
539 jsr166 1.20 }});
540    
541 jsr166 1.33 await(pleaseArrive);
542     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
543 jsr166 1.7 t.interrupt();
544 jsr166 1.19 assertEquals(0, phaser.arrive());
545 jsr166 1.33 awaitTermination(t);
546 jsr166 1.20
547     Thread.currentThread().interrupt();
548     assertEquals(1, phaser.awaitAdvance(0));
549     assertTrue(Thread.interrupted());
550     }
551    
552     /**
553     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
554     */
555 jsr166 1.33 public void testArriveAndAwaitAdvanceAfterInterrupt() {
556 jsr166 1.20 final Phaser phaser = new Phaser();
557     assertEquals(0, phaser.register());
558 jsr166 1.33 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
559 jsr166 1.20
560     Thread t = newStartedThread(new CheckedRunnable() {
561 jsr166 1.33 public void realRun() {
562 jsr166 1.20 Thread.currentThread().interrupt();
563     assertEquals(0, phaser.register());
564 jsr166 1.33 pleaseInterrupt.countDown();
565 jsr166 1.20 assertTrue(Thread.currentThread().isInterrupted());
566     assertEquals(1, phaser.arriveAndAwaitAdvance());
567     assertTrue(Thread.currentThread().isInterrupted());
568     }});
569    
570 jsr166 1.33 await(pleaseInterrupt);
571     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
572 jsr166 1.20 Thread.currentThread().interrupt();
573     assertEquals(1, phaser.arriveAndAwaitAdvance());
574     assertTrue(Thread.interrupted());
575 jsr166 1.33 awaitTermination(t);
576 jsr166 1.20 }
577    
578     /**
579     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
580     */
581 jsr166 1.33 public void testArriveAndAwaitAdvanceBeforeInterrupt() {
582 jsr166 1.20 final Phaser phaser = new Phaser();
583     assertEquals(0, phaser.register());
584 jsr166 1.33 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
585 jsr166 1.20
586     Thread t = newStartedThread(new CheckedRunnable() {
587 jsr166 1.33 public void realRun() {
588 jsr166 1.20 assertEquals(0, phaser.register());
589     assertFalse(Thread.currentThread().isInterrupted());
590 jsr166 1.33 pleaseInterrupt.countDown();
591 jsr166 1.20 assertEquals(1, phaser.arriveAndAwaitAdvance());
592     assertTrue(Thread.currentThread().isInterrupted());
593     }});
594    
595 jsr166 1.33 await(pleaseInterrupt);
596     waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
597 jsr166 1.20 t.interrupt();
598     Thread.currentThread().interrupt();
599     assertEquals(1, phaser.arriveAndAwaitAdvance());
600     assertTrue(Thread.interrupted());
601 jsr166 1.33 awaitTermination(t);
602 dl 1.1 }
603    
604     /**
605     * awaitAdvance atomically waits for all parties within the same phase to
606     * complete before continuing
607     */
608 jsr166 1.33 public void testAwaitAdvance4() {
609 jsr166 1.5 final Phaser phaser = new Phaser(4);
610 jsr166 1.19 final AtomicInteger count = new AtomicInteger(0);
611 jsr166 1.5 List<Thread> threads = new ArrayList<Thread>();
612 jsr166 1.19 for (int i = 0; i < 4; i++)
613 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
614 jsr166 1.9 public void realRun() {
615 jsr166 1.19 for (int k = 0; k < 3; k++) {
616     assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
617     count.incrementAndGet();
618     assertEquals(2*k+1, phaser.arrive());
619     assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
620     assertEquals(count.get(), 4*(k+1));
621     }}}));
622    
623 jsr166 1.5 for (Thread thread : threads)
624 jsr166 1.33 awaitTermination(thread);
625 dl 1.1 }
626    
627     /**
628     * awaitAdvance returns the current phase
629     */
630 jsr166 1.33 public void testAwaitAdvance5() {
631 dl 1.1 final Phaser phaser = new Phaser(1);
632 jsr166 1.19 assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
633     assertEquals(1, phaser.getPhase());
634     assertEquals(1, phaser.register());
635 jsr166 1.7 List<Thread> threads = new ArrayList<Thread>();
636     for (int i = 0; i < 8; i++) {
637 jsr166 1.18 final CountDownLatch latch = new CountDownLatch(1);
638     final boolean goesFirst = ((i & 1) == 0);
639 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
640 jsr166 1.33 public void realRun() {
641 jsr166 1.18 if (goesFirst)
642     latch.countDown();
643     else
644 jsr166 1.33 await(latch);
645 dl 1.1 phaser.arrive();
646 jsr166 1.7 }}));
647 jsr166 1.18 if (goesFirst)
648 jsr166 1.33 await(latch);
649 jsr166 1.18 else
650     latch.countDown();
651 jsr166 1.19 assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
652     assertEquals(i + 2, phaser.getPhase());
653 dl 1.1 }
654 jsr166 1.7 for (Thread thread : threads)
655 jsr166 1.33 awaitTermination(thread);
656 dl 1.1 }
657    
658     /**
659 jsr166 1.29 * awaitAdvance returns the current phase in child phasers
660     */
661     public void testAwaitAdvanceTieredPhaser() throws Exception {
662     final Phaser parent = new Phaser();
663     final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3);
664     final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3);
665     for (int i = 0; i < 3; i++) {
666     zeroPartyChildren.add(new Phaser(parent, 0));
667     onePartyChildren.add(new Phaser(parent, 1));
668     }
669     final List<Phaser> phasers = new ArrayList<Phaser>();
670     phasers.addAll(zeroPartyChildren);
671     phasers.addAll(onePartyChildren);
672     phasers.add(parent);
673     for (Phaser phaser : phasers) {
674     assertEquals(-42, phaser.awaitAdvance(-42));
675     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
676     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
677     }
678    
679     for (Phaser child : onePartyChildren)
680     assertEquals(0, child.arrive());
681     for (Phaser phaser : phasers) {
682     assertEquals(-42, phaser.awaitAdvance(-42));
683     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
684     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
685     assertEquals(1, phaser.awaitAdvance(0));
686     assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
687     assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
688     }
689    
690     for (Phaser child : onePartyChildren)
691     assertEquals(1, child.arrive());
692     for (Phaser phaser : phasers) {
693     assertEquals(-42, phaser.awaitAdvance(-42));
694     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
695     assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
696     assertEquals(2, phaser.awaitAdvance(0));
697     assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
698     assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
699     assertEquals(2, phaser.awaitAdvance(1));
700     assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
701     assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
702     }
703     }
704    
705     /**
706 dl 1.1 * awaitAdvance returns when the phaser is externally terminated
707     */
708 jsr166 1.33 public void testAwaitAdvance6() {
709 dl 1.1 final Phaser phaser = new Phaser(3);
710 jsr166 1.33 final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
711 jsr166 1.18 final List<Thread> threads = new ArrayList<Thread>();
712     for (int i = 0; i < 2; i++) {
713     Runnable r = new CheckedRunnable() {
714     public void realRun() {
715 jsr166 1.19 assertEquals(0, phaser.arrive());
716 jsr166 1.33 pleaseForceTermination.countDown();
717 jsr166 1.19 assertTrue(phaser.awaitAdvance(0) < 0);
718 jsr166 1.18 assertTrue(phaser.isTerminated());
719 jsr166 1.19 assertTrue(phaser.getPhase() < 0);
720 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
721 jsr166 1.19 assertEquals(3, phaser.getRegisteredParties());
722 jsr166 1.18 }};
723     threads.add(newStartedThread(r));
724     }
725 jsr166 1.33 await(pleaseForceTermination);
726 dl 1.1 phaser.forceTermination();
727 jsr166 1.27 assertTrue(phaser.isTerminated());
728 dl 1.24 assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
729 jsr166 1.18 for (Thread thread : threads)
730 jsr166 1.33 awaitTermination(thread);
731 jsr166 1.19 assertEquals(3, phaser.getRegisteredParties());
732 dl 1.1 }
733    
734     /**
735     * arriveAndAwaitAdvance throws IllegalStateException with no
736     * unarrived parties
737     */
738     public void testArriveAndAwaitAdvance1() {
739     try {
740     Phaser phaser = new Phaser();
741     phaser.arriveAndAwaitAdvance();
742     shouldThrow();
743 jsr166 1.8 } catch (IllegalStateException success) {}
744 dl 1.1 }
745    
746     /**
747     * arriveAndAwaitAdvance waits for all threads to arrive, the
748     * number of arrived parties is the same number that is accounted
749     * for when the main thread awaitsAdvance
750     */
751 jsr166 1.33 public void testArriveAndAwaitAdvance3() {
752 dl 1.1 final Phaser phaser = new Phaser(1);
753 jsr166 1.19 final int THREADS = 3;
754 jsr166 1.33 final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
755 jsr166 1.7 final List<Thread> threads = new ArrayList<Thread>();
756 jsr166 1.19 for (int i = 0; i < THREADS; i++)
757 jsr166 1.7 threads.add(newStartedThread(new CheckedRunnable() {
758 jsr166 1.33 public void realRun() {
759 jsr166 1.19 assertEquals(0, phaser.register());
760 jsr166 1.33 pleaseArrive.countDown();
761 jsr166 1.19 assertEquals(1, phaser.arriveAndAwaitAdvance());
762     }}));
763    
764 jsr166 1.33 await(pleaseArrive);
765     long startTime = System.nanoTime();
766 jsr166 1.19 while (phaser.getArrivedParties() < THREADS)
767     Thread.yield();
768     assertEquals(THREADS, phaser.getArrivedParties());
769 jsr166 1.33 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
770     for (Thread thread : threads)
771     waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS);
772 jsr166 1.19 for (Thread thread : threads)
773     assertTrue(thread.isAlive());
774     assertState(phaser, 0, THREADS + 1, 1);
775 dl 1.12 phaser.arriveAndAwaitAdvance();
776 jsr166 1.7 for (Thread thread : threads)
777 jsr166 1.33 awaitTermination(thread);
778 jsr166 1.19 assertState(phaser, 1, THREADS + 1, THREADS + 1);
779 dl 1.1 }
780    
781     }