ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.62 by jsr166, Sun Oct 4 08:07:31 2015 UTC vs.
Revision 1.72 by jsr166, Tue Oct 6 05:30:44 2015 UTC

# Line 48 | Line 48 | public class ScheduledExecutorTest exten
48              final Runnable task = new CheckedRunnable() {
49                  public void realRun() { done.countDown(); }};
50              p.execute(task);
51 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
51 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
52          }
53      }
54  
# Line 156 | Line 156 | public class ScheduledExecutorTest exten
156                      public void realRun() { done.countDown(); }};
157                  ScheduledFuture h =
158                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
159 <                done.await();
159 >                await(done);
160                  h.cancel(true);
161                  double normalizedTime =
162                      (double) millisElapsedSince(startTime) / delay;
# Line 182 | Line 182 | public class ScheduledExecutorTest exten
182                      public void realRun() { done.countDown(); }};
183                  ScheduledFuture h =
184                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
185 <                done.await();
185 >                await(done);
186                  h.cancel(true);
187                  double normalizedTime =
188                      (double) millisElapsedSince(startTime) / delay;
# Line 315 | Line 315 | public class ScheduledExecutorTest exten
315                  public void realRun() throws InterruptedException {
316                      threadStarted.countDown();
317                      assertEquals(1, p.getActiveCount());
318 <                    done.await();
318 >                    await(done);
319                  }});
320 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
320 >            await(threadStarted);
321              assertEquals(1, p.getActiveCount());
322              done.countDown();
323          }
# Line 373 | Line 373 | public class ScheduledExecutorTest exten
373          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
374          final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
375          final CountDownLatch done = new CountDownLatch(1);
376 <        try (PoolCleaner cleaner = cleaner(p)) {
376 >        try (PoolCleaner cleaner = cleaner(p, done)) {
377              assertEquals(0, p.getLargestPoolSize());
378              for (int i = 0; i < THREADS; i++)
379                  p.execute(new CheckedRunnable() {
380                      public void realRun() throws InterruptedException {
381                          threadsStarted.countDown();
382 <                        done.await();
382 >                        await(done);
383                          assertEquals(THREADS, p.getLargestPoolSize());
384                      }});
385 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
385 >            await(threadsStarted);
386              assertEquals(THREADS, p.getLargestPoolSize());
387            done.countDown();
387          }
388          assertEquals(THREADS, p.getLargestPoolSize());
389      }
# Line 397 | Line 396 | public class ScheduledExecutorTest exten
396          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
397          final CountDownLatch threadStarted = new CountDownLatch(1);
398          final CountDownLatch done = new CountDownLatch(1);
399 <        try (PoolCleaner cleaner = cleaner(p)) {
399 >        try (PoolCleaner cleaner = cleaner(p, done)) {
400              assertEquals(0, p.getPoolSize());
401              p.execute(new CheckedRunnable() {
402                  public void realRun() throws InterruptedException {
403                      threadStarted.countDown();
404                      assertEquals(1, p.getPoolSize());
405 <                    done.await();
405 >                    await(done);
406                  }});
407 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
407 >            await(threadStarted);
408              assertEquals(1, p.getPoolSize());
410            done.countDown();
409          }
410      }
411  
# Line 416 | Line 414 | public class ScheduledExecutorTest exten
414       * submitted
415       */
416      public void testGetTaskCount() throws InterruptedException {
417 +        final int TASKS = 3;
418 +        final CountDownLatch done = new CountDownLatch(1);
419          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
420 <        try (PoolCleaner cleaner = cleaner(p)) {
420 >        try (PoolCleaner cleaner = cleaner(p, done)) {
421              final CountDownLatch threadStarted = new CountDownLatch(1);
422            final CountDownLatch done = new CountDownLatch(1);
423            final int TASKS = 5;
422              assertEquals(0, p.getTaskCount());
423 <            for (int i = 0; i < TASKS; i++)
423 >            assertEquals(0, p.getCompletedTaskCount());
424 >            p.execute(new CheckedRunnable() {
425 >                public void realRun() throws InterruptedException {
426 >                    threadStarted.countDown();
427 >                    await(done);
428 >                }});
429 >            await(threadStarted);
430 >            assertEquals(1, p.getTaskCount());
431 >            assertEquals(0, p.getCompletedTaskCount());
432 >            for (int i = 0; i < TASKS; i++) {
433 >                assertEquals(1 + i, p.getTaskCount());
434                  p.execute(new CheckedRunnable() {
435                      public void realRun() throws InterruptedException {
436                          threadStarted.countDown();
437 <                        done.await();
437 >                        assertEquals(1 + TASKS, p.getTaskCount());
438 >                        await(done);
439                      }});
440 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
441 <            assertEquals(TASKS, p.getTaskCount());
442 <            done.countDown();
440 >            }
441 >            assertEquals(1 + TASKS, p.getTaskCount());
442 >            assertEquals(0, p.getCompletedTaskCount());
443          }
444 +        assertEquals(1 + TASKS, p.getTaskCount());
445 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
446      }
447  
448      /**
449       * getThreadFactory returns factory in constructor if not set
450       */
451      public void testGetThreadFactory() throws InterruptedException {
452 <        ThreadFactory threadFactory = new SimpleThreadFactory();
453 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, threadFactory);
454 <        assertSame(threadFactory, p.getThreadFactory());
455 <        joinPool(p);
452 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
453 >        final ScheduledThreadPoolExecutor p =
454 >            new ScheduledThreadPoolExecutor(1, threadFactory);
455 >        try (PoolCleaner cleaner = cleaner(p)) {
456 >            assertSame(threadFactory, p.getThreadFactory());
457 >        }
458      }
459  
460      /**
# Line 497 | Line 510 | public class ScheduledExecutorTest exten
510                  public void realRun() throws InterruptedException {
511                      assertFalse(p.isTerminated());
512                      threadStarted.countDown();
513 <                    done.await();
513 >                    await(done);
514                  }});
515 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
515 >            await(threadStarted);
516              assertFalse(p.isTerminating());
517              done.countDown();
518              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 521 | Line 534 | public class ScheduledExecutorTest exten
534                  public void realRun() throws InterruptedException {
535                      assertFalse(p.isTerminating());
536                      threadStarted.countDown();
537 <                    done.await();
537 >                    await(done);
538                  }});
539 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
539 >            await(threadStarted);
540              assertFalse(p.isTerminating());
541              done.countDown();
542              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 546 | Line 559 | public class ScheduledExecutorTest exten
559                  Runnable r = new CheckedRunnable() {
560                      public void realRun() throws InterruptedException {
561                          threadStarted.countDown();
562 <                        done.await();
562 >                        await(done);
563                      }};
564                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
565              }
566 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
566 >            await(threadStarted);
567              BlockingQueue<Runnable> q = p.getQueue();
568              assertTrue(q.contains(tasks[tasks.length - 1]));
569              assertFalse(q.contains(tasks[0]));
# Line 571 | Line 584 | public class ScheduledExecutorTest exten
584                  Runnable r = new CheckedRunnable() {
585                      public void realRun() throws InterruptedException {
586                          threadStarted.countDown();
587 <                        done.await();
587 >                        await(done);
588                      }};
589                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
590              }
591 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
591 >            await(threadStarted);
592              BlockingQueue<Runnable> q = p.getQueue();
593              assertFalse(p.remove((Runnable)tasks[0]));
594              assertTrue(q.contains((Runnable)tasks[4]));
# Line 594 | Line 607 | public class ScheduledExecutorTest exten
607       * purge eventually removes cancelled tasks from the queue
608       */
609      public void testPurge() throws InterruptedException {
610 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
611 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
612 <        for (int i = 0; i < tasks.length; i++)
613 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
614 <                                  LONG_DELAY_MS, MILLISECONDS);
615 <        try {
610 >        final ScheduledFuture[] tasks = new ScheduledFuture[5];
611 >        final Runnable releaser = new Runnable() { public void run() {
612 >            for (ScheduledFuture task : tasks)
613 >                if (task != null) task.cancel(true); }};
614 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
615 >        try (PoolCleaner cleaner = cleaner(p, releaser)) {
616 >            for (int i = 0; i < tasks.length; i++)
617 >                tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
618 >                                      LONG_DELAY_MS, MILLISECONDS);
619              int max = tasks.length;
620              if (tasks[4].cancel(true)) --max;
621              if (tasks[3].cancel(true)) --max;
# Line 611 | Line 627 | public class ScheduledExecutorTest exten
627                  long count = p.getTaskCount();
628                  if (count == max)
629                      return;
630 <            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
630 >            } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
631              fail("Purge failed to remove cancelled tasks");
616        } finally {
617            for (ScheduledFuture task : tasks)
618                task.cancel(true);
619            joinPool(p);
632          }
633      }
634  
# Line 630 | Line 642 | public class ScheduledExecutorTest exten
642          final AtomicInteger ran = new AtomicInteger(0);
643          final ScheduledThreadPoolExecutor p =
644              new ScheduledThreadPoolExecutor(poolSize);
645 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
645 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
646          Runnable waiter = new CheckedRunnable() { public void realRun() {
647              threadsStarted.countDown();
648              try {
# Line 640 | Line 652 | public class ScheduledExecutorTest exten
652          }};
653          for (int i = 0; i < count; i++)
654              p.execute(waiter);
655 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
655 >        await(threadsStarted);
656          assertEquals(poolSize, p.getActiveCount());
657          assertEquals(0, p.getCompletedTaskCount());
658          final List<Runnable> queuedTasks;
# Line 1127 | Line 1139 | public class ScheduledExecutorTest exten
1139              List<Callable<String>> l = new ArrayList<Callable<String>>();
1140              l.add(new NPETask());
1141              List<Future<String>> futures =
1142 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1142 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1143              assertEquals(1, futures.size());
1144              try {
1145                  futures.get(0).get();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines