ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.33
Committed: Wed Jun 22 07:46:57 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.32: +113 -59 lines
Log Message:
various test improvements

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