ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.38
Committed: Wed Dec 31 16:44:02 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +0 -2 lines
Log Message:
remove unused imports

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