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.2 by jsr166, Fri Jul 31 23:37:31 2009 UTC vs.
Revision 1.16 by jsr166, Mon Oct 11 05:35:19 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 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 100 | Line 100 | public class PhaserTest extends JSR166Te
100          try {
101              phaser.register();
102              shouldThrow();
103 <        } catch (IllegalStateException success) {
104 <        } catch (Exception ex) {
105 <            threadUnexpectedException(ex);
106 <        }
103 >        } catch (IllegalStateException success) {}
104      }
105  
106      /**
# Line 137 | Line 134 | public class PhaserTest extends JSR166Te
134  
135      /**
136       * Invoking bulkRegister with a negative parameter throws an
137 <     * IllegalArgumentExceptiom
137 >     * IllegalArgumentException
138       */
139      public void testBulkRegister1() {
140          try {
141              new Phaser().bulkRegister(-1);
142              shouldThrow();
143 <        } catch (IllegalArgumentException success) {
147 <        }
143 >        } catch (IllegalArgumentException success) {}
144      }
145  
146      /**
# Line 158 | Line 154 | public class PhaserTest extends JSR166Te
154      }
155  
156      /**
157 <     * Registering with a number of parties greater then or equal to 1<<16
158 <     * throws IllegalStateExceptiom.
157 >     * Registering with a number of parties greater than or equal to 1<<16
158 >     * throws IllegalStateException.
159       */
160      public void testBulkRegister3() {
161          try {
162              new Phaser().bulkRegister(1 << 16);
163              shouldThrow();
164 <        } catch (IllegalStateException success) {
169 <        }
164 >        } catch (IllegalStateException success) {}
165      }
166  
167      /**
# Line 183 | 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 192 | 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() {
199 <
200 <                public void run() {
206 <                    r.run();
198 >            threads.add(newStartedThread(new CheckedRunnable() {
199 >                public void realRun() throws InterruptedException {
200 >                    Thread.sleep(SMALL_DELAY_MS);
201                      phaser.arriveAndDeregister();
202 <                }
209 <            };
210 <            thread.start();
211 <        }
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      /**
212 <     * arrive() returns a negative number if the Phaser is termindated
212 >     * arrive() returns a negative number if the Phaser is terminated
213       */
214      public void testArrive3() {
215          Phaser phaser = new Phaser(1);
216          phaser.forceTermination();
217          assertTrue(phaser.arrive() < 0);
225
218      }
219  
220      /**
221       * arriveAndDeregister() throws IllegalStateException if number of
222 <     * registered or unnarived parties would become negative
222 >     * registered or unarrived parties would become negative
223       */
224      public void testArriveAndDeregister1() {
225          try {
226              Phaser phaser = new Phaser();
227              phaser.arriveAndDeregister();
228              shouldThrow();
229 <
238 <        } catch (IllegalStateException success) {
239 <        }
229 >        } catch (IllegalStateException success) {}
230      }
231  
232      /**
233 <     * arriveAndDeregister derigisters reduces the number of arrived parties
233 >     * arriveAndDeregister deregisters reduces the number of arrived parties
234       */
235      public void testArriveAndDergeister2() {
236          final Phaser phaser = new Phaser(1);
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 264 | 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 295 | 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 304 | 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() {
300 <
301 <            public void run() {
312 <                getRunnable(SHORT_DELAY_MS).run();
299 >        Thread t = newStartedThread(new CheckedRunnable() {
300 >            public void realRun() {
301 >                sleepTillInterrupted(SHORT_DELAY_MS);
302                  phaser.arrive();
303 <            }
315 <        }.start();
303 >            }});
304          phaser.arriveAndAwaitAdvance();
305          int phase = phaser.arriveAndDeregister();
306          assertEquals(phase, phaser.getPhase());
307 +        t.join();
308      }
309  
310      /**
# Line 331 | Line 320 | public class PhaserTest extends JSR166Te
320       * phaser
321       */
322      public void testAwaitAdvance2() {
323 <        try {
324 <            Phaser phaser = new Phaser();
336 <            phaser.awaitAdvance(-1);
337 <        } catch (Exception failure) {
338 <            this.unexpectedException();
339 <        }
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();
347        Thread th1 = new Thread() {
348
349            public void run() {
350                try {
351                    phaser.register();
352                    getRunnable(LONG_DELAY_MS).run();
353                    phaser.awaitAdvance(phaser.arrive());
354                } catch (Exception failure) {
355                    threadUnexpectedException(failure);
356                }
357
358            }
359        };
332          phaser.register();
333 <        th1.start();
334 <        try {
335 <            Thread.sleep(SHORT_DELAY_MS);
336 <            th1.interrupt();
337 <            Thread.sleep(LONG_DELAY_MS);
338 <            phaser.arrive();
339 <        } catch (Exception failure) {
340 <            unexpectedException();
341 <        }
342 <        assertFalse(th1.isInterrupted());
333 >
334 >        Thread t = newStartedThread(new CheckedRunnable() {
335 >            public void realRun() throws InterruptedException {
336 >                phaser.register();
337 >                sleepTillInterrupted(LONG_DELAY_MS);
338 >                phaser.awaitAdvance(phaser.arrive());
339 >            }});
340 >        Thread.sleep(SMALL_DELAY_MS);
341 >        t.interrupt();
342 >        Thread.sleep(SMALL_DELAY_MS);
343 >        phaser.arrive();
344 >        assertFalse(t.isInterrupted());
345 >        t.join();
346      }
347  
348      /**
349       * awaitAdvance atomically waits for all parties within the same phase to
350       * complete before continuing
351       */
352 <    public void testAwaitAdvance4() {
353 <        final Phaser phaser = new Phaser(four);
352 >    public void testAwaitAdvance4() throws InterruptedException {
353 >        final Phaser phaser = new Phaser(4);
354          final AtomicInteger phaseCount = new AtomicInteger(0);
355 <        for (int i = 0; i < four; i++) {
356 <            new Thread() {
357 <
358 <                public void run() {
355 >        List<Thread> threads = new ArrayList<Thread>();
356 >        for (int i = 0; i < 4; i++) {
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 <                    assertTrue(phaseCount.get() == four);
364 <                }
390 <            }.start();
363 >                    assertEquals(phaseCount.get(), 4);
364 >                }}));
365          }
366 +        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() {
380 <
381 <                public void run() {
382 <                    getRunnable(SHORT_DELAY_MS).run();
378 >        List<Thread> threads = new ArrayList<Thread>();
379 >        for (int i = 0; i < 8; i++) {
380 >            threads.add(newStartedThread(new CheckedRunnable() {
381 >                public void realRun() {
382 >                    sleepTillInterrupted(SHORT_DELAY_MS);
383                      phaser.arrive();
384 <                }
409 <            }.start();
384 >                }}));
385              phase = phaser.awaitAdvance(phaser.arrive());
386              assertEquals(phase, phaser.getPhase());
387          }
388 +        for (Thread thread : threads)
389 +            thread.join();
390      }
391  
392      /**
393       * awaitAdvance returns when the phaser is externally terminated
394       */
395 <    public void testAwaitAdvance6() {
395 >    public void testAwaitAdvance6() throws InterruptedException {
396          final Phaser phaser = new Phaser(3);
397          /*
398           * Start new thread. This thread waits a small amount of time
# Line 423 | Line 400 | public class PhaserTest extends JSR166Te
400           * in the main thread arrives quickly so at best this thread
401           * waits for the second thread's party to arrive
402           */
403 <        new Thread() {
404 <
405 <            public void run() {
429 <                getRunnable(SMALL_DELAY_MS).run();
403 >        Thread t1 = newStartedThread(new CheckedRunnable() {
404 >            public void realRun() {
405 >                sleepTillInterrupted(SMALL_DELAY_MS);
406                  int phase = phaser.awaitAdvance(phaser.arrive());
407                  /*
408                   * This point is reached when force termination is called in which phase = -1
409                   */
410 <                threadAssertTrue(phase < 0);
411 <                threadAssertTrue(phaser.isTerminated());
412 <            }
437 <        }.start();
410 >                assertTrue(phase < 0);
411 >                assertTrue(phaser.isTerminated());
412 >            }});
413          /*
414           * This thread will cause the first thread run to wait, in doing so
415           * the main thread will force termination in which the first thread
416           * should exit peacefully as this one
417           */
418 <        new Thread() {
419 <
420 <            public void run() {
446 <                getRunnable(LONG_DELAY_MS).run();
418 >        Thread t2 = newStartedThread(new CheckedRunnable() {
419 >            public void realRun() {
420 >                sleepTillInterrupted(MEDIUM_DELAY_MS);
421                  int p1 = phaser.arrive();
422                  int phase = phaser.awaitAdvance(p1);
423 <                threadAssertTrue(phase < 0);
424 <                threadAssertTrue(phaser.isTerminated());
425 <            }
452 <        }.start();
423 >                assertTrue(phase < 0);
424 >                assertTrue(phaser.isTerminated());
425 >            }});
426  
427          phaser.arrive();
428          phaser.forceTermination();
429 +        t1.join();
430 +        t2.join();
431      }
432  
433      /**
# Line 464 | Line 439 | public class PhaserTest extends JSR166Te
439              Phaser phaser = new Phaser();
440              phaser.arriveAndAwaitAdvance();
441              shouldThrow();
442 <        } catch (IllegalStateException success) {
468 <        }
442 >        } catch (IllegalStateException success) {}
443      }
444  
445      /**
446       * Interrupted arriveAndAwaitAdvance does not throw InterruptedException
447       */
448 <    public void testArriveAndAwaitAdvance2() {
448 >    public void testArriveAndAwaitAdvance2() throws InterruptedException {
449          final Phaser phaser = new Phaser(2);
450 <        Thread th = new Thread() {
451 <            public void run() {
452 <                try {
453 <                    phaser.arriveAndAwaitAdvance();
454 <                } catch (Exception failure) {
455 <                    threadUnexpectedException(failure);
456 <                }
457 <            }
458 <        };
450 >        final CountDownLatch threadStarted = new CountDownLatch(1);
451 >        final AtomicBoolean advanced = new AtomicBoolean(false);
452 >        final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false);
453 >        Thread t = newStartedThread(new CheckedRunnable() {
454 >            public void realRun() throws InterruptedException {
455 >                threadStarted.countDown();
456 >                phaser.arriveAndAwaitAdvance();
457 >                advanced.set(true);
458 >                assertTrue(Thread.currentThread().isInterrupted());
459 >                while (!checkedInterruptStatus.get())
460 >                    Thread.yield();
461 >            }});
462  
463 <        try {
464 <            th.start();
465 <            Thread.sleep(LONG_DELAY_MS);
466 <            th.interrupt();
467 <            Thread.sleep(LONG_DELAY_MS);
468 <            phaser.arrive();
469 <        } catch (InterruptedException failure) {
470 <            this.unexpectedException();
494 <        }
495 <        assertFalse(th.isInterrupted());
463 >        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
464 >        t.interrupt();
465 >        phaser.arrive();
466 >        while (!advanced.get())
467 >            Thread.yield();
468 >        assertTrue(t.isInterrupted());
469 >        checkedInterruptStatus.set(true);
470 >        awaitTermination(t, SMALL_DELAY_MS);
471      }
472  
473      /**
# Line 500 | Line 475 | public class PhaserTest extends JSR166Te
475       * number of arrived parties is the same number that is accounted
476       * for when the main thread awaitsAdvance
477       */
478 <    public void testArriveAndAwaitAdvance3() {
478 >    public void testArriveAndAwaitAdvance3() throws InterruptedException {
479          final Phaser phaser = new Phaser(1);
480 <        final AtomicInteger arrivingCount = new AtomicInteger(0);
481 <        for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) {
482 <            new Thread() {
483 <
484 <                public void run() {
485 <                    phaser.register();
486 <                    run.run();
512 <                    arrivingCount.getAndIncrement();
513 <                    phaser.arrive();
514 <                }
515 <            }.start();
480 >        final List<Thread> threads = new ArrayList<Thread>();
481 >        for (int i = 0; i < 3; i++) {
482 >            threads.add(newStartedThread(new CheckedRunnable() {
483 >                    public void realRun() throws InterruptedException {
484 >                        phaser.register();
485 >                        phaser.arriveAndAwaitAdvance();
486 >                    }}));
487          }
488 <        int phaseNumber = phaser.arriveAndAwaitAdvance();
489 <        arrivingCount.incrementAndGet();
490 <        //the + 1 adds to expectedArrive to account for the main threads arrival
491 <        int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1;
492 <        threadAssertEquals(expectedArrived, arrivingCount.get());
522 <    }
523 <    // .. initially called, for n tasks via
524 <    private List<Runnable> getRunnables(int size, long wait) {
525 <        List<Runnable> list = new ArrayList<Runnable>();
526 <        for (int i = 0; i < size; i++) {
527 <            list.add(getRunnable(wait));
528 <        }
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 <        };
488 >        Thread.sleep(MEDIUM_DELAY_MS);
489 >        assertEquals(phaser.getArrivedParties(), 3);
490 >        phaser.arriveAndAwaitAdvance();
491 >        for (Thread thread : threads)
492 >            thread.join();
493      }
494  
495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines