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.1 by dl, Fri Jul 31 23:02:50 2009 UTC vs.
Revision 1.6 by jsr166, Mon Aug 3 22:06:50 2009 UTC

# Line 40 | Line 40 | public class PhaserTest extends JSR166Te
40      public void testConstructor2() {
41          try {
42              new Phaser(-1);
43 <            this.shouldThrow();
43 >            shouldThrow();
44          } catch (IllegalArgumentException success) {
45          }
46      }
# Line 88 | Line 88 | public class PhaserTest extends JSR166Te
88      }
89  
90      /**
91 <     * Registering any more then 65536 parties causes IllegalStateExceptiom
91 >     * Registering more than 65536 parties causes IllegalStateException
92       */
93      public void testRegister2() {
94          Phaser phaser = new Phaser(0);
# Line 101 | Line 101 | public class PhaserTest extends JSR166Te
101              phaser.register();
102              shouldThrow();
103          } catch (IllegalStateException success) {
104        } catch (Exception ex) {
105            threadUnexpectedException(ex);
104          }
105      }
106  
# Line 137 | Line 135 | public class PhaserTest extends JSR166Te
135  
136      /**
137       * Invoking bulkRegister with a negative parameter throws an
138 <     * IllegalArgumentExceptiom
138 >     * IllegalArgumentException
139       */
140      public void testBulkRegister1() {
141          try {
# Line 158 | Line 156 | public class PhaserTest extends JSR166Te
156      }
157  
158      /**
159 <     * Registering with a number of parties greater then or equal to 1<<16
160 <     * throws IllegalStateExceptiom.
159 >     * Registering with a number of parties greater than or equal to 1<<16
160 >     * throws IllegalStateException.
161       */
162      public void testBulkRegister3() {
163          try {
# Line 200 | Line 198 | public class PhaserTest extends JSR166Te
198          Thread thread = null;
199          for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) {
200              phaser.register();
201 <            thread = new Thread() {
202 <
205 <                public void run() {
201 >            thread = new Thread(new CheckedRunnable() {
202 >                void realRun() {
203                      r.run();
204                      phaser.arriveAndDeregister();
205 <                }
209 <            };
205 >                }});
206              thread.start();
207          }
208  
# Line 216 | Line 212 | public class PhaserTest extends JSR166Te
212      }
213  
214      /**
215 <     * arrive() returns a negative number if the Phaser is termindated
215 >     * arrive() returns a negative number if the Phaser is terminated
216       */
217      public void testArrive3() {
218          Phaser phaser = new Phaser(1);
# Line 227 | Line 223 | public class PhaserTest extends JSR166Te
223  
224      /**
225       * arriveAndDeregister() throws IllegalStateException if number of
226 <     * registered or unnarived parties would become negative
226 >     * registered or unarrived parties would become negative
227       */
228      public void testArriveAndDeregister1() {
229          try {
230              Phaser phaser = new Phaser();
231              phaser.arriveAndDeregister();
232              shouldThrow();
237
233          } catch (IllegalStateException success) {
234          }
235      }
236  
237      /**
238 <     * arriveAndDeregister derigisters reduces the number of arrived parties
238 >     * arriveAndDeregister deregisters reduces the number of arrived parties
239       */
240      public void testArriveAndDergeister2() {
241          final Phaser phaser = new Phaser(1);
# Line 306 | Line 301 | public class PhaserTest extends JSR166Te
301       */
302      public void testArriveAndDeregister6() {
303          final Phaser phaser = new Phaser(2);
304 <        new Thread() {
305 <
311 <            public void run() {
304 >        new Thread(new CheckedRunnable() {
305 >            void realRun() {
306                  getRunnable(SHORT_DELAY_MS).run();
307                  phaser.arrive();
308 <            }
315 <        }.start();
308 >            }}).start();
309          phaser.arriveAndAwaitAdvance();
310          int phase = phaser.arriveAndDeregister();
311          assertEquals(phase, phaser.getPhase());
# Line 331 | Line 324 | public class PhaserTest extends JSR166Te
324       * phaser
325       */
326      public void testAwaitAdvance2() {
327 <        try {
328 <            Phaser phaser = new Phaser();
336 <            phaser.awaitAdvance(-1);
337 <        } catch (Exception failure) {
338 <            this.unexpectedException();
339 <        }
327 >        Phaser phaser = new Phaser();
328 >        phaser.awaitAdvance(-1);
329      }
330  
331      /**
332       * awaitAdvance while waiting does not abort on interrupt.
333       */
334 <    public void testAwaitAdvance3() {
334 >    public void testAwaitAdvance3() throws InterruptedException {
335          final Phaser phaser = new Phaser();
347        Thread th1 = new Thread() {
336  
337 <            public void run() {
338 <                try {
339 <                    phaser.register();
340 <                    getRunnable(LONG_DELAY_MS).run();
341 <                    phaser.awaitAdvance(phaser.arrive());
342 <                } catch (Exception failure) {
355 <                    threadUnexpectedException(failure);
356 <                }
357 <
358 <            }
359 <        };
337 >        Thread th1 = new Thread(new CheckedRunnable() {
338 >            void realRun() throws InterruptedException {
339 >                phaser.register();
340 >                getRunnable(LONG_DELAY_MS).run();
341 >                phaser.awaitAdvance(phaser.arrive());
342 >            }});
343          phaser.register();
344          th1.start();
345 <        try {
346 <            Thread.sleep(SHORT_DELAY_MS);
347 <            th1.interrupt();
348 <            Thread.sleep(LONG_DELAY_MS);
366 <            phaser.arrive();
367 <        } catch (Exception failure) {
368 <            unexpectedException();
369 <        }
345 >        Thread.sleep(SHORT_DELAY_MS);
346 >        th1.interrupt();
347 >        Thread.sleep(LONG_DELAY_MS);
348 >        phaser.arrive();
349          assertFalse(th1.isInterrupted());
350      }
351  
# Line 374 | Line 353 | public class PhaserTest extends JSR166Te
353       * awaitAdvance atomically waits for all parties within the same phase to
354       * complete before continuing
355       */
356 <    public void testAwaitAdvance4() {
357 <        final Phaser phaser = new Phaser(four);
356 >    public void testAwaitAdvance4() throws InterruptedException {
357 >        final Phaser phaser = new Phaser(4);
358          final AtomicInteger phaseCount = new AtomicInteger(0);
359 <        for (int i = 0; i < four; i++) {
360 <            new Thread() {
361 <
362 <                public void run() {
359 >        List<Thread> threads = new ArrayList<Thread>();
360 >        for (int i = 0; i < 4; i++) {
361 >            threads.add(new Thread(new CheckedRunnable() {
362 >                void realRun() {
363                      int phase = phaser.arrive();
364                      phaseCount.incrementAndGet();
365                      getRunnable(LONG_DELAY_MS).run();
366                      phaser.awaitAdvance(phase);
367 <                    assertTrue(phaseCount.get() == four);
368 <                }
390 <            }.start();
367 >                    threadAssertTrue(phaseCount.get() == 4);
368 >                }}));
369          }
370 +        for (Thread thread : threads)
371 +            thread.start();
372 +        for (Thread thread : threads)
373 +            thread.join();
374      }
375  
376      /**
# Line 400 | Line 382 | public class PhaserTest extends JSR166Te
382          assertEquals(phase, phaser.getPhase());
383          phaser.register();
384          for (int i = 0; i < eight; i++) {
385 <            new Thread() {
386 <
405 <                public void run() {
385 >            new Thread(new CheckedRunnable() {
386 >                void realRun() {
387                      getRunnable(SHORT_DELAY_MS).run();
388                      phaser.arrive();
389 <                }
409 <            }.start();
389 >                }}).start();
390              phase = phaser.awaitAdvance(phaser.arrive());
391 <            assertEquals(phase, phaser.getPhase());
391 >            threadAssertEquals(phase, phaser.getPhase());
392          }
393      }
394  
# Line 423 | Line 403 | public class PhaserTest extends JSR166Te
403           * in the main thread arrives quickly so at best this thread
404           * waits for the second thread's party to arrive
405           */
406 <        new Thread() {
407 <            
428 <            public void run() {
406 >        new Thread(new CheckedRunnable() {
407 >            void realRun() {
408                  getRunnable(SMALL_DELAY_MS).run();
409                  int phase = phaser.awaitAdvance(phaser.arrive());
410                  /*
# Line 433 | Line 412 | public class PhaserTest extends JSR166Te
412                   */
413                  threadAssertTrue(phase < 0);
414                  threadAssertTrue(phaser.isTerminated());
415 <            }
437 <        }.start();
415 >            }}).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
418 >         * the main thread will force termination in which the first thread
419 >         * should exit peacefully as this one
420           */
421 <        new Thread() {
422 <
445 <            public void run() {
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 <            }
452 <        }.start();
428 >            }}).start();
429  
430          phaser.arrive();
431          phaser.forceTermination();
# Line 471 | Line 447 | public class PhaserTest extends JSR166Te
447      /**
448       * Interrupted arriveAndAwaitAdvance does not throw InterruptedException
449       */
450 <    public void testArriveAndAwaitAdvance2() {
450 >    public void testArriveAndAwaitAdvance2() throws InterruptedException {
451          final Phaser phaser = new Phaser(2);
452 <        Thread th = new Thread() {
453 <            public void run() {
454 <                try {
455 <                    phaser.arriveAndAwaitAdvance();
456 <                } catch (Exception failure) {
457 <                    threadUnexpectedException(failure);
458 <                }
459 <            }
460 <        };
461 <
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 <        }
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());
463      }
464  
# Line 504 | Line 471 | public class PhaserTest extends JSR166Te
471          final Phaser phaser = new Phaser(1);
472          final AtomicInteger arrivingCount = new AtomicInteger(0);
473          for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
474 <            new Thread() {
475 <
509 <                public void run() {
474 >            new Thread(new CheckedRunnable() {
475 >                void realRun() {
476                      phaser.register();
477                      run.run();
478                      arrivingCount.getAndIncrement();
479                      phaser.arrive();
480 <                }
515 <            }.start();
480 >                }}).start();
481          }
482          int phaseNumber = phaser.arriveAndAwaitAdvance();
483          arrivingCount.incrementAndGet();
# Line 530 | Line 495 | public class PhaserTest extends JSR166Te
495      }
496  
497      private Runnable getRunnable(final long wait) {
498 <        return new Runnable() {
499 <
535 <            public void run() {
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
540                } catch (Exception ex) {
541                    threadUnexpectedException(ex);
504                  }
543
505              }
506          };
507      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines