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

Comparing jsr166/src/test/tck/ScheduledExecutorSubclassTest.java (file contents):
Revision 1.49 by jsr166, Mon Oct 5 21:54:33 2015 UTC vs.
Revision 1.60 by jsr166, Mon Feb 22 20:40:11 2016 UTC

# Line 105 | Line 105 | public class ScheduledExecutorSubclassTe
105              final Runnable task = new CheckedRunnable() {
106                  public void realRun() { done.countDown(); }};
107              p.execute(task);
108 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
108 >            await(done);
109          }
110      }
111  
# Line 113 | Line 113 | public class ScheduledExecutorSubclassTe
113       * delayed schedule of callable successfully executes after delay
114       */
115      public void testSchedule1() throws Exception {
116 +        final CountDownLatch done = new CountDownLatch(1);
117          final CustomExecutor p = new CustomExecutor(1);
118 <        try (PoolCleaner cleaner = cleaner(p)) {
118 >        try (PoolCleaner cleaner = cleaner(p, done)) {
119              final long startTime = System.nanoTime();
119            final CountDownLatch done = new CountDownLatch(1);
120              Callable task = new CheckedCallable<Boolean>() {
121                  public Boolean realCall() {
122                      done.countDown();
# Line 126 | Line 126 | public class ScheduledExecutorSubclassTe
126              Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
127              assertSame(Boolean.TRUE, f.get());
128              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
129            assertTrue(done.await(0L, MILLISECONDS));
129          }
130      }
131  
# Line 211 | Line 210 | public class ScheduledExecutorSubclassTe
210                  final CountDownLatch done = new CountDownLatch(cycles);
211                  Runnable task = new CheckedRunnable() {
212                      public void realRun() { done.countDown(); }};
213 <                ScheduledFuture h =
213 >                ScheduledFuture periodicTask =
214                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
215 <                done.await();
216 <                h.cancel(true);
217 <                double normalizedTime =
215 >                await(done);
216 >                periodicTask.cancel(true);
217 >                double elapsedDelays =
218                      (double) millisElapsedSince(startTime) / delay;
219 <                if (normalizedTime >= cycles - 1 &&
220 <                    normalizedTime <= cycles)
219 >                if (elapsedDelays >= cycles - 1 &&
220 >                    elapsedDelays <= cycles)
221                      return;
222              }
223 <            throw new AssertionError("unexpected execution rate");
223 >            fail("unexpected execution rate");
224          }
225      }
226  
# Line 237 | Line 236 | public class ScheduledExecutorSubclassTe
236                  final CountDownLatch done = new CountDownLatch(cycles);
237                  Runnable task = new CheckedRunnable() {
238                      public void realRun() { done.countDown(); }};
239 <                ScheduledFuture h =
239 >                ScheduledFuture periodicTask =
240                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
241 <                done.await();
242 <                h.cancel(true);
243 <                double normalizedTime =
241 >                await(done);
242 >                periodicTask.cancel(true);
243 >                double elapsedDelays =
244                      (double) millisElapsedSince(startTime) / delay;
245 <                if (normalizedTime >= cycles - 1 &&
246 <                    normalizedTime <= cycles)
245 >                if (elapsedDelays >= cycles - 1 &&
246 >                    elapsedDelays <= cycles)
247                      return;
248              }
249 <            throw new AssertionError("unexpected execution rate");
249 >            fail("unexpected execution rate");
250          }
251      }
252  
# Line 363 | Line 362 | public class ScheduledExecutorSubclassTe
362       * thread becomes active
363       */
364      public void testGetActiveCount() throws InterruptedException {
365 +        final CountDownLatch done = new CountDownLatch(1);
366          final ThreadPoolExecutor p = new CustomExecutor(2);
367 <        try (PoolCleaner cleaner = cleaner(p)) {
367 >        try (PoolCleaner cleaner = cleaner(p, done)) {
368              final CountDownLatch threadStarted = new CountDownLatch(1);
369            final CountDownLatch done = new CountDownLatch(1);
369              assertEquals(0, p.getActiveCount());
370              p.execute(new CheckedRunnable() {
371                  public void realRun() throws InterruptedException {
372                      threadStarted.countDown();
373                      assertEquals(1, p.getActiveCount());
374 <                    done.await();
374 >                    await(done);
375                  }});
376 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
376 >            await(threadStarted);
377              assertEquals(1, p.getActiveCount());
379            done.countDown();
378          }
379      }
380  
# Line 427 | Line 425 | public class ScheduledExecutorSubclassTe
425       */
426      public void testGetLargestPoolSize() throws InterruptedException {
427          final int THREADS = 3;
428 +        final CountDownLatch done = new CountDownLatch(1);
429          final ThreadPoolExecutor p = new CustomExecutor(THREADS);
430 <        try (PoolCleaner cleaner = cleaner(p)) {
430 >        try (PoolCleaner cleaner = cleaner(p, done)) {
431              final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
433            final CountDownLatch done = new CountDownLatch(1);
432              assertEquals(0, p.getLargestPoolSize());
433              for (int i = 0; i < THREADS; i++)
434                  p.execute(new CheckedRunnable() {
435                      public void realRun() throws InterruptedException {
436                          threadsStarted.countDown();
437 <                        done.await();
437 >                        await(done);
438                          assertEquals(THREADS, p.getLargestPoolSize());
439                      }});
440 <            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
440 >            await(threadsStarted);
441              assertEquals(THREADS, p.getLargestPoolSize());
444            done.countDown();
442          }
443          assertEquals(THREADS, p.getLargestPoolSize());
444      }
# Line 451 | Line 448 | public class ScheduledExecutorSubclassTe
448       * become active
449       */
450      public void testGetPoolSize() throws InterruptedException {
451 +        final CountDownLatch done = new CountDownLatch(1);
452          final ThreadPoolExecutor p = new CustomExecutor(1);
453 <        try (PoolCleaner cleaner = cleaner(p)) {
453 >        try (PoolCleaner cleaner = cleaner(p, done)) {
454              final CountDownLatch threadStarted = new CountDownLatch(1);
457            final CountDownLatch done = new CountDownLatch(1);
455              assertEquals(0, p.getPoolSize());
456              p.execute(new CheckedRunnable() {
457                  public void realRun() throws InterruptedException {
458                      threadStarted.countDown();
459                      assertEquals(1, p.getPoolSize());
460 <                    done.await();
460 >                    await(done);
461                  }});
462 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
462 >            await(threadStarted);
463              assertEquals(1, p.getPoolSize());
467            done.countDown();
464          }
465      }
466  
# Line 483 | Line 479 | public class ScheduledExecutorSubclassTe
479              p.execute(new CheckedRunnable() {
480                  public void realRun() throws InterruptedException {
481                      threadStarted.countDown();
482 <                    done.await();
482 >                    await(done);
483                  }});
484 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
484 >            await(threadStarted);
485              assertEquals(1, p.getTaskCount());
486              assertEquals(0, p.getCompletedTaskCount());
487              for (int i = 0; i < TASKS; i++) {
# Line 494 | Line 490 | public class ScheduledExecutorSubclassTe
490                      public void realRun() throws InterruptedException {
491                          threadStarted.countDown();
492                          assertEquals(1 + TASKS, p.getTaskCount());
493 <                        done.await();
493 >                        await(done);
494                      }});
495              }
496              assertEquals(1 + TASKS, p.getTaskCount());
# Line 556 | Line 552 | public class ScheduledExecutorSubclassTe
552       * isTerminated is false before termination, true after
553       */
554      public void testIsTerminated() throws InterruptedException {
555 +        final CountDownLatch done = new CountDownLatch(1);
556          final ThreadPoolExecutor p = new CustomExecutor(1);
557          try (PoolCleaner cleaner = cleaner(p)) {
558              final CountDownLatch threadStarted = new CountDownLatch(1);
562            final CountDownLatch done = new CountDownLatch(1);
563            assertFalse(p.isTerminated());
559              p.execute(new CheckedRunnable() {
560                  public void realRun() throws InterruptedException {
561                      assertFalse(p.isTerminated());
562                      threadStarted.countDown();
563 <                    done.await();
563 >                    await(done);
564                  }});
565 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
565 >            await(threadStarted);
566 >            assertFalse(p.isTerminated());
567              assertFalse(p.isTerminating());
568              done.countDown();
569              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 580 | Line 576 | public class ScheduledExecutorSubclassTe
576       * isTerminating is not true when running or when terminated
577       */
578      public void testIsTerminating() throws InterruptedException {
579 +        final CountDownLatch done = new CountDownLatch(1);
580          final ThreadPoolExecutor p = new CustomExecutor(1);
581          try (PoolCleaner cleaner = cleaner(p)) {
582              final CountDownLatch threadStarted = new CountDownLatch(1);
586            final CountDownLatch done = new CountDownLatch(1);
583              assertFalse(p.isTerminating());
584              p.execute(new CheckedRunnable() {
585                  public void realRun() throws InterruptedException {
586                      assertFalse(p.isTerminating());
587                      threadStarted.countDown();
588 <                    done.await();
588 >                    await(done);
589                  }});
590 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
590 >            await(threadStarted);
591              assertFalse(p.isTerminating());
592              done.countDown();
593              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 605 | Line 601 | public class ScheduledExecutorSubclassTe
601       * getQueue returns the work queue, which contains queued tasks
602       */
603      public void testGetQueue() throws InterruptedException {
604 +        final CountDownLatch done = new CountDownLatch(1);
605          final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
606 <        try (PoolCleaner cleaner = cleaner(p)) {
606 >        try (PoolCleaner cleaner = cleaner(p, done)) {
607              final CountDownLatch threadStarted = new CountDownLatch(1);
611            final CountDownLatch done = new CountDownLatch(1);
608              ScheduledFuture[] tasks = new ScheduledFuture[5];
609              for (int i = 0; i < tasks.length; i++) {
610                  Runnable r = new CheckedRunnable() {
611                      public void realRun() throws InterruptedException {
612                          threadStarted.countDown();
613 <                        done.await();
613 >                        await(done);
614                      }};
615                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
616              }
617 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
617 >            await(threadStarted);
618              BlockingQueue<Runnable> q = p.getQueue();
619              assertTrue(q.contains(tasks[tasks.length - 1]));
620              assertFalse(q.contains(tasks[0]));
625            done.countDown();
621          }
622      }
623  
# Line 630 | Line 625 | public class ScheduledExecutorSubclassTe
625       * remove(task) removes queued task, and fails to remove active task
626       */
627      public void testRemove() throws InterruptedException {
628 +        final CountDownLatch done = new CountDownLatch(1);
629          final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
630 <        try (PoolCleaner cleaner = cleaner(p)) {
630 >        try (PoolCleaner cleaner = cleaner(p, done)) {
631              ScheduledFuture[] tasks = new ScheduledFuture[5];
632              final CountDownLatch threadStarted = new CountDownLatch(1);
637            final CountDownLatch done = new CountDownLatch(1);
633              for (int i = 0; i < tasks.length; i++) {
634                  Runnable r = new CheckedRunnable() {
635                      public void realRun() throws InterruptedException {
636                          threadStarted.countDown();
637 <                        done.await();
637 >                        await(done);
638                      }};
639                  tasks[i] = p.schedule(r, 1, MILLISECONDS);
640              }
641 <            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
641 >            await(threadStarted);
642              BlockingQueue<Runnable> q = p.getQueue();
643              assertFalse(p.remove((Runnable)tasks[0]));
644              assertTrue(q.contains((Runnable)tasks[4]));
# Line 654 | Line 649 | public class ScheduledExecutorSubclassTe
649              assertTrue(q.contains((Runnable)tasks[3]));
650              assertTrue(p.remove((Runnable)tasks[3]));
651              assertFalse(q.contains((Runnable)tasks[3]));
657            done.countDown();
652          }
653      }
654  
# Line 706 | Line 700 | public class ScheduledExecutorSubclassTe
700          }};
701          for (int i = 0; i < count; i++)
702              p.execute(waiter);
703 <        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
703 >        await(threadsStarted);
704          assertEquals(poolSize, p.getActiveCount());
705          assertEquals(0, p.getCompletedTaskCount());
706          final List<Runnable> queuedTasks;
# Line 1103 | Line 1097 | public class ScheduledExecutorSubclassTe
1097      public void testTimedInvokeAny4() throws Exception {
1098          final ExecutorService e = new CustomExecutor(2);
1099          try (PoolCleaner cleaner = cleaner(e)) {
1100 +            long startTime = System.nanoTime();
1101              List<Callable<String>> l = new ArrayList<Callable<String>>();
1102              l.add(new NPETask());
1103              try {
1104 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1104 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1105                  shouldThrow();
1106              } catch (ExecutionException success) {
1107                  assertTrue(success.getCause() instanceof NullPointerException);
1108              }
1109 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1110          }
1111      }
1112  
# Line 1120 | Line 1116 | public class ScheduledExecutorSubclassTe
1116      public void testTimedInvokeAny5() throws Exception {
1117          final ExecutorService e = new CustomExecutor(2);
1118          try (PoolCleaner cleaner = cleaner(e)) {
1119 +            long startTime = System.nanoTime();
1120              List<Callable<String>> l = new ArrayList<Callable<String>>();
1121              l.add(new StringTask());
1122              l.add(new StringTask());
1123 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1123 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1124              assertSame(TEST_STRING, result);
1125 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1126          }
1127      }
1128  
# Line 1224 | Line 1222 | public class ScheduledExecutorSubclassTe
1222       * timed invokeAll(c) cancels tasks not completed by timeout
1223       */
1224      public void testTimedInvokeAll6() throws Exception {
1225 <        final ExecutorService e = new CustomExecutor(2);
1226 <        try (PoolCleaner cleaner = cleaner(e)) {
1227 <            for (long timeout = timeoutMillis();;) {
1225 >        for (long timeout = timeoutMillis();;) {
1226 >            final CountDownLatch done = new CountDownLatch(1);
1227 >            final Callable<String> waiter = new CheckedCallable<String>() {
1228 >                public String realCall() {
1229 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1230 >                    catch (InterruptedException ok) {}
1231 >                    return "1"; }};
1232 >            final ExecutorService p = new CustomExecutor(2);
1233 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1234                  List<Callable<String>> tasks = new ArrayList<>();
1235                  tasks.add(new StringTask("0"));
1236 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1236 >                tasks.add(waiter);
1237                  tasks.add(new StringTask("2"));
1238                  long startTime = System.nanoTime();
1239                  List<Future<String>> futures =
1240 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1240 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1241                  assertEquals(tasks.size(), futures.size());
1242                  assertTrue(millisElapsedSince(startTime) >= timeout);
1243                  for (Future future : futures)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines