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.73 by jsr166, Tue Oct 6 06:02:53 2015 UTC

# Line 42 | Line 42 | public class ScheduledExecutorTest exten
42       * execute successfully executes a runnable
43       */
44      public void testExecute() throws InterruptedException {
45 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
45 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
46          try (PoolCleaner cleaner = cleaner(p)) {
47              final CountDownLatch done = new CountDownLatch(1);
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 56 | Line 56 | public class ScheduledExecutorTest exten
56       * delayed schedule of callable successfully executes after delay
57       */
58      public void testSchedule1() throws Exception {
59 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
59 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
60          try (PoolCleaner cleaner = cleaner(p)) {
61              final long startTime = System.nanoTime();
62              final CountDownLatch done = new CountDownLatch(1);
# Line 77 | Line 77 | public class ScheduledExecutorTest exten
77       * delayed schedule of runnable successfully executes after delay
78       */
79      public void testSchedule3() throws Exception {
80 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
80 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
81          try (PoolCleaner cleaner = cleaner(p)) {
82              final long startTime = System.nanoTime();
83              final CountDownLatch done = new CountDownLatch(1);
# Line 97 | Line 97 | public class ScheduledExecutorTest exten
97       * scheduleAtFixedRate executes runnable after given initial delay
98       */
99      public void testSchedule4() throws Exception {
100 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
100 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
101          try (PoolCleaner cleaner = cleaner(p)) {
102              final long startTime = System.nanoTime();
103              final CountDownLatch done = new CountDownLatch(1);
# Line 119 | Line 119 | public class ScheduledExecutorTest exten
119       * scheduleWithFixedDelay executes runnable after given initial delay
120       */
121      public void testSchedule5() throws Exception {
122 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
122 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
123          try (PoolCleaner cleaner = cleaner(p)) {
124              final long startTime = System.nanoTime();
125              final CountDownLatch done = new CountDownLatch(1);
# Line 146 | Line 146 | public class ScheduledExecutorTest exten
146       * scheduleAtFixedRate executes series of tasks at given rate
147       */
148      public void testFixedRateSequence() throws InterruptedException {
149 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
149 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
150          try (PoolCleaner cleaner = cleaner(p)) {
151              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
152                  long startTime = System.nanoTime();
# 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 172 | Line 172 | public class ScheduledExecutorTest exten
172       * scheduleWithFixedDelay executes series of tasks with given period
173       */
174      public void testFixedDelaySequence() throws InterruptedException {
175 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
175 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
176          try (PoolCleaner cleaner = cleaner(p)) {
177              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
178                  long startTime = System.nanoTime();
# 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 198 | Line 198 | public class ScheduledExecutorTest exten
198       * execute(null) throws NPE
199       */
200      public void testExecuteNull() throws InterruptedException {
201 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
201 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
202          try (PoolCleaner cleaner = cleaner(p)) {
203              try {
204                  p.execute(null);
# Line 225 | Line 225 | public class ScheduledExecutorTest exten
225       * execute throws RejectedExecutionException if shutdown
226       */
227      public void testSchedule1_RejectedExecutionException() throws InterruptedException {
228 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
228 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
229          try (PoolCleaner cleaner = cleaner(p)) {
230              try {
231                  p.shutdown();
# Line 241 | Line 241 | public class ScheduledExecutorTest exten
241       * schedule throws RejectedExecutionException if shutdown
242       */
243      public void testSchedule2_RejectedExecutionException() throws InterruptedException {
244 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
244 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
245          try (PoolCleaner cleaner = cleaner(p)) {
246              try {
247                  p.shutdown();
# Line 257 | Line 257 | public class ScheduledExecutorTest exten
257       * schedule callable throws RejectedExecutionException if shutdown
258       */
259      public void testSchedule3_RejectedExecutionException() throws InterruptedException {
260 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
260 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
261          try (PoolCleaner cleaner = cleaner(p)) {
262              try {
263                  p.shutdown();
# Line 273 | Line 273 | public class ScheduledExecutorTest exten
273       * scheduleAtFixedRate throws RejectedExecutionException if shutdown
274       */
275      public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
276 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
276 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
277          try (PoolCleaner cleaner = cleaner(p)) {
278              try {
279                  p.shutdown();
# Line 289 | Line 289 | public class ScheduledExecutorTest exten
289       * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
290       */
291      public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
292 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
292 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
293          try (PoolCleaner cleaner = cleaner(p)) {
294              try {
295                  p.shutdown();
# Line 306 | Line 306 | public class ScheduledExecutorTest exten
306       * thread becomes active
307       */
308      public void testGetActiveCount() throws InterruptedException {
309 +        final CountDownLatch done = new CountDownLatch(1);
310          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
311 <        try (PoolCleaner cleaner = cleaner(p)) {
311 >        try (PoolCleaner cleaner = cleaner(p, done)) {
312              final CountDownLatch threadStarted = new CountDownLatch(1);
312            final CountDownLatch done = new CountDownLatch(1);
313              assertEquals(0, p.getActiveCount());
314              p.execute(new CheckedRunnable() {
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();
322          }
323      }
324  
# Line 373 | Line 372 | public class ScheduledExecutorTest exten
372          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS);
373          final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
374          final CountDownLatch done = new CountDownLatch(1);
375 <        try (PoolCleaner cleaner = cleaner(p)) {
375 >        try (PoolCleaner cleaner = cleaner(p, done)) {
376              assertEquals(0, p.getLargestPoolSize());
377              for (int i = 0; i < THREADS; i++)
378                  p.execute(new CheckedRunnable() {
379                      public void realRun() throws InterruptedException {
380                          threadsStarted.countDown();
381 <                        done.await();
381 >                        await(done);
382                          assertEquals(THREADS, p.getLargestPoolSize());
383                      }});
384 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
384 >            await(threadsStarted);
385              assertEquals(THREADS, p.getLargestPoolSize());
387            done.countDown();
386          }
387          assertEquals(THREADS, p.getLargestPoolSize());
388      }
# Line 397 | Line 395 | public class ScheduledExecutorTest exten
395          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
396          final CountDownLatch threadStarted = new CountDownLatch(1);
397          final CountDownLatch done = new CountDownLatch(1);
398 <        try (PoolCleaner cleaner = cleaner(p)) {
398 >        try (PoolCleaner cleaner = cleaner(p, done)) {
399              assertEquals(0, p.getPoolSize());
400              p.execute(new CheckedRunnable() {
401                  public void realRun() throws InterruptedException {
402                      threadStarted.countDown();
403                      assertEquals(1, p.getPoolSize());
404 <                    done.await();
404 >                    await(done);
405                  }});
406 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
406 >            await(threadStarted);
407              assertEquals(1, p.getPoolSize());
410            done.countDown();
408          }
409      }
410  
# Line 416 | Line 413 | public class ScheduledExecutorTest exten
413       * submitted
414       */
415      public void testGetTaskCount() throws InterruptedException {
416 +        final int TASKS = 3;
417 +        final CountDownLatch done = new CountDownLatch(1);
418          final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
419 <        try (PoolCleaner cleaner = cleaner(p)) {
419 >        try (PoolCleaner cleaner = cleaner(p, done)) {
420              final CountDownLatch threadStarted = new CountDownLatch(1);
422            final CountDownLatch done = new CountDownLatch(1);
423            final int TASKS = 5;
421              assertEquals(0, p.getTaskCount());
422 <            for (int i = 0; i < TASKS; i++)
422 >            assertEquals(0, p.getCompletedTaskCount());
423 >            p.execute(new CheckedRunnable() {
424 >                public void realRun() throws InterruptedException {
425 >                    threadStarted.countDown();
426 >                    await(done);
427 >                }});
428 >            await(threadStarted);
429 >            assertEquals(1, p.getTaskCount());
430 >            assertEquals(0, p.getCompletedTaskCount());
431 >            for (int i = 0; i < TASKS; i++) {
432 >                assertEquals(1 + i, p.getTaskCount());
433                  p.execute(new CheckedRunnable() {
434                      public void realRun() throws InterruptedException {
435                          threadStarted.countDown();
436 <                        done.await();
436 >                        assertEquals(1 + TASKS, p.getTaskCount());
437 >                        await(done);
438                      }});
439 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
440 <            assertEquals(TASKS, p.getTaskCount());
441 <            done.countDown();
439 >            }
440 >            assertEquals(1 + TASKS, p.getTaskCount());
441 >            assertEquals(0, p.getCompletedTaskCount());
442          }
443 +        assertEquals(1 + TASKS, p.getTaskCount());
444 +        assertEquals(1 + TASKS, p.getCompletedTaskCount());
445      }
446  
447      /**
448       * getThreadFactory returns factory in constructor if not set
449       */
450      public void testGetThreadFactory() throws InterruptedException {
451 <        ThreadFactory threadFactory = new SimpleThreadFactory();
452 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, threadFactory);
453 <        assertSame(threadFactory, p.getThreadFactory());
454 <        joinPool(p);
451 >        final ThreadFactory threadFactory = new SimpleThreadFactory();
452 >        final ScheduledThreadPoolExecutor p =
453 >            new ScheduledThreadPoolExecutor(1, threadFactory);
454 >        try (PoolCleaner cleaner = cleaner(p)) {
455 >            assertSame(threadFactory, p.getThreadFactory());
456 >        }
457      }
458  
459      /**
# Line 449 | Line 461 | public class ScheduledExecutorTest exten
461       */
462      public void testSetThreadFactory() throws InterruptedException {
463          ThreadFactory threadFactory = new SimpleThreadFactory();
464 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
464 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
465          try (PoolCleaner cleaner = cleaner(p)) {
466              p.setThreadFactory(threadFactory);
467              assertSame(threadFactory, p.getThreadFactory());
# Line 460 | Line 472 | public class ScheduledExecutorTest exten
472       * setThreadFactory(null) throws NPE
473       */
474      public void testSetThreadFactoryNull() throws InterruptedException {
475 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
475 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
476          try (PoolCleaner cleaner = cleaner(p)) {
477              try {
478                  p.setThreadFactory(null);
# Line 474 | Line 486 | public class ScheduledExecutorTest exten
486       */
487      public void testIsShutdown() {
488  
489 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
489 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
490          try {
491              assertFalse(p.isShutdown());
492          }
# Line 497 | Line 509 | public class ScheduledExecutorTest exten
509                  public void realRun() throws InterruptedException {
510                      assertFalse(p.isTerminated());
511                      threadStarted.countDown();
512 <                    done.await();
512 >                    await(done);
513                  }});
514 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
514 >            await(threadStarted);
515              assertFalse(p.isTerminating());
516              done.countDown();
517              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 521 | Line 533 | public class ScheduledExecutorTest exten
533                  public void realRun() throws InterruptedException {
534                      assertFalse(p.isTerminating());
535                      threadStarted.countDown();
536 <                    done.await();
536 >                    await(done);
537                  }});
538 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
538 >            await(threadStarted);
539              assertFalse(p.isTerminating());
540              done.countDown();
541              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 537 | Line 549 | public class ScheduledExecutorTest exten
549       * getQueue returns the work queue, which contains queued tasks
550       */
551      public void testGetQueue() throws InterruptedException {
552 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
553 <        try (PoolCleaner cleaner = cleaner(p)) {
552 >        final CountDownLatch done = new CountDownLatch(1);
553 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
554 >        try (PoolCleaner cleaner = cleaner(p, done)) {
555              final CountDownLatch threadStarted = new CountDownLatch(1);
543            final CountDownLatch done = new CountDownLatch(1);
556              ScheduledFuture[] tasks = new ScheduledFuture[5];
557              for (int i = 0; i < tasks.length; i++) {
558                  Runnable r = new CheckedRunnable() {
559                      public void realRun() throws InterruptedException {
560                          threadStarted.countDown();
561 <                        done.await();
561 >                        await(done);
562                      }};
563                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
564              }
565 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
565 >            await(threadStarted);
566              BlockingQueue<Runnable> q = p.getQueue();
567              assertTrue(q.contains(tasks[tasks.length - 1]));
568              assertFalse(q.contains(tasks[0]));
557            done.countDown();
569          }
570      }
571  
# Line 562 | Line 573 | public class ScheduledExecutorTest exten
573       * remove(task) removes queued task, and fails to remove active task
574       */
575      public void testRemove() throws InterruptedException {
576 +        final CountDownLatch done = new CountDownLatch(1);
577          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
578 <        try (PoolCleaner cleaner = cleaner(p)) {
578 >        try (PoolCleaner cleaner = cleaner(p, done)) {
579              ScheduledFuture[] tasks = new ScheduledFuture[5];
580              final CountDownLatch threadStarted = new CountDownLatch(1);
569            final CountDownLatch done = new CountDownLatch(1);
581              for (int i = 0; i < tasks.length; i++) {
582                  Runnable r = new CheckedRunnable() {
583                      public void realRun() throws InterruptedException {
584                          threadStarted.countDown();
585 <                        done.await();
585 >                        await(done);
586                      }};
587                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
588              }
589 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
589 >            await(threadStarted);
590              BlockingQueue<Runnable> q = p.getQueue();
591              assertFalse(p.remove((Runnable)tasks[0]));
592              assertTrue(q.contains((Runnable)tasks[4]));
# Line 586 | Line 597 | public class ScheduledExecutorTest exten
597              assertTrue(q.contains((Runnable)tasks[3]));
598              assertTrue(p.remove((Runnable)tasks[3]));
599              assertFalse(q.contains((Runnable)tasks[3]));
589            done.countDown();
600          }
601      }
602  
# Line 594 | Line 604 | public class ScheduledExecutorTest exten
604       * purge eventually removes cancelled tasks from the queue
605       */
606      public void testPurge() throws InterruptedException {
607 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
608 <        ScheduledFuture[] tasks = new ScheduledFuture[5];
609 <        for (int i = 0; i < tasks.length; i++)
610 <            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
611 <                                  LONG_DELAY_MS, MILLISECONDS);
612 <        try {
607 >        final ScheduledFuture[] tasks = new ScheduledFuture[5];
608 >        final Runnable releaser = new Runnable() { public void run() {
609 >            for (ScheduledFuture task : tasks)
610 >                if (task != null) task.cancel(true); }};
611 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
612 >        try (PoolCleaner cleaner = cleaner(p, releaser)) {
613 >            for (int i = 0; i < tasks.length; i++)
614 >                tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
615 >                                      LONG_DELAY_MS, MILLISECONDS);
616              int max = tasks.length;
617              if (tasks[4].cancel(true)) --max;
618              if (tasks[3].cancel(true)) --max;
# Line 611 | Line 624 | public class ScheduledExecutorTest exten
624                  long count = p.getTaskCount();
625                  if (count == max)
626                      return;
627 <            } while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
627 >            } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
628              fail("Purge failed to remove cancelled tasks");
616        } finally {
617            for (ScheduledFuture task : tasks)
618                task.cancel(true);
619            joinPool(p);
629          }
630      }
631  
# Line 630 | Line 639 | public class ScheduledExecutorTest exten
639          final AtomicInteger ran = new AtomicInteger(0);
640          final ScheduledThreadPoolExecutor p =
641              new ScheduledThreadPoolExecutor(poolSize);
642 <        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
642 >        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
643          Runnable waiter = new CheckedRunnable() { public void realRun() {
644              threadsStarted.countDown();
645              try {
# Line 640 | Line 649 | public class ScheduledExecutorTest exten
649          }};
650          for (int i = 0; i < count; i++)
651              p.execute(waiter);
652 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
652 >        await(threadsStarted);
653          assertEquals(poolSize, p.getActiveCount());
654          assertEquals(0, p.getCompletedTaskCount());
655          final List<Runnable> queuedTasks;
# Line 663 | Line 672 | public class ScheduledExecutorTest exten
672       * and those tasks are drained from the queue
673       */
674      public void testShutdownNow_delayedTasks() throws InterruptedException {
675 <        ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
675 >        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
676          List<ScheduledFuture> tasks = new ArrayList<>();
677          for (int i = 0; i < 3; i++) {
678              Runnable r = new NoOpRunnable();
# Line 1127 | Line 1136 | public class ScheduledExecutorTest exten
1136              List<Callable<String>> l = new ArrayList<Callable<String>>();
1137              l.add(new NPETask());
1138              List<Future<String>> futures =
1139 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1139 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1140              assertEquals(1, futures.size());
1141              try {
1142                  futures.get(0).get();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines