ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PhaserTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PhaserTest.java (file contents):
Revision 1.3 by jsr166, Sat Aug 1 21:56:02 2009 UTC vs.
Revision 1.20 by jsr166, Thu Oct 21 23:28:13 2010 UTC

# Line 8 | Line 8
8   import java.util.ArrayList;
9   import java.util.List;
10   import java.util.concurrent.atomic.AtomicInteger;
11 + import java.util.concurrent.atomic.AtomicBoolean;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
15   import junit.framework.Test;
16   import junit.framework.TestSuite;
17  
# Line 22 | Line 25 | public class PhaserTest extends JSR166Te
25          return new TestSuite(PhaserTest.class);
26      }
27  
28 +    /** Checks state of phaser. */
29 +    protected void assertState(Phaser phaser,
30 +                               int phase, int parties, int unarrived) {
31 +        assertEquals(phase, phaser.getPhase());
32 +        assertEquals(parties, phaser.getRegisteredParties());
33 +        assertEquals(unarrived, phaser.getUnarrivedParties());
34 +        assertEquals(parties - unarrived, phaser.getArrivedParties());
35 +        assertTrue((phaser.getPhase() >= 0) ^ phaser.isTerminated());
36 +    }
37 +
38 +    /** Checks state of terminated phaser. */
39 +    protected void assertTerminated(Phaser phaser, int parties, int unarrived) {
40 +        assertTrue(phaser.isTerminated());
41 +        assertTrue(phaser.getPhase() < 0);
42 +        assertEquals(parties, phaser.getRegisteredParties());
43 +        assertEquals(unarrived, phaser.getUnarrivedParties());
44 +        assertEquals(parties - unarrived, phaser.getArrivedParties());
45 +    }
46 +
47 +    protected void assertTerminated(Phaser phaser) {
48 +        assertTerminated(phaser, 0, 0);
49 +    }
50 +
51      /**
52       * Empty constructor builds a new Phaser with no parent, no registered
53       * parties and initial phase number of 0
# Line 40 | Line 66 | public class PhaserTest extends JSR166Te
66      public void testConstructor2() {
67          try {
68              new Phaser(-1);
69 <            this.shouldThrow();
70 <        } catch (IllegalArgumentException success) {
45 <        }
69 >            shouldThrow();
70 >        } catch (IllegalArgumentException success) {}
71      }
72  
73      /**
# Line 62 | Line 87 | public class PhaserTest extends JSR166Te
87          try {
88              new Phaser(new Phaser(), -1);
89              shouldThrow();
90 <        } catch (IllegalArgumentException success) {
66 <        }
90 >        } catch (IllegalArgumentException success) {}
91      }
92  
93      /**
# Line 81 | Line 105 | public class PhaserTest extends JSR166Te
105       */
106      public void testRegister1() {
107          Phaser phaser = new Phaser();
108 <        assertEquals(0, phaser.getUnarrivedParties());
109 <        phaser.register();
110 <        assertEquals(1, phaser.getUnarrivedParties());
87 <        assertEquals(0, phaser.getArrivedParties());
108 >        assertState(phaser, 0, 0, 0);
109 >        assertEquals(0, phaser.register());
110 >        assertState(phaser, 0, 1, 1);
111      }
112  
113      /**
114 <     * Registering any more then 65536 parties causes IllegalStateExceptiom
114 >     * Registering more than 65536 parties causes IllegalStateException
115       */
116      public void testRegister2() {
117          Phaser phaser = new Phaser(0);
118 <        int expectedUnnarivedParties = (1 << 16) - 1;
119 <        for (int i = 0; i < expectedUnnarivedParties; i++) {
120 <            phaser.register();
121 <            assertEquals(i + 1, phaser.getUnarrivedParties());
118 >        int maxParties = (1 << 16) - 1;
119 >        assertState(phaser, 0, 0, 0);
120 >        assertEquals(0, phaser.bulkRegister(maxParties - 10));
121 >        assertState(phaser, 0, maxParties - 10, maxParties - 10);
122 >        for (int i = 0; i < 10; i++) {
123 >            assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i);
124 >            assertEquals(0, phaser.register());
125          }
126 +        assertState(phaser, 0, maxParties, maxParties);
127          try {
128              phaser.register();
129              shouldThrow();
130 <        } catch (IllegalStateException success) {
104 <        } catch (Exception ex) {
105 <            threadUnexpectedException(ex);
106 <        }
130 >        } catch (IllegalStateException success) {}
131      }
132  
133      /**
# Line 113 | Line 137 | public class PhaserTest extends JSR166Te
137      public void testRegister3() {
138          Phaser phaser = new Phaser();
139          assertEquals(0, phaser.register());
140 <        phaser.arrive();
140 >        assertEquals(0, phaser.arrive());
141          assertEquals(1, phaser.register());
142 +        assertState(phaser, 1, 2, 2);
143      }
144  
145      /**
# Line 123 | Line 148 | public class PhaserTest extends JSR166Te
148       */
149      public void testRegister4() {
150          Phaser phaser = new Phaser(1);
151 <        phaser.arrive();
152 <        int expectedPhase = phaser.register();
153 <        phaser.arrive();
154 <        assertEquals(expectedPhase, phaser.getPhase());
130 <    }
131 <
132 <    public void testRegister5() {
133 <        Phaser phaser = new Phaser();
134 <        phaser.register();
135 <        assertEquals(1, phaser.getUnarrivedParties());
151 >        assertEquals(0, phaser.arrive());
152 >        assertEquals(1, phaser.register());
153 >        assertEquals(1, phaser.arrive());
154 >        assertState(phaser, 1, 2, 1);
155      }
156  
157      /**
158       * Invoking bulkRegister with a negative parameter throws an
159 <     * IllegalArgumentExceptiom
159 >     * IllegalArgumentException
160       */
161      public void testBulkRegister1() {
162          try {
163              new Phaser().bulkRegister(-1);
164              shouldThrow();
165 <        } catch (IllegalArgumentException success) {
147 <        }
165 >        } catch (IllegalArgumentException success) {}
166      }
167  
168      /**
# Line 153 | Line 171 | public class PhaserTest extends JSR166Te
171       */
172      public void testBulkRegister2() {
173          Phaser phaser = new Phaser();
174 <        phaser.bulkRegister(20);
175 <        assertEquals(20, phaser.getUnarrivedParties());
174 >        assertEquals(0, phaser.bulkRegister(20));
175 >        assertState(phaser, 0, 20, 20);
176      }
177  
178      /**
179 <     * Registering with a number of parties greater then or equal to 1<<16
180 <     * throws IllegalStateExceptiom.
179 >     * Registering with a number of parties greater than or equal to 1<<16
180 >     * throws IllegalStateException.
181       */
182      public void testBulkRegister3() {
183 +        assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1));
184 +
185          try {
186              new Phaser().bulkRegister(1 << 16);
187              shouldThrow();
188 <        } catch (IllegalStateException success) {
189 <        }
188 >        } catch (IllegalStateException success) {}
189 >
190 >        try {
191 >            new Phaser(2).bulkRegister((1 << 16) - 2);
192 >            shouldThrow();
193 >        } catch (IllegalStateException success) {}
194      }
195  
196      /**
# Line 183 | Line 207 | public class PhaserTest extends JSR166Te
207      }
208  
209      /**
210 <     *  Arrive() on a registered phaser increments phase.
210 >     * arrive() on a registered phaser increments phase.
211       */
212      public void testArrive1() {
213          Phaser phaser = new Phaser(1);
214 <        phaser.arrive();
215 <        assertEquals(1, phaser.getPhase());
214 >        assertState(phaser, 0, 1, 1);
215 >        assertEquals(0, phaser.arrive());
216 >        assertState(phaser, 1, 1, 1);
217      }
218  
219      /**
220 <     * arrive does not wait for others to arrive at barrier
220 >     * arriveAndDeregister does not wait for others to arrive at barrier
221       */
222 <    public void testArrive2() {
222 >    public void testArriveAndDeregister() throws InterruptedException {
223          final Phaser phaser = new Phaser(1);
224 <        phaser.register();
225 <        Thread thread = null;
226 <        for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) {
227 <            phaser.register();
228 <            thread = new Thread() {
224 >        for (int i = 0; i < 10; i++) {
225 >            assertState(phaser, 0, 1, 1);
226 >            assertEquals(0, phaser.register());
227 >            assertState(phaser, 0, 2, 2);
228 >            assertEquals(0, phaser.arriveAndDeregister());
229 >            assertState(phaser, 0, 1, 1);
230 >        }
231 >        assertEquals(0, phaser.arriveAndDeregister());
232 >        assertTerminated(phaser);
233 >    }
234  
235 <                public void run() {
236 <                    r.run();
237 <                    phaser.arriveAndDeregister();
238 <                }
239 <            };
240 <            thread.start();
235 >    /**
236 >     * arriveAndDeregister does not wait for others to arrive at barrier
237 >     */
238 >    public void testArrive2() throws InterruptedException {
239 >        final Phaser phaser = new Phaser();
240 >        assertEquals(0, phaser.register());
241 >        List<Thread> threads = new ArrayList<Thread>();
242 >        for (int i = 0; i < 10; i++) {
243 >            assertEquals(0, phaser.register());
244 >            threads.add(newStartedThread(new CheckedRunnable() {
245 >                public void realRun() throws InterruptedException {
246 >                    assertEquals(0, phaser.arriveAndDeregister());
247 >                }}));
248          }
249  
250 <        phaser.arrive();
251 <        assertTrue(thread.isAlive());
252 <        assertFalse(phaser.isTerminated());
250 >        for (Thread thread : threads)
251 >            awaitTermination(thread, LONG_DELAY_MS);
252 >        assertState(phaser, 0, 1, 1);
253 >        assertEquals(0, phaser.arrive());
254 >        assertState(phaser, 1, 1, 1);
255      }
256  
257      /**
# Line 221 | Line 260 | public class PhaserTest extends JSR166Te
260      public void testArrive3() {
261          Phaser phaser = new Phaser(1);
262          phaser.forceTermination();
263 +        assertTerminated(phaser, 1, 1);
264          assertTrue(phaser.arrive() < 0);
265 <
265 >        assertTrue(phaser.register() < 0);
266 >        assertTrue(phaser.arriveAndDeregister() < 0);
267 >        assertTrue(phaser.awaitAdvance(1) < 0);
268 >        assertTrue(phaser.getPhase() < 0);
269      }
270  
271      /**
# Line 234 | Line 277 | public class PhaserTest extends JSR166Te
277              Phaser phaser = new Phaser();
278              phaser.arriveAndDeregister();
279              shouldThrow();
280 <
238 <        } catch (IllegalStateException success) {
239 <        }
280 >        } catch (IllegalStateException success) {}
281      }
282  
283      /**
284 <     * arriveAndDeregister deregisters reduces the number of arrived parties
284 >     * arriveAndDeregister reduces the number of arrived parties
285       */
286 <    public void testArriveAndDergeister2() {
286 >    public void testArriveAndDeregister2() {
287          final Phaser phaser = new Phaser(1);
288 <        phaser.register();
289 <        phaser.arrive();
290 <        int p = phaser.getArrivedParties();
291 <        assertTrue(p == 1);
292 <        phaser.arriveAndDeregister();
252 <        assertTrue(phaser.getArrivedParties() < p);
288 >        assertEquals(0, phaser.register());
289 >        assertEquals(0, phaser.arrive());
290 >        assertState(phaser, 0, 2, 1);
291 >        assertEquals(0, phaser.arriveAndDeregister());
292 >        assertState(phaser, 1, 1, 1);
293      }
294  
295      /**
296 <     * arriveAndDeregister arrives to the barrier on a phaser with a parent and
296 >     * arriveAndDeregister arrives at the barrier on a phaser with a parent and
297       * when a deregistration occurs and causes the phaser to have zero parties
298       * its parent will be deregistered as well
299       */
300 <    public void testArriveAndDeregsiter3() {
300 >    public void testArriveAndDeregister3() {
301          Phaser parent = new Phaser();
302 <        Phaser root = new Phaser(parent);
303 <        root.register();
304 <        assertTrue(parent.getUnarrivedParties() > 0);
305 <        assertTrue(root.getUnarrivedParties() > 0);
306 <        root.arriveAndDeregister();
307 <        assertTrue(parent.getUnarrivedParties() == 0);
308 <        assertTrue(root.getUnarrivedParties() == 0);
309 <        assertTrue(root.isTerminated() && parent.isTerminated());
302 >        Phaser child = new Phaser(parent);
303 >        assertState(child, 0, 0, 0);
304 >        assertState(parent, 0, 1, 1);
305 >        assertEquals(0, child.register());
306 >        assertState(child, 0, 1, 1);
307 >        assertState(parent, 0, 1, 1);
308 >        assertEquals(0, child.arriveAndDeregister());
309 >        assertTerminated(child);
310 >        assertTerminated(parent);
311      }
312  
313      /**
314       * arriveAndDeregister deregisters one party from its parent when
315 <     * the number of parties of root is zero after deregistration
315 >     * the number of parties of child is zero after deregistration
316       */
317 <    public void testArriveAndDeregsiter4() {
317 >    public void testArriveAndDeregister4() {
318          Phaser parent = new Phaser();
319 <        Phaser root = new Phaser(parent);
320 <        parent.register();
321 <        root.register();
322 <        int parentParties = parent.getUnarrivedParties();
323 <        root.arriveAndDeregister();
324 <        assertEquals(parentParties - 1, parent.getUnarrivedParties());
319 >        Phaser child = new Phaser(parent);
320 >        assertEquals(0, parent.register());
321 >        assertEquals(0, child.register());
322 >        assertState(child, 0, 1, 1);
323 >        assertState(parent, 0, 2, 2);
324 >        assertEquals(0, child.arriveAndDeregister());
325 >        assertState(child, 0, 0, 0);
326 >        assertState(parent, 0, 1, 1);
327      }
328  
329      /**
# Line 288 | Line 331 | public class PhaserTest extends JSR166Te
331       * the number of parties of root is nonzero after deregistration.
332       */
333      public void testArriveAndDeregister5() {
334 <        Phaser parent = new Phaser();
334 >        Phaser root = new Phaser();
335 >        Phaser parent = new Phaser(root);
336          Phaser child = new Phaser(parent);
337 <        Phaser root = new Phaser(child);
338 <        assertTrue(parent.getUnarrivedParties() > 0);
339 <        assertTrue(child.getUnarrivedParties() > 0);
340 <        root.register();
341 <        root.arriveAndDeregister();
342 <        assertTrue(parent.getUnarrivedParties() == 0);
343 <        assertTrue(child.getUnarrivedParties() == 0);
344 <        assertTrue(root.isTerminated());
337 >        assertState(root, 0, 1, 1);
338 >        assertState(parent, 0, 1, 1);
339 >        assertState(child, 0, 0, 0);
340 >        assertEquals(0, child.register());
341 >        assertState(root, 0, 1, 1);
342 >        assertState(parent, 0, 1, 1);
343 >        assertState(child, 0, 1, 1);
344 >        assertEquals(0, child.arriveAndDeregister());
345 >        assertTerminated(child);
346 >        assertTerminated(parent);
347 >        assertTerminated(root);
348      }
349  
350      /**
351       * arriveAndDeregister returns the phase in which it leaves the
352       * phaser in after deregistration
353       */
354 <    public void testArriveAndDeregister6() {
354 >    public void testArriveAndDeregister6() throws InterruptedException {
355          final Phaser phaser = new Phaser(2);
356 <        new Thread() {
357 <
358 <            public void run() {
359 <                getRunnable(SHORT_DELAY_MS).run();
360 <                phaser.arrive();
361 <            }
362 <        }.start();
363 <        phaser.arriveAndAwaitAdvance();
364 <        int phase = phaser.arriveAndDeregister();
365 <        assertEquals(phase, phaser.getPhase());
356 >        Thread t = newStartedThread(new CheckedRunnable() {
357 >            public void realRun() {
358 >                assertEquals(0, phaser.arrive());
359 >            }});
360 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
361 >        assertState(phaser, 1, 2, 2);
362 >        assertEquals(1, phaser.arriveAndDeregister());
363 >        assertState(phaser, 1, 1, 1);
364 >        assertEquals(1, phaser.arriveAndDeregister());
365 >        assertTerminated(phaser);
366 >        awaitTermination(t, SHORT_DELAY_MS);
367      }
368  
369      /**
# Line 323 | Line 371 | public class PhaserTest extends JSR166Te
371       */
372      public void testAwaitAdvance1() {
373          final Phaser phaser = new Phaser(1);
374 <        phaser.awaitAdvance(phaser.arrive());
374 >        assertEquals(0, phaser.arrive());
375 >        assertEquals(1, phaser.awaitAdvance(0));
376      }
377  
378      /**
# Line 331 | Line 380 | public class PhaserTest extends JSR166Te
380       * phaser
381       */
382      public void testAwaitAdvance2() {
383 <        try {
384 <            Phaser phaser = new Phaser();
385 <            phaser.awaitAdvance(-1);
337 <        } catch (Exception failure) {
338 <            this.unexpectedException();
339 <        }
383 >        Phaser phaser = new Phaser();
384 >        assertTrue(phaser.awaitAdvance(-1) < 0);
385 >        assertState(phaser, 0, 0, 0);
386      }
387  
388      /**
389 <     * awaitAdvance while waiting does not abort on interrupt.
389 >     * awaitAdvance continues waiting if interrupted before waiting
390       */
391 <    public void testAwaitAdvance3() {
391 >    public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
392          final Phaser phaser = new Phaser();
393 <        Thread th1 = new Thread() {
393 >        assertEquals(0, phaser.register());
394 >        final CountDownLatch threadStarted = new CountDownLatch(1);
395  
396 <            public void run() {
397 <                try {
398 <                    phaser.register();
399 <                    getRunnable(LONG_DELAY_MS).run();
400 <                    phaser.awaitAdvance(phaser.arrive());
401 <                } catch (Exception failure) {
402 <                    threadUnexpectedException(failure);
403 <                }
396 >        Thread t = newStartedThread(new CheckedRunnable() {
397 >            public void realRun() throws InterruptedException {
398 >                Thread.currentThread().interrupt();
399 >                assertEquals(0, phaser.register());
400 >                assertEquals(0, phaser.arrive());
401 >                threadStarted.countDown();
402 >                assertTrue(Thread.currentThread().isInterrupted());
403 >                assertEquals(1, phaser.awaitAdvance(0));
404 >                assertTrue(Thread.currentThread().isInterrupted());
405 >            }});
406  
407 <            }
408 <        };
409 <        phaser.register();
410 <        th1.start();
411 <        try {
412 <            Thread.sleep(SHORT_DELAY_MS);
413 <            th1.interrupt();
414 <            Thread.sleep(LONG_DELAY_MS);
415 <            phaser.arrive();
416 <        } catch (Exception failure) {
417 <            unexpectedException();
418 <        }
419 <        assertFalse(th1.isInterrupted());
407 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
408 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
409 >        assertEquals(0, phaser.arrive());
410 >        awaitTermination(t, SMALL_DELAY_MS);
411 >
412 >        Thread.currentThread().interrupt();
413 >        assertEquals(1, phaser.awaitAdvance(0));
414 >        assertTrue(Thread.interrupted());
415 >    }
416 >
417 >    /**
418 >     * awaitAdvance continues waiting if interrupted while waiting
419 >     */
420 >    public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
421 >        final Phaser phaser = new Phaser();
422 >        assertEquals(0, phaser.register());
423 >        final CountDownLatch threadStarted = new CountDownLatch(1);
424 >
425 >        Thread t = newStartedThread(new CheckedRunnable() {
426 >            public void realRun() throws InterruptedException {
427 >                assertEquals(0, phaser.register());
428 >                assertEquals(0, phaser.arrive());
429 >                threadStarted.countDown();
430 >                assertFalse(Thread.currentThread().isInterrupted());
431 >                assertEquals(1, phaser.awaitAdvance(0));
432 >                assertTrue(Thread.currentThread().isInterrupted());
433 >            }});
434 >
435 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
436 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
437 >        t.interrupt();
438 >        assertEquals(0, phaser.arrive());
439 >        awaitTermination(t, SMALL_DELAY_MS);
440 >
441 >        Thread.currentThread().interrupt();
442 >        assertEquals(1, phaser.awaitAdvance(0));
443 >        assertTrue(Thread.interrupted());
444 >    }
445 >
446 >    /**
447 >     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
448 >     */
449 >    public void testArriveAndAwaitAdvanceAfterInterrupt()
450 >            throws InterruptedException {
451 >        final Phaser phaser = new Phaser();
452 >        assertEquals(0, phaser.register());
453 >        final CountDownLatch threadStarted = new CountDownLatch(1);
454 >
455 >        Thread t = newStartedThread(new CheckedRunnable() {
456 >            public void realRun() throws InterruptedException {
457 >                Thread.currentThread().interrupt();
458 >                assertEquals(0, phaser.register());
459 >                threadStarted.countDown();
460 >                assertTrue(Thread.currentThread().isInterrupted());
461 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
462 >                assertTrue(Thread.currentThread().isInterrupted());
463 >            }});
464 >
465 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
466 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
467 >        Thread.currentThread().interrupt();
468 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
469 >        assertTrue(Thread.interrupted());
470 >        awaitTermination(t, SMALL_DELAY_MS);
471 >    }
472 >
473 >    /**
474 >     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
475 >     */
476 >    public void testArriveAndAwaitAdvanceBeforeInterrupt()
477 >            throws InterruptedException {
478 >        final Phaser phaser = new Phaser();
479 >        assertEquals(0, phaser.register());
480 >        final CountDownLatch threadStarted = new CountDownLatch(1);
481 >
482 >        Thread t = newStartedThread(new CheckedRunnable() {
483 >            public void realRun() throws InterruptedException {
484 >                assertEquals(0, phaser.register());
485 >                threadStarted.countDown();
486 >                assertFalse(Thread.currentThread().isInterrupted());
487 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
488 >                assertTrue(Thread.currentThread().isInterrupted());
489 >            }});
490 >
491 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
492 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
493 >        t.interrupt();
494 >        Thread.currentThread().interrupt();
495 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
496 >        assertTrue(Thread.interrupted());
497 >        awaitTermination(t, SMALL_DELAY_MS);
498      }
499  
500      /**
501       * awaitAdvance atomically waits for all parties within the same phase to
502       * complete before continuing
503       */
504 <    public void testAwaitAdvance4() {
505 <        final Phaser phaser = new Phaser(four);
506 <        final AtomicInteger phaseCount = new AtomicInteger(0);
507 <        for (int i = 0; i < four; i++) {
508 <            new Thread() {
509 <
510 <                public void run() {
511 <                    int phase = phaser.arrive();
512 <                    phaseCount.incrementAndGet();
513 <                    getRunnable(LONG_DELAY_MS).run();
514 <                    phaser.awaitAdvance(phase);
515 <                    assertTrue(phaseCount.get() == four);
516 <                }
517 <            }.start();
518 <        }
504 >    public void testAwaitAdvance4() throws InterruptedException {
505 >        final Phaser phaser = new Phaser(4);
506 >        final AtomicInteger count = new AtomicInteger(0);
507 >        List<Thread> threads = new ArrayList<Thread>();
508 >        for (int i = 0; i < 4; i++)
509 >            threads.add(newStartedThread(new CheckedRunnable() {
510 >                public void realRun() {
511 >                    for (int k = 0; k < 3; k++) {
512 >                        assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
513 >                        count.incrementAndGet();
514 >                        assertEquals(2*k+1, phaser.arrive());
515 >                        assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
516 >                        assertEquals(count.get(), 4*(k+1));
517 >                    }}}));
518 >
519 >        for (Thread thread : threads)
520 >            awaitTermination(thread, MEDIUM_DELAY_MS);
521      }
522  
523      /**
524       * awaitAdvance returns the current phase
525       */
526 <    public void testAwaitAdvance5() {
526 >    public void testAwaitAdvance5() throws InterruptedException {
527          final Phaser phaser = new Phaser(1);
528 <        int phase = phaser.awaitAdvance(phaser.arrive());
529 <        assertEquals(phase, phaser.getPhase());
530 <        phaser.register();
531 <        for (int i = 0; i < eight; i++) {
532 <            new Thread() {
533 <
534 <                public void run() {
535 <                    getRunnable(SHORT_DELAY_MS).run();
528 >        assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
529 >        assertEquals(1, phaser.getPhase());
530 >        assertEquals(1, phaser.register());
531 >        List<Thread> threads = new ArrayList<Thread>();
532 >        for (int i = 0; i < 8; i++) {
533 >            final CountDownLatch latch = new CountDownLatch(1);
534 >            final boolean goesFirst = ((i & 1) == 0);
535 >            threads.add(newStartedThread(new CheckedRunnable() {
536 >                public void realRun() throws InterruptedException {
537 >                    if (goesFirst)
538 >                        latch.countDown();
539 >                    else
540 >                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
541                      phaser.arrive();
542 <                }
543 <            }.start();
544 <            phase = phaser.awaitAdvance(phaser.arrive());
545 <            assertEquals(phase, phaser.getPhase());
542 >                }}));
543 >            if (goesFirst)
544 >                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
545 >            else
546 >                latch.countDown();
547 >            assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
548 >            assertEquals(i + 2, phaser.getPhase());
549          }
550 +        for (Thread thread : threads)
551 +            awaitTermination(thread, SMALL_DELAY_MS);
552      }
553  
554      /**
555       * awaitAdvance returns when the phaser is externally terminated
556       */
557 <    public void testAwaitAdvance6() {
557 >    public void testAwaitAdvance6() throws InterruptedException {
558          final Phaser phaser = new Phaser(3);
559 <        /*
560 <         * Start new thread. This thread waits a small amount of time
561 <         * and waits for the other two parties to arrive.  The party
562 <         * in the main thread arrives quickly so at best this thread
563 <         * waits for the second thread's party to arrive
564 <         */
565 <        new Thread() {
566 <
567 <            public void run() {
568 <                getRunnable(SMALL_DELAY_MS).run();
569 <                int phase = phaser.awaitAdvance(phaser.arrive());
570 <                /*
571 <                 * This point is reached when force termination is called in which phase = -1
572 <                 */
573 <                threadAssertTrue(phase < 0);
435 <                threadAssertTrue(phaser.isTerminated());
436 <            }
437 <        }.start();
438 <        /*
439 <         * This thread will cause the first thread run to wait, in doing so
440 <         * the main thread will force termination in which the first thread
441 <         * should exit peacefully as this one
442 <         */
443 <        new Thread() {
444 <
445 <            public void run() {
446 <                getRunnable(LONG_DELAY_MS).run();
447 <                int p1 = phaser.arrive();
448 <                int phase = phaser.awaitAdvance(p1);
449 <                threadAssertTrue(phase < 0);
450 <                threadAssertTrue(phaser.isTerminated());
451 <            }
452 <        }.start();
453 <
454 <        phaser.arrive();
559 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
560 >        final List<Thread> threads = new ArrayList<Thread>();
561 >        for (int i = 0; i < 2; i++) {
562 >            Runnable r = new CheckedRunnable() {
563 >                public void realRun() {
564 >                    assertEquals(0, phaser.arrive());
565 >                    threadsStarted.countDown();
566 >                    assertTrue(phaser.awaitAdvance(0) < 0);
567 >                    assertTrue(phaser.isTerminated());
568 >                    assertTrue(phaser.getPhase() < 0);
569 >                    assertEquals(3, phaser.getRegisteredParties());
570 >                }};
571 >            threads.add(newStartedThread(r));
572 >        }
573 >        threadsStarted.await();
574          phaser.forceTermination();
575 +        for (Thread thread : threads)
576 +            awaitTermination(thread, SMALL_DELAY_MS);
577 +        assertTrue(phaser.isTerminated());
578 +        assertTrue(phaser.getPhase() < 0);
579 +        assertEquals(3, phaser.getRegisteredParties());
580      }
581  
582      /**
# Line 464 | Line 588 | public class PhaserTest extends JSR166Te
588              Phaser phaser = new Phaser();
589              phaser.arriveAndAwaitAdvance();
590              shouldThrow();
591 <        } catch (IllegalStateException success) {
468 <        }
469 <    }
470 <
471 <    /**
472 <     * Interrupted arriveAndAwaitAdvance does not throw InterruptedException
473 <     */
474 <    public void testArriveAndAwaitAdvance2() {
475 <        final Phaser phaser = new Phaser(2);
476 <        Thread th = new Thread() {
477 <            public void run() {
478 <                try {
479 <                    phaser.arriveAndAwaitAdvance();
480 <                } catch (Exception failure) {
481 <                    threadUnexpectedException(failure);
482 <                }
483 <            }
484 <        };
485 <
486 <        try {
487 <            th.start();
488 <            Thread.sleep(LONG_DELAY_MS);
489 <            th.interrupt();
490 <            Thread.sleep(LONG_DELAY_MS);
491 <            phaser.arrive();
492 <        } catch (InterruptedException failure) {
493 <            this.unexpectedException();
494 <        }
495 <        assertFalse(th.isInterrupted());
591 >        } catch (IllegalStateException success) {}
592      }
593  
594      /**
# Line 500 | Line 596 | public class PhaserTest extends JSR166Te
596       * number of arrived parties is the same number that is accounted
597       * for when the main thread awaitsAdvance
598       */
599 <    public void testArriveAndAwaitAdvance3() {
599 >    public void testArriveAndAwaitAdvance3() throws InterruptedException {
600          final Phaser phaser = new Phaser(1);
601 <        final AtomicInteger arrivingCount = new AtomicInteger(0);
602 <        for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
603 <            new Thread() {
604 <
605 <                public void run() {
606 <                    phaser.register();
607 <                    run.run();
608 <                    arrivingCount.getAndIncrement();
609 <                    phaser.arrive();
610 <                }
611 <            }.start();
612 <        }
613 <        int phaseNumber = phaser.arriveAndAwaitAdvance();
614 <        arrivingCount.incrementAndGet();
615 <        //the + 1 adds to expectedArrive to account for the main threads arrival
616 <        int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1;
617 <        threadAssertEquals(expectedArrived, arrivingCount.get());
618 <    }
619 <    // .. initially called, for n tasks via
620 <    private List<Runnable> getRunnables(int size, long wait) {
621 <        List<Runnable> list = new ArrayList<Runnable>();
622 <        for (int i = 0; i < size; i++) {
623 <            list.add(getRunnable(wait));
624 <        }
529 <        return list;
530 <    }
531 <
532 <    private Runnable getRunnable(final long wait) {
533 <        return new Runnable() {
534 <
535 <            public void run() {
536 <                try {
537 <                    Thread.sleep(wait);
538 <                } catch (InterruptedException noop) {
539 <                // sleep interruption isn't a problem case for these example
540 <                } catch (Exception ex) {
541 <                    threadUnexpectedException(ex);
542 <                }
543 <
544 <            }
545 <        };
601 >        final int THREADS = 3;
602 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
603 >        final List<Thread> threads = new ArrayList<Thread>();
604 >        for (int i = 0; i < THREADS; i++)
605 >            threads.add(newStartedThread(new CheckedRunnable() {
606 >                public void realRun() throws InterruptedException {
607 >                    assertEquals(0, phaser.register());
608 >                    threadsStarted.countDown();
609 >                    assertEquals(1, phaser.arriveAndAwaitAdvance());
610 >                }}));
611 >
612 >        assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
613 >        long t0 = System.nanoTime();
614 >        while (phaser.getArrivedParties() < THREADS)
615 >            Thread.yield();
616 >        assertEquals(THREADS, phaser.getArrivedParties());
617 >        assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
618 >        for (Thread thread : threads)
619 >            assertTrue(thread.isAlive());
620 >        assertState(phaser, 0, THREADS + 1, 1);
621 >        phaser.arriveAndAwaitAdvance();
622 >        for (Thread thread : threads)
623 >            awaitTermination(thread, SMALL_DELAY_MS);
624 >        assertState(phaser, 1, THREADS + 1, THREADS + 1);
625      }
626  
627   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines