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.46 by jsr166, Tue May 31 16:16:24 2011 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 141 | Line 159 | public class ScheduledExecutorTest exten
159       */
160      public void testFixedRateSequence() throws InterruptedException {
161          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
162 <        RunnableCounter counter = new RunnableCounter();
163 <        ScheduledFuture h =
164 <            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
165 <        delay(SMALL_DELAY_MS);
166 <        h.cancel(true);
167 <        int c = counter.count.get();
168 <        // By time scaling conventions, we must have at least
169 <        // an execution per SHORT delay, but no more than one SHORT more
170 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
171 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
172 <        joinPool(p);
162 >        try {
163 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
164 >                long startTime = System.nanoTime();
165 >                int cycles = 10;
166 >                final CountDownLatch done = new CountDownLatch(cycles);
167 >                Runnable task = new CheckedRunnable() {
168 >                    public void realRun() { done.countDown(); }};
169 >                ScheduledFuture h =
170 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
171 >                done.await();
172 >                h.cancel(true);
173 >                double normalizedTime =
174 >                    (double) millisElapsedSince(startTime) / delay;
175 >                if (normalizedTime >= cycles - 1 &&
176 >                    normalizedTime <= cycles)
177 >                    return;
178 >            }
179 >            throw new AssertionError("unexpected execution rate");
180 >        } finally {
181 >            joinPool(p);
182 >        }
183      }
184  
185      /**
# Line 159 | Line 187 | public class ScheduledExecutorTest exten
187       */
188      public void testFixedDelaySequence() throws InterruptedException {
189          ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
190 <        RunnableCounter counter = new RunnableCounter();
191 <        ScheduledFuture h =
192 <            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
193 <        delay(SMALL_DELAY_MS);
194 <        h.cancel(true);
195 <        int c = counter.count.get();
196 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
197 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
198 <        joinPool(p);
190 >        try {
191 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
192 >                long startTime = System.nanoTime();
193 >                int cycles = 10;
194 >                final CountDownLatch done = new CountDownLatch(cycles);
195 >                Runnable task = new CheckedRunnable() {
196 >                    public void realRun() { done.countDown(); }};
197 >                ScheduledFuture h =
198 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
199 >                done.await();
200 >                h.cancel(true);
201 >                double normalizedTime =
202 >                    (double) millisElapsedSince(startTime) / delay;
203 >                if (normalizedTime >= cycles - 1 &&
204 >                    normalizedTime <= cycles)
205 >                    return;
206 >            }
207 >            throw new AssertionError("unexpected execution rate");
208 >        } finally {
209 >            joinPool(p);
210 >        }
211      }
212  
213      /**
# Line 613 | 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();
625 <            assertTrue(p.isShutdown());
626 <            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 1149 | 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