ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
Revision: 1.25
Committed: Tue Nov 30 01:56:12 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.24: +17 -18 lines
Log Message:
more assertions, now that we have TERMINATION_BIT

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