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.26 by jsr166, Sat May 28 15:33:19 2011 UTC vs.
Revision 1.35 by jsr166, Sat Apr 25 04:55:31 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 java.util.concurrent.atomic.*;
8 >
9 > import java.util.ArrayList;
10 > import java.util.List;
11 > import java.util.concurrent.BlockingQueue;
12 > import java.util.concurrent.Callable;
13 > import java.util.concurrent.CountDownLatch;
14 > import java.util.concurrent.Delayed;
15 > import java.util.concurrent.ExecutionException;
16 > import java.util.concurrent.Executors;
17 > import java.util.concurrent.ExecutorService;
18 > import java.util.concurrent.Future;
19 > import java.util.concurrent.RejectedExecutionException;
20 > import java.util.concurrent.RejectedExecutionHandler;
21 > import java.util.concurrent.RunnableScheduledFuture;
22 > import java.util.concurrent.ScheduledFuture;
23 > import java.util.concurrent.ScheduledThreadPoolExecutor;
24 > import java.util.concurrent.ThreadFactory;
25 > import java.util.concurrent.ThreadPoolExecutor;
26 > import java.util.concurrent.TimeoutException;
27 > import java.util.concurrent.TimeUnit;
28 > import java.util.concurrent.atomic.AtomicInteger;
29 >
30 > import junit.framework.Test;
31 > import junit.framework.TestSuite;
32  
33   public class ScheduledExecutorSubclassTest 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(ScheduledExecutorSubclassTest.class);
# Line 36 | Line 56 | public class ScheduledExecutorSubclassTe
56          }
57          public boolean isCancelled() { return task.isCancelled(); }
58          public boolean isDone() { return task.isDone(); }
59 <        public V get() throws InterruptedException,  ExecutionException {
59 >        public V get() throws InterruptedException, ExecutionException {
60              V v = task.get();
61              assertTrue(ran);
62              return v;
63          }
64 <        public V get(long time, TimeUnit unit) throws InterruptedException,  ExecutionException, TimeoutException {
64 >        public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
65              V v = task.get(time, unit);
66              assertTrue(ran);
67              return v;
# Line 57 | Line 77 | public class ScheduledExecutorSubclassTe
77          protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
78              return new CustomTask<V>(task);
79          }
80 <        CustomExecutor(int corePoolSize) { super(corePoolSize);}
80 >        CustomExecutor(int corePoolSize) { super(corePoolSize); }
81          CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
82              super(corePoolSize, handler);
83          }
# Line 193 | Line 213 | public class ScheduledExecutorSubclassTe
213       */
214      public void testFixedRateSequence() throws InterruptedException {
215          CustomExecutor p = new CustomExecutor(1);
216 <        RunnableCounter counter = new RunnableCounter();
217 <        ScheduledFuture h =
218 <            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
219 <        delay(SMALL_DELAY_MS);
220 <        h.cancel(true);
221 <        int c = counter.count.get();
222 <        // By time scaling conventions, we must have at least
223 <        // an execution per SHORT delay, but no more than one SHORT more
224 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
225 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
226 <        joinPool(p);
216 >        try {
217 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
218 >                long startTime = System.nanoTime();
219 >                int cycles = 10;
220 >                final CountDownLatch done = new CountDownLatch(cycles);
221 >                Runnable task = new CheckedRunnable() {
222 >                    public void realRun() { done.countDown(); }};
223 >                ScheduledFuture h =
224 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
225 >                done.await();
226 >                h.cancel(true);
227 >                double normalizedTime =
228 >                    (double) millisElapsedSince(startTime) / delay;
229 >                if (normalizedTime >= cycles - 1 &&
230 >                    normalizedTime <= cycles)
231 >                    return;
232 >            }
233 >            throw new AssertionError("unexpected execution rate");
234 >        } finally {
235 >            joinPool(p);
236 >        }
237      }
238  
239      /**
# Line 211 | Line 241 | public class ScheduledExecutorSubclassTe
241       */
242      public void testFixedDelaySequence() throws InterruptedException {
243          CustomExecutor p = new CustomExecutor(1);
244 <        RunnableCounter counter = new RunnableCounter();
245 <        ScheduledFuture h =
246 <            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
247 <        delay(SMALL_DELAY_MS);
248 <        h.cancel(true);
249 <        int c = counter.count.get();
250 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
251 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
252 <        joinPool(p);
244 >        try {
245 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
246 >                long startTime = System.nanoTime();
247 >                int cycles = 10;
248 >                final CountDownLatch done = new CountDownLatch(cycles);
249 >                Runnable task = new CheckedRunnable() {
250 >                    public void realRun() { done.countDown(); }};
251 >                ScheduledFuture h =
252 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
253 >                done.await();
254 >                h.cancel(true);
255 >                double normalizedTime =
256 >                    (double) millisElapsedSince(startTime) / delay;
257 >                if (normalizedTime >= cycles - 1 &&
258 >                    normalizedTime <= cycles)
259 >                    return;
260 >            }
261 >            throw new AssertionError("unexpected execution rate");
262 >        } finally {
263 >            joinPool(p);
264 >        }
265      }
266  
267      /**
# Line 370 | Line 412 | public class ScheduledExecutorSubclassTe
412                      threadProceed.await();
413                      threadDone.countDown();
414                  }});
415 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
415 >            await(threadStarted);
416              assertEquals(0, p.getCompletedTaskCount());
417              threadProceed.countDown();
418              threadDone.await();
# Line 1204 | Line 1246 | public class ScheduledExecutorSubclassTe
1246              l.add(new StringTask());
1247              List<Future<String>> futures =
1248                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1249 <            assertEquals(3, futures.size());
1250 <            Iterator<Future<String>> it = futures.iterator();
1251 <            Future<String> f1 = it.next();
1252 <            Future<String> f2 = it.next();
1253 <            Future<String> f3 = it.next();
1212 <            assertTrue(f1.isDone());
1213 <            assertTrue(f2.isDone());
1214 <            assertTrue(f3.isDone());
1215 <            assertFalse(f1.isCancelled());
1216 <            assertTrue(f2.isCancelled());
1249 >            assertEquals(l.size(), futures.size());
1250 >            for (Future future : futures)
1251 >                assertTrue(future.isDone());
1252 >            assertFalse(futures.get(0).isCancelled());
1253 >            assertTrue(futures.get(1).isCancelled());
1254          } finally {
1255              joinPool(e);
1256          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines