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.7 by jsr166, Wed Aug 5 00:48:28 2009 UTC

# Line 190 | Line 190 | public class PhaserTest extends JSR166Te
190      }
191  
192      /**
193 <     * arrive does not wait for others to arrive at barrier
193 >     * arriveAndDeregister does not wait for others to arrive at barrier
194       */
195 <    public void testArrive2() {
195 >    public void testArrive2() throws InterruptedException {
196          final Phaser phaser = new Phaser(1);
197          phaser.register();
198 <        Thread thread = null;
199 <        for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) {
198 >        List<Thread> threads = new ArrayList<Thread>();
199 >        for (int i = 0; i < 10; i++)
200              phaser.register();
201 <            thread = new Thread(new CheckedRunnable() {
202 <                void realRun() {
203 <                    r.run();
201 >            threads.add(newStartedThread(new CheckedRunnable() {
202 >                void realRun() throws InterruptedException {
203 >                    Thread.sleep(SMALL_DELAY_MS);
204                      phaser.arriveAndDeregister();
205 <                }});
206 <            thread.start();
207 <        }
205 >                }}));
206  
207          phaser.arrive();
208 <        assertTrue(thread.isAlive());
208 >        assertTrue(threads.get(0).isAlive());
209          assertFalse(phaser.isTerminated());
210 +        for (Thread thread : threads)
211 +            thread.join();
212      }
213  
214      /**
# Line 218 | Line 218 | public class PhaserTest extends JSR166Te
218          Phaser phaser = new Phaser(1);
219          phaser.forceTermination();
220          assertTrue(phaser.arrive() < 0);
221
221      }
222  
223      /**
# Line 299 | Line 298 | public class PhaserTest extends JSR166Te
298       * arriveAndDeregister returns the phase in which it leaves the
299       * phaser in after deregistration
300       */
301 <    public void testArriveAndDeregister6() {
301 >    public void testArriveAndDeregister6() throws InterruptedException {
302          final Phaser phaser = new Phaser(2);
303 <        new Thread(new CheckedRunnable() {
303 >        Thread t = newStartedThread(new CheckedRunnable() {
304              void realRun() {
305 <                getRunnable(SHORT_DELAY_MS).run();
305 >                sleepTillInterrupted(SHORT_DELAY_MS);
306                  phaser.arrive();
307 <            }}).start();
307 >            }});
308          phaser.arriveAndAwaitAdvance();
309          int phase = phaser.arriveAndDeregister();
310          assertEquals(phase, phaser.getPhase());
311 +        t.join();
312      }
313  
314      /**
# Line 333 | Line 333 | public class PhaserTest extends JSR166Te
333       */
334      public void testAwaitAdvance3() throws InterruptedException {
335          final Phaser phaser = new Phaser();
336 +        phaser.register();
337  
338 <        Thread th1 = new Thread(new CheckedRunnable() {
338 >        Thread t = newStartedThread(new CheckedRunnable() {
339              void realRun() throws InterruptedException {
340                  phaser.register();
341 <                getRunnable(LONG_DELAY_MS).run();
341 >                sleepTillInterrupted(LONG_DELAY_MS);
342                  phaser.awaitAdvance(phaser.arrive());
343              }});
344 <        phaser.register();
345 <        th1.start();
346 <        Thread.sleep(SHORT_DELAY_MS);
346 <        th1.interrupt();
347 <        Thread.sleep(LONG_DELAY_MS);
344 >        Thread.sleep(SMALL_DELAY_MS);
345 >        t.interrupt();
346 >        Thread.sleep(SMALL_DELAY_MS);
347          phaser.arrive();
348 <        assertFalse(th1.isInterrupted());
348 >        assertFalse(t.isInterrupted());
349 >        t.join();
350      }
351  
352      /**
# Line 358 | Line 358 | public class PhaserTest extends JSR166Te
358          final AtomicInteger phaseCount = new AtomicInteger(0);
359          List<Thread> threads = new ArrayList<Thread>();
360          for (int i = 0; i < 4; i++) {
361 <            threads.add(new Thread(new CheckedRunnable() {
361 >            threads.add(newStartedThread(new CheckedRunnable() {
362                  void realRun() {
363                      int phase = phaser.arrive();
364                      phaseCount.incrementAndGet();
365 <                    getRunnable(LONG_DELAY_MS).run();
365 >                    sleepTillInterrupted(SMALL_DELAY_MS);
366                      phaser.awaitAdvance(phase);
367                      threadAssertTrue(phaseCount.get() == 4);
368                  }}));
369          }
370          for (Thread thread : threads)
371            thread.start();
372        for (Thread thread : threads)
371              thread.join();
372      }
373  
374      /**
375       * awaitAdvance returns the current phase
376       */
377 <    public void testAwaitAdvance5() {
377 >    public void testAwaitAdvance5() throws InterruptedException {
378          final Phaser phaser = new Phaser(1);
379          int phase = phaser.awaitAdvance(phaser.arrive());
380          assertEquals(phase, phaser.getPhase());
381          phaser.register();
382 <        for (int i = 0; i < eight; i++) {
383 <            new Thread(new CheckedRunnable() {
382 >        List<Thread> threads = new ArrayList<Thread>();
383 >        for (int i = 0; i < 8; i++) {
384 >            threads.add(newStartedThread(new CheckedRunnable() {
385                  void realRun() {
386 <                    getRunnable(SHORT_DELAY_MS).run();
386 >                    sleepTillInterrupted(SHORT_DELAY_MS);
387                      phaser.arrive();
388 <                }}).start();
388 >                }}));
389              phase = phaser.awaitAdvance(phaser.arrive());
390              threadAssertEquals(phase, phaser.getPhase());
391          }
392 +        for (Thread thread : threads)
393 +            thread.join();
394      }
395  
396      /**
397       * awaitAdvance returns when the phaser is externally terminated
398       */
399 <    public void testAwaitAdvance6() {
399 >    public void testAwaitAdvance6() throws InterruptedException {
400          final Phaser phaser = new Phaser(3);
401          /*
402           * Start new thread. This thread waits a small amount of time
# Line 403 | Line 404 | public class PhaserTest extends JSR166Te
404           * in the main thread arrives quickly so at best this thread
405           * waits for the second thread's party to arrive
406           */
407 <        new Thread(new CheckedRunnable() {
407 >        Thread t1 = newStartedThread(new CheckedRunnable() {
408              void realRun() {
409 <                getRunnable(SMALL_DELAY_MS).run();
409 >                sleepTillInterrupted(SMALL_DELAY_MS);
410                  int phase = phaser.awaitAdvance(phaser.arrive());
411                  /*
412                   * This point is reached when force termination is called in which phase = -1
413                   */
414                  threadAssertTrue(phase < 0);
415                  threadAssertTrue(phaser.isTerminated());
416 <            }}).start();
416 >            }});
417          /*
418           * This thread will cause the first thread run to wait, in doing so
419           * the main thread will force termination in which the first thread
420           * should exit peacefully as this one
421           */
422 <        new Thread(new CheckedRunnable() {
422 >        Thread t2 = newStartedThread(new CheckedRunnable() {
423              void realRun() {
424 <                getRunnable(LONG_DELAY_MS).run();
424 >                sleepTillInterrupted(MEDIUM_DELAY_MS);
425                  int p1 = phaser.arrive();
426                  int phase = phaser.awaitAdvance(p1);
427                  threadAssertTrue(phase < 0);
428                  threadAssertTrue(phaser.isTerminated());
429 <            }}).start();
429 >            }});
430  
431          phaser.arrive();
432          phaser.forceTermination();
433 +        t1.join();
434 +        t2.join();
435      }
436  
437      /**
# Line 449 | Line 452 | public class PhaserTest extends JSR166Te
452       */
453      public void testArriveAndAwaitAdvance2() throws InterruptedException {
454          final Phaser phaser = new Phaser(2);
455 <        Thread th = new Thread(new CheckedRunnable() {
455 >        Thread th = newStartedThread(new CheckedRunnable() {
456              void realRun() {
457                  phaser.arriveAndAwaitAdvance();
458              }});
459  
460 <        th.start();
458 <        Thread.sleep(LONG_DELAY_MS);
460 >        Thread.sleep(SMALL_DELAY_MS);
461          th.interrupt();
462 <        Thread.sleep(LONG_DELAY_MS);
462 >        Thread.sleep(SMALL_DELAY_MS);
463          phaser.arrive();
464          assertFalse(th.isInterrupted());
465 +        th.join();
466      }
467  
468      /**
# Line 467 | Line 470 | public class PhaserTest extends JSR166Te
470       * number of arrived parties is the same number that is accounted
471       * for when the main thread awaitsAdvance
472       */
473 <    public void testArriveAndAwaitAdvance3() {
473 >    public void testArriveAndAwaitAdvance3() throws InterruptedException {
474          final Phaser phaser = new Phaser(1);
475          final AtomicInteger arrivingCount = new AtomicInteger(0);
476 <        for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
477 <            new Thread(new CheckedRunnable() {
478 <                void realRun() {
476 >        final List<Thread> threads = new ArrayList<Thread>();
477 >        for (int i = 0; i < 6; i++) {
478 >            threads.add(newStartedThread(new CheckedRunnable() {
479 >                void realRun() throws InterruptedException {
480                      phaser.register();
481 <                    run.run();
481 >                    sleepTillInterrupted(SHORT_DELAY_MS);
482                      arrivingCount.getAndIncrement();
483                      phaser.arrive();
484 <                }}).start();
484 >                }}));
485          }
486          int phaseNumber = phaser.arriveAndAwaitAdvance();
487          arrivingCount.incrementAndGet();
488          //the + 1 adds to expectedArrive to account for the main threads arrival
489          int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1;
490          threadAssertEquals(expectedArrived, arrivingCount.get());
491 <    }
492 <    // .. 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 <    }
496 <
497 <    private Runnable getRunnable(final long wait) {
498 <        return new CheckedRunnable() {
499 <            void realRun() {
500 <                try {
501 <                    Thread.sleep(wait);
502 <                } catch (InterruptedException noop) {
503 <                // sleep interruption isn't a problem case for these example
504 <                }
505 <            }
506 <        };
491 >        for (Thread thread : threads)
492 >            thread.join();
493      }
494  
495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines