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.5 by jsr166, Mon Aug 3 20:33:57 2009 UTC vs.
Revision 1.18 by jsr166, Tue Oct 12 06:19:44 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 junit.framework.Test;
15   import junit.framework.TestSuite;
16  
# Line 40 | Line 42 | public class PhaserTest extends JSR166Te
42      public void testConstructor2() {
43          try {
44              new Phaser(-1);
45 <            this.shouldThrow();
46 <        } catch (IllegalArgumentException success) {
45 <        }
45 >            shouldThrow();
46 >        } catch (IllegalArgumentException success) {}
47      }
48  
49      /**
# Line 62 | Line 63 | public class PhaserTest extends JSR166Te
63          try {
64              new Phaser(new Phaser(), -1);
65              shouldThrow();
66 <        } catch (IllegalArgumentException success) {
66 <        }
66 >        } catch (IllegalArgumentException success) {}
67      }
68  
69      /**
# Line 100 | Line 100 | public class PhaserTest extends JSR166Te
100          try {
101              phaser.register();
102              shouldThrow();
103 <        } catch (IllegalStateException success) {
104 <        }
103 >        } catch (IllegalStateException success) {}
104      }
105  
106      /**
# Line 141 | Line 140 | public class PhaserTest extends JSR166Te
140          try {
141              new Phaser().bulkRegister(-1);
142              shouldThrow();
143 <        } catch (IllegalArgumentException success) {
145 <        }
143 >        } catch (IllegalArgumentException success) {}
144      }
145  
146      /**
# Line 163 | Line 161 | public class PhaserTest extends JSR166Te
161          try {
162              new Phaser().bulkRegister(1 << 16);
163              shouldThrow();
164 <        } catch (IllegalStateException success) {
167 <        }
164 >        } catch (IllegalStateException success) {}
165      }
166  
167      /**
# Line 181 | Line 178 | public class PhaserTest extends JSR166Te
178      }
179  
180      /**
181 <     *  Arrive() on a registered phaser increments phase.
181 >     * Arrive() on a registered phaser increments phase.
182       */
183      public void testArrive1() {
184          Phaser phaser = new Phaser(1);
# Line 190 | Line 187 | public class PhaserTest extends JSR166Te
187      }
188  
189      /**
190 <     * arrive does not wait for others to arrive at barrier
190 >     * arriveAndDeregister does not wait for others to arrive at barrier
191       */
192 <    public void testArrive2() {
192 >    public void testArrive2() throws InterruptedException {
193          final Phaser phaser = new Phaser(1);
194          phaser.register();
195 <        Thread thread = null;
196 <        for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) {
195 >        List<Thread> threads = new ArrayList<Thread>();
196 >        for (int i = 0; i < 10; i++)
197              phaser.register();
198 <            thread = new Thread(new CheckedRunnable() {
199 <                void realRun() {
200 <                    r.run();
198 >            threads.add(newStartedThread(new CheckedRunnable() {
199 >                public void realRun() throws InterruptedException {
200 >                    Thread.sleep(SMALL_DELAY_MS);
201                      phaser.arriveAndDeregister();
202 <                }});
206 <            thread.start();
207 <        }
202 >                }}));
203  
204          phaser.arrive();
205 <        assertTrue(thread.isAlive());
205 >        assertTrue(threads.get(0).isAlive());
206          assertFalse(phaser.isTerminated());
207 +        for (Thread thread : threads)
208 +            thread.join();
209      }
210  
211      /**
# Line 218 | Line 215 | public class PhaserTest extends JSR166Te
215          Phaser phaser = new Phaser(1);
216          phaser.forceTermination();
217          assertTrue(phaser.arrive() < 0);
221
218      }
219  
220      /**
# Line 230 | Line 226 | public class PhaserTest extends JSR166Te
226              Phaser phaser = new Phaser();
227              phaser.arriveAndDeregister();
228              shouldThrow();
229 <
234 <        } catch (IllegalStateException success) {
235 <        }
229 >        } catch (IllegalStateException success) {}
230      }
231  
232      /**
# Line 243 | Line 237 | public class PhaserTest extends JSR166Te
237          phaser.register();
238          phaser.arrive();
239          int p = phaser.getArrivedParties();
240 <        assertTrue(p == 1);
240 >        assertEquals(1, p);
241          phaser.arriveAndDeregister();
242          assertTrue(phaser.getArrivedParties() < p);
243      }
# Line 260 | Line 254 | public class PhaserTest extends JSR166Te
254          assertTrue(parent.getUnarrivedParties() > 0);
255          assertTrue(root.getUnarrivedParties() > 0);
256          root.arriveAndDeregister();
257 <        assertTrue(parent.getUnarrivedParties() == 0);
258 <        assertTrue(root.getUnarrivedParties() == 0);
257 >        assertEquals(0, parent.getUnarrivedParties());
258 >        assertEquals(0, root.getUnarrivedParties());
259          assertTrue(root.isTerminated() && parent.isTerminated());
260      }
261  
# Line 291 | Line 285 | public class PhaserTest extends JSR166Te
285          assertTrue(child.getUnarrivedParties() > 0);
286          root.register();
287          root.arriveAndDeregister();
288 <        assertTrue(parent.getUnarrivedParties() == 0);
289 <        assertTrue(child.getUnarrivedParties() == 0);
288 >        assertEquals(0, parent.getUnarrivedParties());
289 >        assertEquals(0, child.getUnarrivedParties());
290          assertTrue(root.isTerminated());
291      }
292  
# Line 300 | Line 294 | public class PhaserTest extends JSR166Te
294       * arriveAndDeregister returns the phase in which it leaves the
295       * phaser in after deregistration
296       */
297 <    public void testArriveAndDeregister6() {
297 >    public void testArriveAndDeregister6() throws InterruptedException {
298          final Phaser phaser = new Phaser(2);
299 <        new Thread(new CheckedRunnable() {
300 <            void realRun() {
301 <                getRunnable(SHORT_DELAY_MS).run();
299 >        Thread t = newStartedThread(new CheckedRunnable() {
300 >            public void realRun() {
301 >                sleepTillInterrupted(SHORT_DELAY_MS);
302                  phaser.arrive();
303 <            }}).start();
303 >            }});
304          phaser.arriveAndAwaitAdvance();
305          int phase = phaser.arriveAndDeregister();
306          assertEquals(phase, phaser.getPhase());
307 +        t.join();
308      }
309  
310      /**
# Line 325 | Line 320 | public class PhaserTest extends JSR166Te
320       * phaser
321       */
322      public void testAwaitAdvance2() {
323 <        try {
324 <            Phaser phaser = new Phaser();
330 <            phaser.awaitAdvance(-1);
331 <        } catch (Exception failure) {
332 <            this.unexpectedException();
333 <        }
323 >        Phaser phaser = new Phaser();
324 >        phaser.awaitAdvance(-1);
325      }
326  
327      /**
328       * awaitAdvance while waiting does not abort on interrupt.
329       */
330 <    public void testAwaitAdvance3() {
330 >    public void testAwaitAdvance3() throws InterruptedException {
331          final Phaser phaser = new Phaser();
332 +        phaser.register();
333 +        final CountDownLatch threadStarted = new CountDownLatch(1);
334  
335 <        Thread th1 = new Thread(new CheckedRunnable() {
336 <            void realRun() throws InterruptedException {
335 >        Thread t = newStartedThread(new CheckedRunnable() {
336 >            public void realRun() throws InterruptedException {
337                  phaser.register();
338 <                getRunnable(LONG_DELAY_MS).run();
338 >                threadStarted.countDown();
339                  phaser.awaitAdvance(phaser.arrive());
340 +                assertTrue(Thread.currentThread().isInterrupted());
341              }});
342 <        phaser.register();
343 <        th1.start();
344 <        try {
345 <            Thread.sleep(SHORT_DELAY_MS);
352 <            th1.interrupt();
353 <            Thread.sleep(LONG_DELAY_MS);
354 <            phaser.arrive();
355 <        } catch (InterruptedException failure) {
356 <            threadUnexpectedException(failure);
357 <        }
358 <        assertFalse(th1.isInterrupted());
342 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
343 >        t.interrupt();
344 >        phaser.arrive();
345 >        awaitTermination(t, SMALL_DELAY_MS);
346      }
347  
348      /**
# Line 367 | Line 354 | public class PhaserTest extends JSR166Te
354          final AtomicInteger phaseCount = new AtomicInteger(0);
355          List<Thread> threads = new ArrayList<Thread>();
356          for (int i = 0; i < 4; i++) {
357 <            threads.add(new Thread(new CheckedRunnable() {
358 <                void realRun() {
357 >            threads.add(newStartedThread(new CheckedRunnable() {
358 >                public void realRun() {
359                      int phase = phaser.arrive();
360                      phaseCount.incrementAndGet();
361 <                    getRunnable(LONG_DELAY_MS).run();
361 >                    sleepTillInterrupted(SMALL_DELAY_MS);
362                      phaser.awaitAdvance(phase);
363 <                    threadAssertTrue(phaseCount.get() == 4);
363 >                    assertEquals(phaseCount.get(), 4);
364                  }}));
365          }
366          for (Thread thread : threads)
380            thread.start();
381        for (Thread thread : threads)
367              thread.join();
368      }
369  
370      /**
371       * awaitAdvance returns the current phase
372       */
373 <    public void testAwaitAdvance5() {
373 >    public void testAwaitAdvance5() throws InterruptedException {
374          final Phaser phaser = new Phaser(1);
375          int phase = phaser.awaitAdvance(phaser.arrive());
376          assertEquals(phase, phaser.getPhase());
377          phaser.register();
378 <        for (int i = 0; i < eight; i++) {
379 <            new Thread(new CheckedRunnable() {
380 <                void realRun() {
381 <                    getRunnable(SHORT_DELAY_MS).run();
378 >        List<Thread> threads = new ArrayList<Thread>();
379 >        for (int i = 0; i < 8; i++) {
380 >            final CountDownLatch latch = new CountDownLatch(1);
381 >            final boolean goesFirst = ((i & 1) == 0);
382 >            threads.add(newStartedThread(new CheckedRunnable() {
383 >                public void realRun() throws InterruptedException {
384 >                    if (goesFirst)
385 >                        latch.countDown();
386 >                    else
387 >                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
388                      phaser.arrive();
389 <                }
390 <                }).start();
389 >                }}));
390 >            if (goesFirst)
391 >                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
392 >            else
393 >                latch.countDown();
394              phase = phaser.awaitAdvance(phaser.arrive());
395 <            threadAssertEquals(phase, phaser.getPhase());
395 >            assertEquals(phase, phaser.getPhase());
396          }
397 +        for (Thread thread : threads)
398 +            awaitTermination(thread, SMALL_DELAY_MS);
399      }
400  
401      /**
402       * awaitAdvance returns when the phaser is externally terminated
403       */
404 <    public void testAwaitAdvance6() {
404 >    public void testAwaitAdvance6() throws InterruptedException {
405          final Phaser phaser = new Phaser(3);
406 <        /*
407 <         * Start new thread. This thread waits a small amount of time
408 <         * and waits for the other two parties to arrive.  The party
409 <         * in the main thread arrives quickly so at best this thread
410 <         * waits for the second thread's party to arrive
411 <         */
412 <        new Thread(new CheckedRunnable() {
413 <            void realRun() {
414 <                getRunnable(SMALL_DELAY_MS).run();
415 <                int phase = phaser.awaitAdvance(phaser.arrive());
416 <                /*
417 <                 * This point is reached when force termination is called in which phase = -1
418 <                 */
419 <                threadAssertTrue(phase < 0);
420 <                threadAssertTrue(phaser.isTerminated());
425 <            }}).start();
426 <        /*
427 <         * This thread will cause the first thread run to wait, in doing so
428 <         * the main thread will force termination in which the first thread
429 <         * should exit peacefully as this one
430 <         */
431 <        new Thread(new CheckedRunnable() {
432 <            void realRun() {
433 <                getRunnable(LONG_DELAY_MS).run();
434 <                int p1 = phaser.arrive();
435 <                int phase = phaser.awaitAdvance(p1);
436 <                threadAssertTrue(phase < 0);
437 <                threadAssertTrue(phaser.isTerminated());
438 <            }}).start();
439 <
440 <        phaser.arrive();
406 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
407 >        final List<Thread> threads = new ArrayList<Thread>();
408 >        for (int i = 0; i < 2; i++) {
409 >            Runnable r = new CheckedRunnable() {
410 >                public void realRun() {
411 >                    int p1 = phaser.arrive();
412 >                    assertTrue(p1 >= 0);
413 >                    threadsStarted.countDown();
414 >                    int phase = phaser.awaitAdvance(p1);
415 >                    assertTrue(phase < 0);
416 >                    assertTrue(phaser.isTerminated());
417 >                }};
418 >            threads.add(newStartedThread(r));
419 >        }
420 >        threadsStarted.await();
421          phaser.forceTermination();
422 +        for (Thread thread : threads)
423 +            awaitTermination(thread, SMALL_DELAY_MS);
424      }
425  
426      /**
# Line 450 | Line 432 | public class PhaserTest extends JSR166Te
432              Phaser phaser = new Phaser();
433              phaser.arriveAndAwaitAdvance();
434              shouldThrow();
435 <        } catch (IllegalStateException success) {
454 <        }
435 >        } catch (IllegalStateException success) {}
436      }
437  
438      /**
439       * Interrupted arriveAndAwaitAdvance does not throw InterruptedException
440       */
441 <    public void testArriveAndAwaitAdvance2() {
441 >    public void testArriveAndAwaitAdvance2() throws InterruptedException {
442          final Phaser phaser = new Phaser(2);
443 <        Thread th = new Thread(new CheckedRunnable() {
444 <            void realRun() {
443 >        final CountDownLatch threadStarted = new CountDownLatch(1);
444 >        final AtomicBoolean advanced = new AtomicBoolean(false);
445 >        final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false);
446 >        Thread t = newStartedThread(new CheckedRunnable() {
447 >            public void realRun() throws InterruptedException {
448 >                threadStarted.countDown();
449                  phaser.arriveAndAwaitAdvance();
450 +                advanced.set(true);
451 +                assertTrue(Thread.currentThread().isInterrupted());
452 +                while (!checkedInterruptStatus.get())
453 +                    Thread.yield();
454              }});
455  
456 <        try {
457 <            th.start();
458 <            Thread.sleep(LONG_DELAY_MS);
459 <            th.interrupt();
460 <            Thread.sleep(LONG_DELAY_MS);
461 <            phaser.arrive();
462 <        } catch (InterruptedException failure) {
463 <            this.unexpectedException();
475 <        }
476 <        assertFalse(th.isInterrupted());
456 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
457 >        t.interrupt();
458 >        phaser.arrive();
459 >        while (!advanced.get())
460 >            Thread.yield();
461 >        assertTrue(t.isInterrupted());
462 >        checkedInterruptStatus.set(true);
463 >        awaitTermination(t, SMALL_DELAY_MS);
464      }
465  
466      /**
# Line 481 | Line 468 | public class PhaserTest extends JSR166Te
468       * number of arrived parties is the same number that is accounted
469       * for when the main thread awaitsAdvance
470       */
471 <    public void testArriveAndAwaitAdvance3() {
471 >    public void testArriveAndAwaitAdvance3() throws InterruptedException {
472          final Phaser phaser = new Phaser(1);
473 <        final AtomicInteger arrivingCount = new AtomicInteger(0);
474 <        for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
475 <            new Thread(new CheckedRunnable() {
476 <                void realRun() {
477 <                    phaser.register();
478 <                    run.run();
479 <                    arrivingCount.getAndIncrement();
493 <                    phaser.arrive();
494 <                }}).start();
495 <        }
496 <        int phaseNumber = phaser.arriveAndAwaitAdvance();
497 <        arrivingCount.incrementAndGet();
498 <        //the + 1 adds to expectedArrive to account for the main threads arrival
499 <        int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1;
500 <        threadAssertEquals(expectedArrived, arrivingCount.get());
501 <    }
502 <    // .. initially called, for n tasks via
503 <    private List<Runnable> getRunnables(int size, long wait) {
504 <        List<Runnable> list = new ArrayList<Runnable>();
505 <        for (int i = 0; i < size; i++) {
506 <            list.add(getRunnable(wait));
473 >        final List<Thread> threads = new ArrayList<Thread>();
474 >        for (int i = 0; i < 3; i++) {
475 >            threads.add(newStartedThread(new CheckedRunnable() {
476 >                    public void realRun() throws InterruptedException {
477 >                        phaser.register();
478 >                        phaser.arriveAndAwaitAdvance();
479 >                    }}));
480          }
481 <        return list;
482 <    }
483 <
484 <    private Runnable getRunnable(final long wait) {
485 <        return new CheckedRunnable() {
513 <            void realRun() {
514 <                try {
515 <                    Thread.sleep(wait);
516 <                } catch (InterruptedException noop) {
517 <                // sleep interruption isn't a problem case for these example
518 <                }
519 <            }
520 <        };
481 >        Thread.sleep(MEDIUM_DELAY_MS);
482 >        assertEquals(phaser.getArrivedParties(), 3);
483 >        phaser.arriveAndAwaitAdvance();
484 >        for (Thread thread : threads)
485 >            thread.join();
486      }
487  
488   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines