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.6 by jsr166, Mon Aug 3 22:06:50 2009 UTC vs.
Revision 1.29 by jsr166, Fri Dec 3 23:55:55 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 +    private static final int maxParties = 65535;
29 +
30 +    /** Checks state of unterminated 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 +        assertFalse(phaser.isTerminated());
38 +    }
39 +
40 +    /** Checks state of terminated phaser. */
41 +    protected void assertTerminated(Phaser phaser, int maxPhase, int parties) {
42 +        assertTrue(phaser.isTerminated());
43 +        int expectedPhase = maxPhase + Integer.MIN_VALUE;
44 +        assertEquals(expectedPhase, phaser.getPhase());
45 +        assertEquals(parties, phaser.getRegisteredParties());
46 +        assertEquals(expectedPhase, phaser.register());
47 +        assertEquals(expectedPhase, phaser.arrive());
48 +        assertEquals(expectedPhase, phaser.arriveAndDeregister());
49 +    }
50 +
51 +    protected void assertTerminated(Phaser phaser, int maxPhase) {
52 +        assertTerminated(phaser, maxPhase, 0);
53 +    }
54 +
55      /**
56       * Empty constructor builds a new Phaser with no parent, no registered
57       * parties and initial phase number of 0
58       */
59 <    public void testConstructor1() {
59 >    public void testConstructorDefaultValues() {
60          Phaser phaser = new Phaser();
61          assertNull(phaser.getParent());
62 +        assertEquals(0, phaser.getRegisteredParties());
63          assertEquals(0, phaser.getArrivedParties());
64 +        assertEquals(0, phaser.getUnarrivedParties());
65          assertEquals(0, phaser.getPhase());
66      }
67  
68      /**
69 <     * A negative party number for the constructor throws illegal argument
70 <     * exception
69 >     * Constructing with a negative number of parties throws
70 >     * IllegalArgumentException
71       */
72 <    public void testConstructor2() {
72 >    public void testConstructorNegativeParties() {
73          try {
74              new Phaser(-1);
75              shouldThrow();
76 <        } catch (IllegalArgumentException success) {
45 <        }
76 >        } catch (IllegalArgumentException success) {}
77      }
78  
79      /**
80 <     * The parent being input into the constructor should equal the original
81 <     * parent when being returned
80 >     * Constructing with a negative number of parties throws
81 >     * IllegalArgumentException
82       */
83 <    public void testConstructor3() {
84 <        Phaser parent = new Phaser();
85 <        assertEquals(parent, new Phaser(parent).getParent());
83 >    public void testConstructorNegativeParties2() {
84 >        try {
85 >            new Phaser(new Phaser(), -1);
86 >            shouldThrow();
87 >        } catch (IllegalArgumentException success) {}
88      }
89  
90      /**
91 <     * A negative party number for the constructor throws illegal argument
92 <     * exception
91 >     * Constructing with a number of parties > 65535 throws
92 >     * IllegalArgumentException
93       */
94 <    public void testConstructor4() {
94 >    public void testConstructorPartiesExceedsLimit() {
95 >        new Phaser(maxParties);
96          try {
97 <            new Phaser(new Phaser(), -1);
97 >            new Phaser(maxParties + 1);
98              shouldThrow();
99 <        } catch (IllegalArgumentException success) {
100 <        }
99 >        } catch (IllegalArgumentException success) {}
100 >
101 >        new Phaser(new Phaser(), maxParties);
102 >        try {
103 >            new Phaser(new Phaser(), maxParties + 1);
104 >            shouldThrow();
105 >        } catch (IllegalArgumentException success) {}
106 >    }
107 >
108 >    /**
109 >     * 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      /**
# Line 72 | Line 121 | public class PhaserTest extends JSR166Te
121       */
122      public void testConstructor5() {
123          Phaser parent = new Phaser();
124 <        assertEquals(parent, new Phaser(parent, 0).getParent());
124 >        assertSame(parent, new Phaser(parent, 0).getParent());
125 >        assertNull(new Phaser(null, 0).getParent());
126      }
127  
128      /**
129 <     * register() will increment the number of unarrived parties by one and not
130 <     * affect its arrived parties
129 >     * register() will increment the number of unarrived parties by
130 >     * one and not affect its arrived parties
131       */
132      public void testRegister1() {
133          Phaser phaser = new Phaser();
134 <        assertEquals(0, phaser.getUnarrivedParties());
135 <        phaser.register();
136 <        assertEquals(1, phaser.getUnarrivedParties());
87 <        assertEquals(0, phaser.getArrivedParties());
134 >        assertState(phaser, 0, 0, 0);
135 >        assertEquals(0, phaser.register());
136 >        assertState(phaser, 0, 1, 1);
137      }
138  
139      /**
# Line 92 | Line 141 | public class PhaserTest extends JSR166Te
141       */
142      public void testRegister2() {
143          Phaser phaser = new Phaser(0);
144 <        int expectedUnnarivedParties = (1 << 16) - 1;
145 <        for (int i = 0; i < expectedUnnarivedParties; i++) {
146 <            phaser.register();
147 <            assertEquals(i + 1, phaser.getUnarrivedParties());
144 >        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          }
151 +        assertState(phaser, 0, maxParties, maxParties);
152          try {
153              phaser.register();
154              shouldThrow();
155 <        } catch (IllegalStateException success) {
156 <        }
155 >        } catch (IllegalStateException success) {}
156 >
157 >        try {
158 >            phaser.bulkRegister(Integer.MAX_VALUE);
159 >            shouldThrow();
160 >        } catch (IllegalStateException success) {}
161      }
162  
163      /**
# Line 111 | Line 167 | public class PhaserTest extends JSR166Te
167      public void testRegister3() {
168          Phaser phaser = new Phaser();
169          assertEquals(0, phaser.register());
170 <        phaser.arrive();
170 >        assertEquals(0, phaser.arrive());
171          assertEquals(1, phaser.register());
172 +        assertState(phaser, 1, 2, 2);
173      }
174  
175      /**
# Line 121 | Line 178 | public class PhaserTest extends JSR166Te
178       */
179      public void testRegister4() {
180          Phaser phaser = new Phaser(1);
181 <        phaser.arrive();
182 <        int expectedPhase = phaser.register();
183 <        phaser.arrive();
184 <        assertEquals(expectedPhase, phaser.getPhase());
128 <    }
129 <
130 <    public void testRegister5() {
131 <        Phaser phaser = new Phaser();
132 <        phaser.register();
133 <        assertEquals(1, phaser.getUnarrivedParties());
181 >        assertEquals(0, phaser.arrive());
182 >        assertEquals(1, phaser.register());
183 >        assertEquals(1, phaser.arrive());
184 >        assertState(phaser, 1, 2, 1);
185      }
186  
187      /**
# Line 141 | Line 192 | public class PhaserTest extends JSR166Te
192          try {
193              new Phaser().bulkRegister(-1);
194              shouldThrow();
195 <        } catch (IllegalArgumentException success) {
145 <        }
195 >        } catch (IllegalArgumentException success) {}
196      }
197  
198      /**
# Line 151 | Line 201 | public class PhaserTest extends JSR166Te
201       */
202      public void testBulkRegister2() {
203          Phaser phaser = new Phaser();
204 <        phaser.bulkRegister(20);
205 <        assertEquals(20, phaser.getUnarrivedParties());
204 >        assertEquals(0, phaser.bulkRegister(20));
205 >        assertState(phaser, 0, 20, 20);
206      }
207  
208      /**
# Line 160 | Line 210 | public class PhaserTest extends JSR166Te
210       * throws IllegalStateException.
211       */
212      public void testBulkRegister3() {
213 +        assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1));
214 +
215          try {
216              new Phaser().bulkRegister(1 << 16);
217              shouldThrow();
218 <        } catch (IllegalStateException success) {
219 <        }
218 >        } catch (IllegalStateException success) {}
219 >
220 >        try {
221 >            new Phaser(2).bulkRegister((1 << 16) - 2);
222 >            shouldThrow();
223 >        } catch (IllegalStateException success) {}
224      }
225  
226      /**
# Line 181 | Line 237 | public class PhaserTest extends JSR166Te
237      }
238  
239      /**
240 <     *  Arrive() on a registered phaser increments phase.
240 >     * arrive() on a registered phaser increments phase.
241       */
242      public void testArrive1() {
243          Phaser phaser = new Phaser(1);
244 <        phaser.arrive();
245 <        assertEquals(1, phaser.getPhase());
244 >        assertState(phaser, 0, 1, 1);
245 >        assertEquals(0, phaser.arrive());
246 >        assertState(phaser, 1, 1, 1);
247      }
248  
249      /**
250 <     * arrive does not wait for others to arrive at barrier
250 >     * arriveAndDeregister does not wait for others to arrive at barrier
251       */
252 <    public void testArrive2() {
252 >    public void testArriveAndDeregister() throws InterruptedException {
253          final Phaser phaser = new Phaser(1);
254 <        phaser.register();
255 <        Thread thread = null;
256 <        for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) {
257 <            phaser.register();
258 <            thread = new Thread(new CheckedRunnable() {
259 <                void realRun() {
203 <                    r.run();
204 <                    phaser.arriveAndDeregister();
205 <                }});
206 <            thread.start();
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 +        assertTerminated(phaser, 1);
263 +    }
264  
265 <        phaser.arrive();
266 <        assertTrue(thread.isAlive());
267 <        assertFalse(phaser.isTerminated());
265 >    /**
266 >     * arriveAndDeregister does not wait for others to arrive at barrier
267 >     */
268 >    public void testArrive2() throws InterruptedException {
269 >        final Phaser phaser = new Phaser();
270 >        assertEquals(0, phaser.register());
271 >        List<Thread> threads = new ArrayList<Thread>();
272 >        for (int i = 0; i < 10; i++) {
273 >            assertEquals(0, phaser.register());
274 >            threads.add(newStartedThread(new CheckedRunnable() {
275 >                public void realRun() throws InterruptedException {
276 >                    assertEquals(0, phaser.arriveAndDeregister());
277 >                }}));
278 >        }
279 >
280 >        for (Thread thread : threads)
281 >            awaitTermination(thread, LONG_DELAY_MS);
282 >        assertState(phaser, 0, 1, 1);
283 >        assertEquals(0, phaser.arrive());
284 >        assertState(phaser, 1, 1, 1);
285      }
286  
287      /**
# Line 217 | Line 290 | public class PhaserTest extends JSR166Te
290      public void testArrive3() {
291          Phaser phaser = new Phaser(1);
292          phaser.forceTermination();
293 +        assertTerminated(phaser, 0, 1);
294 +        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
295          assertTrue(phaser.arrive() < 0);
296 <
296 >        assertTrue(phaser.register() < 0);
297 >        assertTrue(phaser.arriveAndDeregister() < 0);
298 >        assertTrue(phaser.awaitAdvance(1) < 0);
299 >        assertTrue(phaser.getPhase() < 0);
300      }
301  
302      /**
# Line 230 | Line 308 | public class PhaserTest extends JSR166Te
308              Phaser phaser = new Phaser();
309              phaser.arriveAndDeregister();
310              shouldThrow();
311 <        } catch (IllegalStateException success) {
234 <        }
311 >        } catch (IllegalStateException success) {}
312      }
313  
314      /**
315 <     * arriveAndDeregister deregisters reduces the number of arrived parties
315 >     * arriveAndDeregister reduces the number of arrived parties
316       */
317 <    public void testArriveAndDergeister2() {
317 >    public void testArriveAndDeregister2() {
318          final Phaser phaser = new Phaser(1);
319 <        phaser.register();
320 <        phaser.arrive();
321 <        int p = phaser.getArrivedParties();
322 <        assertTrue(p == 1);
323 <        phaser.arriveAndDeregister();
247 <        assertTrue(phaser.getArrivedParties() < p);
319 >        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      }
325  
326      /**
327 <     * arriveAndDeregister arrives to the barrier on a phaser with a parent and
327 >     * arriveAndDeregister arrives at the barrier on a phaser with a parent and
328       * when a deregistration occurs and causes the phaser to have zero parties
329       * its parent will be deregistered as well
330       */
331 <    public void testArriveAndDeregsiter3() {
331 >    public void testArriveAndDeregister3() {
332          Phaser parent = new Phaser();
333 <        Phaser root = new Phaser(parent);
334 <        root.register();
335 <        assertTrue(parent.getUnarrivedParties() > 0);
336 <        assertTrue(root.getUnarrivedParties() > 0);
337 <        root.arriveAndDeregister();
338 <        assertTrue(parent.getUnarrivedParties() == 0);
339 <        assertTrue(root.getUnarrivedParties() == 0);
340 <        assertTrue(root.isTerminated() && parent.isTerminated());
333 >        Phaser child = new Phaser(parent);
334 >        assertState(child, 0, 0, 0);
335 >        assertState(parent, 0, 0, 0);
336 >        assertEquals(0, child.register());
337 >        assertState(child, 0, 1, 1);
338 >        assertState(parent, 0, 1, 1);
339 >        assertEquals(0, child.arriveAndDeregister());
340 >        assertTerminated(child, 1);
341 >        assertTerminated(parent, 1);
342      }
343  
344      /**
345       * arriveAndDeregister deregisters one party from its parent when
346 <     * the number of parties of root is zero after deregistration
346 >     * the number of parties of child is zero after deregistration
347       */
348 <    public void testArriveAndDeregsiter4() {
348 >    public void testArriveAndDeregister4() {
349          Phaser parent = new Phaser();
350 <        Phaser root = new Phaser(parent);
351 <        parent.register();
352 <        root.register();
353 <        int parentParties = parent.getUnarrivedParties();
354 <        root.arriveAndDeregister();
355 <        assertEquals(parentParties - 1, parent.getUnarrivedParties());
350 >        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      }
359  
360      /**
# Line 283 | Line 362 | public class PhaserTest extends JSR166Te
362       * the number of parties of root is nonzero after deregistration.
363       */
364      public void testArriveAndDeregister5() {
365 <        Phaser parent = new Phaser();
365 >        Phaser root = new Phaser();
366 >        Phaser parent = new Phaser(root);
367          Phaser child = new Phaser(parent);
368 <        Phaser root = new Phaser(child);
369 <        assertTrue(parent.getUnarrivedParties() > 0);
370 <        assertTrue(child.getUnarrivedParties() > 0);
371 <        root.register();
372 <        root.arriveAndDeregister();
373 <        assertTrue(parent.getUnarrivedParties() == 0);
374 <        assertTrue(child.getUnarrivedParties() == 0);
375 <        assertTrue(root.isTerminated());
368 >        assertState(root, 0, 0, 0);
369 >        assertState(parent, 0, 0, 0);
370 >        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 >        assertTerminated(child, 1);
377 >        assertTerminated(parent, 1);
378 >        assertTerminated(root, 1);
379      }
380  
381      /**
382       * arriveAndDeregister returns the phase in which it leaves the
383       * phaser in after deregistration
384       */
385 <    public void testArriveAndDeregister6() {
385 >    public void testArriveAndDeregister6() throws InterruptedException {
386          final Phaser phaser = new Phaser(2);
387 <        new Thread(new CheckedRunnable() {
388 <            void realRun() {
389 <                getRunnable(SHORT_DELAY_MS).run();
390 <                phaser.arrive();
391 <            }}).start();
392 <        phaser.arriveAndAwaitAdvance();
393 <        int phase = phaser.arriveAndDeregister();
394 <        assertEquals(phase, phaser.getPhase());
387 >        Thread t = newStartedThread(new CheckedRunnable() {
388 >            public void realRun() {
389 >                assertEquals(0, phaser.arrive());
390 >            }});
391 >        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 >        assertTerminated(phaser, 2);
397 >        awaitTermination(t, SHORT_DELAY_MS);
398      }
399  
400      /**
# Line 316 | Line 402 | public class PhaserTest extends JSR166Te
402       */
403      public void testAwaitAdvance1() {
404          final Phaser phaser = new Phaser(1);
405 <        phaser.awaitAdvance(phaser.arrive());
405 >        assertEquals(0, phaser.arrive());
406 >        assertEquals(1, phaser.awaitAdvance(0));
407      }
408  
409      /**
# Line 325 | Line 412 | public class PhaserTest extends JSR166Te
412       */
413      public void testAwaitAdvance2() {
414          Phaser phaser = new Phaser();
415 <        phaser.awaitAdvance(-1);
415 >        assertTrue(phaser.awaitAdvance(-1) < 0);
416 >        assertState(phaser, 0, 0, 0);
417 >    }
418 >
419 >    /**
420 >     * awaitAdvance continues waiting if interrupted before waiting
421 >     */
422 >    public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
423 >        final Phaser phaser = new Phaser();
424 >        assertEquals(0, phaser.register());
425 >        final CountDownLatch threadStarted = new CountDownLatch(1);
426 >
427 >        Thread t = newStartedThread(new CheckedRunnable() {
428 >            public void realRun() throws InterruptedException {
429 >                Thread.currentThread().interrupt();
430 >                assertEquals(0, phaser.register());
431 >                assertEquals(0, phaser.arrive());
432 >                threadStarted.countDown();
433 >                assertTrue(Thread.currentThread().isInterrupted());
434 >                assertEquals(1, phaser.awaitAdvance(0));
435 >                assertTrue(Thread.currentThread().isInterrupted());
436 >            }});
437 >
438 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
439 >        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 while waiting does not abort on interrupt.
449 >     * awaitAdvance continues waiting if interrupted while waiting
450       */
451 <    public void testAwaitAdvance3() throws InterruptedException {
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 th1 = new Thread(new CheckedRunnable() {
457 <            void realRun() throws InterruptedException {
458 <                phaser.register();
459 <                getRunnable(LONG_DELAY_MS).run();
460 <                phaser.awaitAdvance(phaser.arrive());
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 <        phaser.register();
466 <        th1.start();
467 <        Thread.sleep(SHORT_DELAY_MS);
468 <        th1.interrupt();
469 <        Thread.sleep(LONG_DELAY_MS);
470 <        phaser.arrive();
471 <        assertFalse(th1.isInterrupted());
465 >
466 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
467 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
468 >        t.interrupt();
469 >        assertEquals(0, phaser.arrive());
470 >        awaitTermination(t, SMALL_DELAY_MS);
471 >
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      }
530  
531      /**
# Line 355 | Line 534 | public class PhaserTest extends JSR166Te
534       */
535      public void testAwaitAdvance4() throws InterruptedException {
536          final Phaser phaser = new Phaser(4);
537 <        final AtomicInteger phaseCount = new AtomicInteger(0);
537 >        final AtomicInteger count = new AtomicInteger(0);
538          List<Thread> threads = new ArrayList<Thread>();
539 <        for (int i = 0; i < 4; i++) {
540 <            threads.add(new Thread(new CheckedRunnable() {
541 <                void realRun() {
542 <                    int phase = phaser.arrive();
543 <                    phaseCount.incrementAndGet();
544 <                    getRunnable(LONG_DELAY_MS).run();
545 <                    phaser.awaitAdvance(phase);
546 <                    threadAssertTrue(phaseCount.get() == 4);
547 <                }}));
548 <        }
549 <        for (Thread thread : threads)
371 <            thread.start();
539 >        for (int i = 0; i < 4; i++)
540 >            threads.add(newStartedThread(new CheckedRunnable() {
541 >                public void realRun() {
542 >                    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          for (Thread thread : threads)
551 <            thread.join();
551 >            awaitTermination(thread, MEDIUM_DELAY_MS);
552      }
553  
554      /**
555       * awaitAdvance returns the current phase
556       */
557 <    public void testAwaitAdvance5() {
557 >    public void testAwaitAdvance5() throws InterruptedException {
558          final Phaser phaser = new Phaser(1);
559 <        int phase = phaser.awaitAdvance(phaser.arrive());
560 <        assertEquals(phase, phaser.getPhase());
561 <        phaser.register();
562 <        for (int i = 0; i < eight; i++) {
563 <            new Thread(new CheckedRunnable() {
564 <                void realRun() {
565 <                    getRunnable(SHORT_DELAY_MS).run();
559 >        assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
560 >        assertEquals(1, phaser.getPhase());
561 >        assertEquals(1, phaser.register());
562 >        List<Thread> threads = new ArrayList<Thread>();
563 >        for (int i = 0; i < 8; i++) {
564 >            final CountDownLatch latch = new CountDownLatch(1);
565 >            final boolean goesFirst = ((i & 1) == 0);
566 >            threads.add(newStartedThread(new CheckedRunnable() {
567 >                public void realRun() throws InterruptedException {
568 >                    if (goesFirst)
569 >                        latch.countDown();
570 >                    else
571 >                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
572                      phaser.arrive();
573 <                }}).start();
574 <            phase = phaser.awaitAdvance(phaser.arrive());
575 <            threadAssertEquals(phase, phaser.getPhase());
573 >                }}));
574 >            if (goesFirst)
575 >                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
576 >            else
577 >                latch.countDown();
578 >            assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
579 >            assertEquals(i + 2, phaser.getPhase());
580 >        }
581 >        for (Thread thread : threads)
582 >            awaitTermination(thread, SMALL_DELAY_MS);
583 >    }
584 >
585 >    /**
586 >     * awaitAdvance returns the current phase in child phasers
587 >     */
588 >    public void testAwaitAdvanceTieredPhaser() throws Exception {
589 >        final Phaser parent = new Phaser();
590 >        final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3);
591 >        final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3);
592 >        for (int i = 0; i < 3; i++) {
593 >            zeroPartyChildren.add(new Phaser(parent, 0));
594 >            onePartyChildren.add(new Phaser(parent, 1));
595 >        }
596 >        final List<Phaser> phasers = new ArrayList<Phaser>();
597 >        phasers.addAll(zeroPartyChildren);
598 >        phasers.addAll(onePartyChildren);
599 >        phasers.add(parent);
600 >        for (Phaser phaser : phasers) {
601 >            assertEquals(-42, phaser.awaitAdvance(-42));
602 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
603 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
604 >        }
605 >
606 >        for (Phaser child : onePartyChildren)
607 >            assertEquals(0, child.arrive());
608 >        for (Phaser phaser : phasers) {
609 >            assertEquals(-42, phaser.awaitAdvance(-42));
610 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
611 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
612 >            assertEquals(1, phaser.awaitAdvance(0));
613 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
614 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
615 >        }
616 >
617 >        for (Phaser child : onePartyChildren)
618 >            assertEquals(1, child.arrive());
619 >        for (Phaser phaser : phasers) {
620 >            assertEquals(-42, phaser.awaitAdvance(-42));
621 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
622 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
623 >            assertEquals(2, phaser.awaitAdvance(0));
624 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
625 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
626 >            assertEquals(2, phaser.awaitAdvance(1));
627 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
628 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
629          }
630      }
631  
632      /**
633       * awaitAdvance returns when the phaser is externally terminated
634       */
635 <    public void testAwaitAdvance6() {
635 >    public void testAwaitAdvance6() throws InterruptedException {
636          final Phaser phaser = new Phaser(3);
637 <        /*
638 <         * Start new thread. This thread waits a small amount of time
639 <         * and waits for the other two parties to arrive.  The party
640 <         * in the main thread arrives quickly so at best this thread
641 <         * waits for the second thread's party to arrive
642 <         */
643 <        new Thread(new CheckedRunnable() {
644 <            void realRun() {
645 <                getRunnable(SMALL_DELAY_MS).run();
646 <                int phase = phaser.awaitAdvance(phaser.arrive());
647 <                /*
648 <                 * This point is reached when force termination is called in which phase = -1
649 <                 */
650 <                threadAssertTrue(phase < 0);
651 <                threadAssertTrue(phaser.isTerminated());
652 <            }}).start();
416 <        /*
417 <         * This thread will cause the first thread run to wait, in doing so
418 <         * the main thread will force termination in which the first thread
419 <         * should exit peacefully as this one
420 <         */
421 <        new Thread(new CheckedRunnable() {
422 <            void realRun() {
423 <                getRunnable(LONG_DELAY_MS).run();
424 <                int p1 = phaser.arrive();
425 <                int phase = phaser.awaitAdvance(p1);
426 <                threadAssertTrue(phase < 0);
427 <                threadAssertTrue(phaser.isTerminated());
428 <            }}).start();
429 <
430 <        phaser.arrive();
637 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
638 >        final List<Thread> threads = new ArrayList<Thread>();
639 >        for (int i = 0; i < 2; i++) {
640 >            Runnable r = new CheckedRunnable() {
641 >                public void realRun() {
642 >                    assertEquals(0, phaser.arrive());
643 >                    threadsStarted.countDown();
644 >                    assertTrue(phaser.awaitAdvance(0) < 0);
645 >                    assertTrue(phaser.isTerminated());
646 >                    assertTrue(phaser.getPhase() < 0);
647 >                    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
648 >                    assertEquals(3, phaser.getRegisteredParties());
649 >                }};
650 >            threads.add(newStartedThread(r));
651 >        }
652 >        threadsStarted.await();
653          phaser.forceTermination();
654 +        assertTrue(phaser.isTerminated());
655 +        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
656 +        for (Thread thread : threads)
657 +            awaitTermination(thread, SMALL_DELAY_MS);
658 +        assertEquals(3, phaser.getRegisteredParties());
659      }
660  
661      /**
# Line 440 | Line 667 | public class PhaserTest extends JSR166Te
667              Phaser phaser = new Phaser();
668              phaser.arriveAndAwaitAdvance();
669              shouldThrow();
670 <        } catch (IllegalStateException success) {
444 <        }
445 <    }
446 <
447 <    /**
448 <     * Interrupted arriveAndAwaitAdvance does not throw InterruptedException
449 <     */
450 <    public void testArriveAndAwaitAdvance2() throws InterruptedException {
451 <        final Phaser phaser = new Phaser(2);
452 <        Thread th = new Thread(new CheckedRunnable() {
453 <            void realRun() {
454 <                phaser.arriveAndAwaitAdvance();
455 <            }});
456 <
457 <        th.start();
458 <        Thread.sleep(LONG_DELAY_MS);
459 <        th.interrupt();
460 <        Thread.sleep(LONG_DELAY_MS);
461 <        phaser.arrive();
462 <        assertFalse(th.isInterrupted());
670 >        } catch (IllegalStateException success) {}
671      }
672  
673      /**
# Line 467 | Line 675 | public class PhaserTest extends JSR166Te
675       * number of arrived parties is the same number that is accounted
676       * for when the main thread awaitsAdvance
677       */
678 <    public void testArriveAndAwaitAdvance3() {
678 >    public void testArriveAndAwaitAdvance3() throws InterruptedException {
679          final Phaser phaser = new Phaser(1);
680 <        final AtomicInteger arrivingCount = new AtomicInteger(0);
681 <        for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
682 <            new Thread(new CheckedRunnable() {
683 <                void realRun() {
684 <                    phaser.register();
685 <                    run.run();
686 <                    arrivingCount.getAndIncrement();
687 <                    phaser.arrive();
688 <                }}).start();
689 <        }
482 <        int phaseNumber = phaser.arriveAndAwaitAdvance();
483 <        arrivingCount.incrementAndGet();
484 <        //the + 1 adds to expectedArrive to account for the main threads arrival
485 <        int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1;
486 <        threadAssertEquals(expectedArrived, arrivingCount.get());
487 <    }
488 <    // .. initially called, for n tasks via
489 <    private List<Runnable> getRunnables(int size, long wait) {
490 <        List<Runnable> list = new ArrayList<Runnable>();
491 <        for (int i = 0; i < size; i++) {
492 <            list.add(getRunnable(wait));
493 <        }
494 <        return list;
495 <    }
680 >        final int THREADS = 3;
681 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
682 >        final List<Thread> threads = new ArrayList<Thread>();
683 >        for (int i = 0; i < THREADS; i++)
684 >            threads.add(newStartedThread(new CheckedRunnable() {
685 >                public void realRun() throws InterruptedException {
686 >                    assertEquals(0, phaser.register());
687 >                    threadsStarted.countDown();
688 >                    assertEquals(1, phaser.arriveAndAwaitAdvance());
689 >                }}));
690  
691 <    private Runnable getRunnable(final long wait) {
692 <        return new CheckedRunnable() {
693 <            void realRun() {
694 <                try {
695 <                    Thread.sleep(wait);
696 <                } catch (InterruptedException noop) {
697 <                // sleep interruption isn't a problem case for these example
698 <                }
699 <            }
700 <        };
691 >        assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
692 >        long t0 = System.nanoTime();
693 >        while (phaser.getArrivedParties() < THREADS)
694 >            Thread.yield();
695 >        assertEquals(THREADS, phaser.getArrivedParties());
696 >        assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
697 >        for (Thread thread : threads)
698 >            assertTrue(thread.isAlive());
699 >        assertState(phaser, 0, THREADS + 1, 1);
700 >        phaser.arriveAndAwaitAdvance();
701 >        for (Thread thread : threads)
702 >            awaitTermination(thread, SMALL_DELAY_MS);
703 >        assertState(phaser, 1, THREADS + 1, THREADS + 1);
704      }
705  
706   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines