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.24 by jsr166, Fri May 27 16:26:29 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;
68          }
69      }
70  
51
71      public class CustomExecutor extends ScheduledThreadPoolExecutor {
72  
73          protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
# Line 58 | 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 73 | Line 92 | public class ScheduledExecutorSubclassTe
92  
93      }
94  
76
95      /**
96       * execute successfully executes a runnable
97       */
# Line 92 | Line 110 | public class ScheduledExecutorSubclassTe
110          }
111      }
112  
95
113      /**
114       * delayed schedule of callable successfully executes after delay
115       */
116      public void testSchedule1() throws Exception {
117          CustomExecutor p = new CustomExecutor(1);
118 <        final long t0 = System.nanoTime();
102 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
118 >        final long startTime = System.nanoTime();
119          final CountDownLatch done = new CountDownLatch(1);
120          try {
121              Callable task = new CheckedCallable<Boolean>() {
122                  public Boolean realCall() {
123                      done.countDown();
124 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
124 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
125                      return Boolean.TRUE;
126                  }};
127 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
128 <            assertEquals(Boolean.TRUE, f.get());
129 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
127 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
128 >            assertSame(Boolean.TRUE, f.get());
129 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
130              assertTrue(done.await(0L, MILLISECONDS));
131          } finally {
132              joinPool(p);
# Line 122 | Line 138 | public class ScheduledExecutorSubclassTe
138       */
139      public void testSchedule3() throws Exception {
140          CustomExecutor p = new CustomExecutor(1);
141 <        final long t0 = System.nanoTime();
126 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
141 >        final long startTime = System.nanoTime();
142          final CountDownLatch done = new CountDownLatch(1);
143          try {
144              Runnable task = new CheckedRunnable() {
145                  public void realRun() {
146                      done.countDown();
147 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
147 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
148                  }};
149 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
150 <            assertNull(f.get());
151 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
152 <            assertTrue(done.await(0L, MILLISECONDS));
149 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
150 >            await(done);
151 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
152 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
153          } finally {
154              joinPool(p);
155          }
# Line 145 | Line 160 | public class ScheduledExecutorSubclassTe
160       */
161      public void testSchedule4() throws InterruptedException {
162          CustomExecutor p = new CustomExecutor(1);
163 <        final long t0 = System.nanoTime();
149 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
163 >        final long startTime = System.nanoTime();
164          final CountDownLatch done = new CountDownLatch(1);
165          try {
166              Runnable task = new CheckedRunnable() {
167                  public void realRun() {
168                      done.countDown();
169 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
169 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
170                  }};
171              ScheduledFuture f =
172 <                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
173 <                                      SHORT_DELAY_MS, MILLISECONDS);
174 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
175 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
172 >                p.scheduleAtFixedRate(task, timeoutMillis(),
173 >                                      LONG_DELAY_MS, MILLISECONDS);
174 >            await(done);
175 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
176              f.cancel(true);
177          } finally {
178              joinPool(p);
# Line 170 | Line 184 | public class ScheduledExecutorSubclassTe
184       */
185      public void testSchedule5() throws InterruptedException {
186          CustomExecutor p = new CustomExecutor(1);
187 <        final long t0 = System.nanoTime();
174 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
187 >        final long startTime = System.nanoTime();
188          final CountDownLatch done = new CountDownLatch(1);
189          try {
190              Runnable task = new CheckedRunnable() {
191                  public void realRun() {
192                      done.countDown();
193 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
193 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
194                  }};
195              ScheduledFuture f =
196 <                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
197 <                                         SHORT_DELAY_MS, MILLISECONDS);
198 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
199 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
196 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
197 >                                         LONG_DELAY_MS, MILLISECONDS);
198 >            await(done);
199 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
200              f.cancel(true);
201          } finally {
202              joinPool(p);
# Line 200 | 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 218 | 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  
232
267      /**
268       * execute(null) throws NPE
269       */
# Line 378 | 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();
419 <            delay(SHORT_DELAY_MS);
420 <            assertEquals(1, p.getCompletedTaskCount());
419 >            long startTime = System.nanoTime();
420 >            while (p.getCompletedTaskCount() != 1) {
421 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
422 >                    fail("timed out");
423 >                Thread.yield();
424 >            }
425          } finally {
426              joinPool(p);
427          }
# Line 523 | Line 561 | public class ScheduledExecutorSubclassTe
561          assertTrue(p.isShutdown());
562      }
563  
526
564      /**
565       * isTerminated is false before termination, true after
566       */
# Line 714 | Line 751 | public class ScheduledExecutorSubclassTe
751          }
752      }
753  
717
754      /**
755       * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
756       * delayed tasks are cancelled at shutdown
# Line 741 | Line 777 | public class ScheduledExecutorSubclassTe
777          }
778      }
779  
744
780      /**
781       * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
782       * periodic tasks are cancelled at shutdown
# Line 1211 | 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();
1219 <            assertTrue(f1.isDone());
1220 <            assertTrue(f2.isDone());
1221 <            assertTrue(f3.isDone());
1222 <            assertFalse(f1.isCancelled());
1223 <            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