ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.36
Committed: Fri Nov 18 20:11:17 2011 UTC (12 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +12 -0 lines
Log Message:
testRegisterEmptySubPhaser: more assertions

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