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.48 by jsr166, Wed Sep 25 06:59:34 2013 UTC vs.
Revision 1.54 by jsr166, Sun Sep 27 20:17:39 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
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;
21 + import java.util.concurrent.ExecutorService;
22 + import java.util.concurrent.Future;
23 + import java.util.concurrent.RejectedExecutionException;
24 + import java.util.concurrent.ScheduledFuture;
25 + import java.util.concurrent.ScheduledThreadPoolExecutor;
26 + import java.util.concurrent.ThreadFactory;
27 + import java.util.concurrent.ThreadPoolExecutor;
28   import java.util.concurrent.atomic.AtomicInteger;
29  
30 + import junit.framework.Test;
31 + 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 146 | Line 164 | public class ScheduledExecutorTest exten
164                  long startTime = System.nanoTime();
165                  int cycles = 10;
166                  final CountDownLatch done = new CountDownLatch(cycles);
167 <                CheckedRunnable task = new CheckedRunnable() {
167 >                Runnable task = new CheckedRunnable() {
168                      public void realRun() { done.countDown(); }};
169                  ScheduledFuture h =
170                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
# Line 174 | Line 192 | public class ScheduledExecutorTest exten
192                  long startTime = System.nanoTime();
193                  int cycles = 10;
194                  final CountDownLatch done = new CountDownLatch(cycles);
195 <                CheckedRunnable task = new CheckedRunnable() {
195 >                Runnable task = new CheckedRunnable() {
196                      public void realRun() { done.countDown(); }};
197                  ScheduledFuture h =
198                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
# Line 635 | 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 >        assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
669 >        final List<Runnable> queuedTasks;
670          try {
671 <            List<Runnable> l = p.shutdownNow();
647 <            assertTrue(p.isShutdown());
648 <            assertEquals(5, l.size());
671 >            queuedTasks = p.shutdownNow();
672          } catch (SecurityException ok) {
673 <            // Allowed in case test doesn't have privs
674 <        } finally {
675 <            joinPool(p);
673 >            return; // Allowed in case test doesn't have privs
674 >        }
675 >        assertTrue(p.isShutdown());
676 >        assertTrue(p.getQueue().isEmpty());
677 >        assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
678 >        assertEquals(tasks.size(), queuedTasks.size());
679 >        for (ScheduledFuture task : tasks) {
680 >            assertFalse(task.isDone());
681 >            assertFalse(task.isCancelled());
682          }
683 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
684 +        assertTrue(p.isTerminated());
685      }
686  
687      /**
# Line 1171 | Line 1202 | public class ScheduledExecutorTest exten
1202      public void testTimedInvokeAll6() throws Exception {
1203          ExecutorService e = new ScheduledThreadPoolExecutor(2);
1204          try {
1205 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1206 <            l.add(new StringTask());
1207 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1208 <            l.add(new StringTask());
1209 <            List<Future<String>> futures =
1210 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1211 <            assertEquals(l.size(), futures.size());
1212 <            for (Future future : futures)
1213 <                assertTrue(future.isDone());
1214 <            assertFalse(futures.get(0).isCancelled());
1215 <            assertTrue(futures.get(1).isCancelled());
1205 >            for (long timeout = timeoutMillis();;) {
1206 >                List<Callable<String>> tasks = new ArrayList<>();
1207 >                tasks.add(new StringTask("0"));
1208 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1209 >                tasks.add(new StringTask("2"));
1210 >                long startTime = System.nanoTime();
1211 >                List<Future<String>> futures =
1212 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1213 >                assertEquals(tasks.size(), futures.size());
1214 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1215 >                for (Future future : futures)
1216 >                    assertTrue(future.isDone());
1217 >                assertTrue(futures.get(1).isCancelled());
1218 >                try {
1219 >                    assertEquals("0", futures.get(0).get());
1220 >                    assertEquals("2", futures.get(2).get());
1221 >                    break;
1222 >                } catch (CancellationException retryWithLongerTimeout) {
1223 >                    timeout *= 2;
1224 >                    if (timeout >= LONG_DELAY_MS / 2)
1225 >                        fail("expected exactly one task to be cancelled");
1226 >                }
1227 >            }
1228          } finally {
1229              joinPool(e);
1230          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines