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.66 by jsr166, Mon Oct 5 21:54:33 2015 UTC vs.
Revision 1.76 by jsr166, Thu Oct 8 03:08:37 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 426 | Line 423 | public class ScheduledExecutorTest exten
423              p.execute(new CheckedRunnable() {
424                  public void realRun() throws InterruptedException {
425                      threadStarted.countDown();
426 <                    done.await();
426 >                    await(done);
427                  }});
428 <            assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS));
428 >            await(threadStarted);
429              assertEquals(1, p.getTaskCount());
430              assertEquals(0, p.getCompletedTaskCount());
431              for (int i = 0; i < TASKS; i++) {
# Line 437 | Line 434 | public class ScheduledExecutorTest exten
434                      public void realRun() throws InterruptedException {
435                          threadStarted.countDown();
436                          assertEquals(1 + TASKS, p.getTaskCount());
437 <                        done.await();
437 >                        await(done);
438                      }});
439              }
440              assertEquals(1 + TASKS, p.getTaskCount());
# Line 464 | 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 475 | 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 489 | 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 512 | 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 536 | 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 552 | 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);
558            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]));
572            done.countDown();
569          }
570      }
571  
# Line 577 | 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);
584            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 601 | 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]));
604            done.countDown();
600          }
601      }
602  
# Line 654 | 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 677 | 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 1051 | Line 1046 | public class ScheduledExecutorTest exten
1046      public void testTimedInvokeAny4() throws Exception {
1047          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1048          try (PoolCleaner cleaner = cleaner(e)) {
1049 +            long startTime = System.nanoTime();
1050              List<Callable<String>> l = new ArrayList<Callable<String>>();
1051              l.add(new NPETask());
1052              try {
1053 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1053 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1054                  shouldThrow();
1055              } catch (ExecutionException success) {
1056                  assertTrue(success.getCause() instanceof NullPointerException);
1057              }
1058 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1059          }
1060      }
1061  
# Line 1068 | Line 1065 | public class ScheduledExecutorTest exten
1065      public void testTimedInvokeAny5() throws Exception {
1066          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1067          try (PoolCleaner cleaner = cleaner(e)) {
1068 +            long startTime = System.nanoTime();
1069              List<Callable<String>> l = new ArrayList<Callable<String>>();
1070              l.add(new StringTask());
1071              l.add(new StringTask());
1072 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1072 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1073              assertSame(TEST_STRING, result);
1074 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1075          }
1076      }
1077  
# Line 1141 | Line 1140 | public class ScheduledExecutorTest exten
1140              List<Callable<String>> l = new ArrayList<Callable<String>>();
1141              l.add(new NPETask());
1142              List<Future<String>> futures =
1143 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1143 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1144              assertEquals(1, futures.size());
1145              try {
1146                  futures.get(0).get();
# Line 1173 | Line 1172 | public class ScheduledExecutorTest exten
1172       * timed invokeAll(c) cancels tasks not completed by timeout
1173       */
1174      public void testTimedInvokeAll6() throws Exception {
1175 <        final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1176 <        try (PoolCleaner cleaner = cleaner(e)) {
1177 <            for (long timeout = timeoutMillis();;) {
1175 >        for (long timeout = timeoutMillis();;) {
1176 >            final CountDownLatch done = new CountDownLatch(1);
1177 >            final Callable<String> waiter = new CheckedCallable<String>() {
1178 >                public String realCall() {
1179 >                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1180 >                    catch (InterruptedException ok) {}
1181 >                    return "1"; }};
1182 >            final ExecutorService p = new ScheduledThreadPoolExecutor(2);
1183 >            try (PoolCleaner cleaner = cleaner(p, done)) {
1184                  List<Callable<String>> tasks = new ArrayList<>();
1185                  tasks.add(new StringTask("0"));
1186 <                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1186 >                tasks.add(waiter);
1187                  tasks.add(new StringTask("2"));
1188                  long startTime = System.nanoTime();
1189                  List<Future<String>> futures =
1190 <                    e.invokeAll(tasks, timeout, MILLISECONDS);
1190 >                    p.invokeAll(tasks, timeout, MILLISECONDS);
1191                  assertEquals(tasks.size(), futures.size());
1192                  assertTrue(millisElapsedSince(startTime) >= timeout);
1193                  for (Future future : futures)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines