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.50 by jsr166, Wed Dec 31 19:05:43 2014 UTC vs.
Revision 1.55 by jsr166, Mon Sep 28 02:32:57 2015 UTC

# Line 7 | Line 7
7   */
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 + import static java.util.concurrent.TimeUnit.SECONDS;
11  
12   import java.util.ArrayList;
13 + import java.util.HashSet;
14   import java.util.List;
15   import java.util.concurrent.BlockingQueue;
16   import java.util.concurrent.Callable;
17 + import java.util.concurrent.CancellationException;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.ExecutionException;
20   import java.util.concurrent.Executors;
# Line 29 | Line 32 | import junit.framework.TestSuite;
32  
33   public class ScheduledExecutorTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run(suite());
35 >        main(suite(), args);
36      }
37      public static Test suite() {
38          return new TestSuite(ScheduledExecutorTest.class);
# Line 650 | Line 653 | public class ScheduledExecutorTest exten
653      }
654  
655      /**
656 <     * shutdownNow returns a list containing tasks that were not run
656 >     * shutdownNow returns a list containing tasks that were not run,
657 >     * and those tasks are drained from the queue
658       */
659 <    public void testShutdownNow() {
659 >    public void testShutdownNow_delayedTasks() throws InterruptedException {
660          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
661 <        for (int i = 0; i < 5; i++)
662 <            p.schedule(new SmallPossiblyInterruptedRunnable(),
663 <                       LONG_DELAY_MS, MILLISECONDS);
661 >        List<ScheduledFuture> tasks = new ArrayList<>();
662 >        for (int i = 0; i < 3; i++) {
663 >            Runnable r = new NoOpRunnable();
664 >            tasks.add(p.schedule(r, 9, SECONDS));
665 >            tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
666 >            tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
667 >        }
668 >        if (testImplementationDetails)
669 >            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
670 >        final List<Runnable> queuedTasks;
671          try {
672 <            List<Runnable> l = p.shutdownNow();
662 <            assertTrue(p.isShutdown());
663 <            assertEquals(5, l.size());
672 >            queuedTasks = p.shutdownNow();
673          } catch (SecurityException ok) {
674 <            // Allowed in case test doesn't have privs
675 <        } finally {
676 <            joinPool(p);
674 >            return; // Allowed in case test doesn't have privs
675 >        }
676 >        assertTrue(p.isShutdown());
677 >        assertTrue(p.getQueue().isEmpty());
678 >        if (testImplementationDetails)
679 >            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
680 >        assertEquals(tasks.size(), queuedTasks.size());
681 >        for (ScheduledFuture task : tasks) {
682 >            assertFalse(task.isDone());
683 >            assertFalse(task.isCancelled());
684          }
685 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
686 +        assertTrue(p.isTerminated());
687      }
688  
689      /**
# Line 1186 | Line 1204 | public class ScheduledExecutorTest exten
1204      public void testTimedInvokeAll6() throws Exception {
1205          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1206          try {
1207 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1208 <            l.add(new StringTask());
1209 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1210 <            l.add(new StringTask());
1211 <            List<Future<String>> futures =
1212 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1213 <            assertEquals(l.size(), futures.size());
1214 <            for (Future future : futures)
1215 <                assertTrue(future.isDone());
1216 <            assertFalse(futures.get(0).isCancelled());
1217 <            assertTrue(futures.get(1).isCancelled());
1207 >            for (long timeout = timeoutMillis();;) {
1208 >                List<Callable<String>> tasks = new ArrayList<>();
1209 >                tasks.add(new StringTask("0"));
1210 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1211 >                tasks.add(new StringTask("2"));
1212 >                long startTime = System.nanoTime();
1213 >                List<Future<String>> futures =
1214 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1215 >                assertEquals(tasks.size(), futures.size());
1216 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1217 >                for (Future future : futures)
1218 >                    assertTrue(future.isDone());
1219 >                assertTrue(futures.get(1).isCancelled());
1220 >                try {
1221 >                    assertEquals("0", futures.get(0).get());
1222 >                    assertEquals("2", futures.get(2).get());
1223 >                    break;
1224 >                } catch (CancellationException retryWithLongerTimeout) {
1225 >                    timeout *= 2;
1226 >                    if (timeout >= LONG_DELAY_MS / 2)
1227 >                        fail("expected exactly one task to be cancelled");
1228 >                }
1229 >            }
1230          } finally {
1231              joinPool(e);
1232          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines