ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.48
Committed: Sun Aug 11 22:29:27 2019 UTC (4 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.47: +6 -2 lines
Log Message:
more assertions; more interleavings

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