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.56 by jsr166, Mon Sep 28 02:41:29 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() throws InterruptedException {
660 >        final int poolSize = 2;
661 >        final int count = 5;
662 >        final AtomicInteger ran = new AtomicInteger(0);
663 >        final ScheduledThreadPoolExecutor p =
664 >            new ScheduledThreadPoolExecutor(poolSize);
665 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
666 >        CheckedRunnable waiter = new CheckedRunnable() { public void realRun() {
667 >            threadsStarted.countDown();
668 >            try {
669 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
670 >            } catch (InterruptedException success) {}
671 >            ran.getAndIncrement();
672 >        }};
673 >        for (int i = 0; i < count; i++)
674 >            p.execute(waiter);
675 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
676 >        final List<Runnable> queuedTasks;
677 >        try {
678 >            queuedTasks = p.shutdownNow();
679 >        } catch (SecurityException ok) {
680 >            return; // Allowed in case test doesn't have privs
681 >        }
682 >        assertTrue(p.isShutdown());
683 >        assertTrue(p.getQueue().isEmpty());
684 >        assertEquals(count - poolSize, queuedTasks.size());
685 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
686 >        assertTrue(p.isTerminated());
687 >        assertEquals(poolSize, ran.get());
688 >    }
689 >
690 >    /**
691 >     * shutdownNow returns a list containing tasks that were not run,
692 >     * and those tasks are drained from the queue
693 >     */
694 >    public void testShutdownNow_delayedTasks() throws InterruptedException {
695          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
696 <        for (int i = 0; i < 5; i++)
697 <            p.schedule(new SmallPossiblyInterruptedRunnable(),
698 <                       LONG_DELAY_MS, MILLISECONDS);
696 >        List<ScheduledFuture> tasks = new ArrayList<>();
697 >        for (int i = 0; i < 3; i++) {
698 >            Runnable r = new NoOpRunnable();
699 >            tasks.add(p.schedule(r, 9, SECONDS));
700 >            tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
701 >            tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
702 >        }
703 >        if (testImplementationDetails)
704 >            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
705 >        final List<Runnable> queuedTasks;
706          try {
707 <            List<Runnable> l = p.shutdownNow();
662 <            assertTrue(p.isShutdown());
663 <            assertEquals(5, l.size());
707 >            queuedTasks = p.shutdownNow();
708          } catch (SecurityException ok) {
709 <            // Allowed in case test doesn't have privs
666 <        } finally {
667 <            joinPool(p);
709 >            return; // Allowed in case test doesn't have privs
710          }
711 +        assertTrue(p.isShutdown());
712 +        assertTrue(p.getQueue().isEmpty());
713 +        if (testImplementationDetails)
714 +            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
715 +        assertEquals(tasks.size(), queuedTasks.size());
716 +        for (ScheduledFuture task : tasks) {
717 +            assertFalse(task.isDone());
718 +            assertFalse(task.isCancelled());
719 +        }
720 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
721 +        assertTrue(p.isTerminated());
722      }
723  
724      /**
# Line 1186 | Line 1239 | public class ScheduledExecutorTest exten
1239      public void testTimedInvokeAll6() throws Exception {
1240          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1241          try {
1242 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1243 <            l.add(new StringTask());
1244 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1245 <            l.add(new StringTask());
1246 <            List<Future<String>> futures =
1247 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1248 <            assertEquals(l.size(), futures.size());
1249 <            for (Future future : futures)
1250 <                assertTrue(future.isDone());
1251 <            assertFalse(futures.get(0).isCancelled());
1252 <            assertTrue(futures.get(1).isCancelled());
1242 >            for (long timeout = timeoutMillis();;) {
1243 >                List<Callable<String>> tasks = new ArrayList<>();
1244 >                tasks.add(new StringTask("0"));
1245 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1246 >                tasks.add(new StringTask("2"));
1247 >                long startTime = System.nanoTime();
1248 >                List<Future<String>> futures =
1249 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1250 >                assertEquals(tasks.size(), futures.size());
1251 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1252 >                for (Future future : futures)
1253 >                    assertTrue(future.isDone());
1254 >                assertTrue(futures.get(1).isCancelled());
1255 >                try {
1256 >                    assertEquals("0", futures.get(0).get());
1257 >                    assertEquals("2", futures.get(2).get());
1258 >                    break;
1259 >                } catch (CancellationException retryWithLongerTimeout) {
1260 >                    timeout *= 2;
1261 >                    if (timeout >= LONG_DELAY_MS / 2)
1262 >                        fail("expected exactly one task to be cancelled");
1263 >                }
1264 >            }
1265          } finally {
1266              joinPool(e);
1267          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines