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.31 by jsr166, Tue Sep 24 18:35:21 2013 UTC vs.
Revision 1.41 by jsr166, Mon Sep 28 03:05:23 2015 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 import junit.framework.*;
8 import java.util.*;
9 import java.util.concurrent.*;
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 + import static java.util.concurrent.TimeUnit.SECONDS;
9 +
10 + import java.util.ArrayList;
11 + import java.util.HashSet;
12 + import java.util.List;
13 + import java.util.concurrent.BlockingQueue;
14 + import java.util.concurrent.Callable;
15 + import java.util.concurrent.CancellationException;
16 + import java.util.concurrent.CountDownLatch;
17 + import java.util.concurrent.Delayed;
18 + import java.util.concurrent.ExecutionException;
19 + import java.util.concurrent.Executors;
20 + import java.util.concurrent.ExecutorService;
21 + import java.util.concurrent.Future;
22 + import java.util.concurrent.RejectedExecutionException;
23 + import java.util.concurrent.RejectedExecutionHandler;
24 + import java.util.concurrent.RunnableScheduledFuture;
25 + import java.util.concurrent.ScheduledFuture;
26 + import java.util.concurrent.ScheduledThreadPoolExecutor;
27 + import java.util.concurrent.ThreadFactory;
28 + import java.util.concurrent.ThreadPoolExecutor;
29 + import java.util.concurrent.TimeoutException;
30 + import java.util.concurrent.TimeUnit;
31   import java.util.concurrent.atomic.AtomicInteger;
32  
33 + import junit.framework.Test;
34 + import junit.framework.TestSuite;
35 +
36   public class ScheduledExecutorSubclassTest extends JSR166TestCase {
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run(suite());
38 >        main(suite(), args);
39      }
40      public static Test suite() {
41          return new TestSuite(ScheduledExecutorSubclassTest.class);
# Line 198 | Line 221 | public class ScheduledExecutorSubclassTe
221                  long startTime = System.nanoTime();
222                  int cycles = 10;
223                  final CountDownLatch done = new CountDownLatch(cycles);
224 <                CheckedRunnable task = new CheckedRunnable() {
224 >                Runnable task = new CheckedRunnable() {
225                      public void realRun() { done.countDown(); }};
203                
226                  ScheduledFuture h =
227                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
228                  done.await();
# Line 227 | Line 249 | public class ScheduledExecutorSubclassTe
249                  long startTime = System.nanoTime();
250                  int cycles = 10;
251                  final CountDownLatch done = new CountDownLatch(cycles);
252 <                CheckedRunnable task = new CheckedRunnable() {
252 >                Runnable task = new CheckedRunnable() {
253                      public void realRun() { done.countDown(); }};
232                
254                  ScheduledFuture h =
255                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
256                  done.await();
# Line 686 | Line 707 | public class ScheduledExecutorSubclassTe
707      }
708  
709      /**
710 <     * shutdownNow returns a list containing tasks that were not run
710 >     * shutdownNow returns a list containing tasks that were not run,
711 >     * and those tasks are drained from the queue
712       */
713 <    public void testShutdownNow() {
713 >    public void testShutdownNow() throws InterruptedException {
714 >        final int poolSize = 2;
715 >        final int count = 5;
716 >        final AtomicInteger ran = new AtomicInteger(0);
717 >        final CustomExecutor p = new CustomExecutor(poolSize);
718 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
719 >        CheckedRunnable waiter = new CheckedRunnable() { public void realRun() {
720 >            threadsStarted.countDown();
721 >            try {
722 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
723 >            } catch (InterruptedException success) {}
724 >            ran.getAndIncrement();
725 >        }};
726 >        for (int i = 0; i < count; i++)
727 >            p.execute(waiter);
728 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
729 >        assertEquals(poolSize, p.getActiveCount());
730 >        assertEquals(0, p.getCompletedTaskCount());
731 >        final List<Runnable> queuedTasks;
732 >        try {
733 >            queuedTasks = p.shutdownNow();
734 >        } catch (SecurityException ok) {
735 >            return; // Allowed in case test doesn't have privs
736 >        }
737 >        assertTrue(p.isShutdown());
738 >        assertTrue(p.getQueue().isEmpty());
739 >        assertEquals(count - poolSize, queuedTasks.size());
740 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
741 >        assertTrue(p.isTerminated());
742 >        assertEquals(poolSize, ran.get());
743 >        assertEquals(poolSize, p.getCompletedTaskCount());
744 >    }
745 >
746 >    /**
747 >     * shutdownNow returns a list containing tasks that were not run,
748 >     * and those tasks are drained from the queue
749 >     */
750 >    public void testShutdownNow_delayedTasks() throws InterruptedException {
751          CustomExecutor p = new CustomExecutor(1);
752 <        for (int i = 0; i < 5; i++)
753 <            p.schedule(new SmallPossiblyInterruptedRunnable(),
754 <                       LONG_DELAY_MS, MILLISECONDS);
752 >        List<ScheduledFuture> tasks = new ArrayList<>();
753 >        for (int i = 0; i < 3; i++) {
754 >            Runnable r = new NoOpRunnable();
755 >            tasks.add(p.schedule(r, 9, SECONDS));
756 >            tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
757 >            tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
758 >        }
759 >        if (testImplementationDetails)
760 >            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
761 >        final List<Runnable> queuedTasks;
762          try {
763 <            List<Runnable> l = p.shutdownNow();
698 <            assertTrue(p.isShutdown());
699 <            assertEquals(5, l.size());
763 >            queuedTasks = p.shutdownNow();
764          } catch (SecurityException ok) {
765 <            // Allowed in case test doesn't have privs
766 <        } finally {
767 <            joinPool(p);
765 >            return; // Allowed in case test doesn't have privs
766 >        }
767 >        assertTrue(p.isShutdown());
768 >        assertTrue(p.getQueue().isEmpty());
769 >        if (testImplementationDetails)
770 >            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
771 >        assertEquals(tasks.size(), queuedTasks.size());
772 >        for (ScheduledFuture task : tasks) {
773 >            assertFalse(((CustomTask)task).ran);
774 >            assertFalse(task.isDone());
775 >            assertFalse(task.isCancelled());
776          }
777 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
778 +        assertTrue(p.isTerminated());
779      }
780  
781      /**
# Line 1222 | Line 1296 | public class ScheduledExecutorSubclassTe
1296      public void testTimedInvokeAll6() throws Exception {
1297          ExecutorService e = new CustomExecutor(2);
1298          try {
1299 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1300 <            l.add(new StringTask());
1301 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1302 <            l.add(new StringTask());
1303 <            List<Future<String>> futures =
1304 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1305 <            assertEquals(l.size(), futures.size());
1306 <            for (Future future : futures)
1307 <                assertTrue(future.isDone());
1308 <            assertFalse(futures.get(0).isCancelled());
1309 <            assertTrue(futures.get(1).isCancelled());
1299 >            for (long timeout = timeoutMillis();;) {
1300 >                List<Callable<String>> tasks = new ArrayList<>();
1301 >                tasks.add(new StringTask("0"));
1302 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1303 >                tasks.add(new StringTask("2"));
1304 >                long startTime = System.nanoTime();
1305 >                List<Future<String>> futures =
1306 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1307 >                assertEquals(tasks.size(), futures.size());
1308 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1309 >                for (Future future : futures)
1310 >                    assertTrue(future.isDone());
1311 >                assertTrue(futures.get(1).isCancelled());
1312 >                try {
1313 >                    assertEquals("0", futures.get(0).get());
1314 >                    assertEquals("2", futures.get(2).get());
1315 >                    break;
1316 >                } catch (CancellationException retryWithLongerTimeout) {
1317 >                    timeout *= 2;
1318 >                    if (timeout >= LONG_DELAY_MS / 2)
1319 >                        fail("expected exactly one task to be cancelled");
1320 >                }
1321 >            }
1322          } finally {
1323              joinPool(e);
1324          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines