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.31 by jsr166, Tue Sep 24 18:35:21 2013 UTC

# Line 8 | Line 8 | import junit.framework.*;
8   import java.util.*;
9   import java.util.concurrent.*;
10   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 < import java.util.concurrent.atomic.*;
11 > import java.util.concurrent.atomic.AtomicInteger;
12  
13   public class ScheduledExecutorSubclassTest extends JSR166TestCase {
14      public static void main(String[] args) {
# Line 36 | Line 36 | public class ScheduledExecutorSubclassTe
36          }
37          public boolean isCancelled() { return task.isCancelled(); }
38          public boolean isDone() { return task.isDone(); }
39 <        public V get() throws InterruptedException,  ExecutionException {
39 >        public V get() throws InterruptedException, ExecutionException {
40              V v = task.get();
41              assertTrue(ran);
42              return v;
43          }
44 <        public V get(long time, TimeUnit unit) throws InterruptedException,  ExecutionException, TimeoutException {
44 >        public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
45              V v = task.get(time, unit);
46              assertTrue(ran);
47              return v;
# Line 57 | Line 57 | public class ScheduledExecutorSubclassTe
57          protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
58              return new CustomTask<V>(task);
59          }
60 <        CustomExecutor(int corePoolSize) { super(corePoolSize);}
60 >        CustomExecutor(int corePoolSize) { super(corePoolSize); }
61          CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
62              super(corePoolSize, handler);
63          }
# Line 193 | Line 193 | public class ScheduledExecutorSubclassTe
193       */
194      public void testFixedRateSequence() throws InterruptedException {
195          CustomExecutor p = new CustomExecutor(1);
196 <        RunnableCounter counter = new RunnableCounter();
197 <        ScheduledFuture h =
198 <            p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
199 <        delay(SMALL_DELAY_MS);
200 <        h.cancel(true);
201 <        int c = counter.count.get();
202 <        // By time scaling conventions, we must have at least
203 <        // an execution per SHORT delay, but no more than one SHORT more
204 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
205 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
206 <        joinPool(p);
196 >        try {
197 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
198 >                long startTime = System.nanoTime();
199 >                int cycles = 10;
200 >                final CountDownLatch done = new CountDownLatch(cycles);
201 >                CheckedRunnable task = new CheckedRunnable() {
202 >                    public void realRun() { done.countDown(); }};
203 >                
204 >                ScheduledFuture h =
205 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
206 >                done.await();
207 >                h.cancel(true);
208 >                double normalizedTime =
209 >                    (double) millisElapsedSince(startTime) / delay;
210 >                if (normalizedTime >= cycles - 1 &&
211 >                    normalizedTime <= cycles)
212 >                    return;
213 >            }
214 >            throw new AssertionError("unexpected execution rate");
215 >        } finally {
216 >            joinPool(p);
217 >        }
218      }
219  
220      /**
# Line 211 | Line 222 | public class ScheduledExecutorSubclassTe
222       */
223      public void testFixedDelaySequence() throws InterruptedException {
224          CustomExecutor p = new CustomExecutor(1);
225 <        RunnableCounter counter = new RunnableCounter();
226 <        ScheduledFuture h =
227 <            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
228 <        delay(SMALL_DELAY_MS);
229 <        h.cancel(true);
230 <        int c = counter.count.get();
231 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
232 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
233 <        joinPool(p);
225 >        try {
226 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
227 >                long startTime = System.nanoTime();
228 >                int cycles = 10;
229 >                final CountDownLatch done = new CountDownLatch(cycles);
230 >                CheckedRunnable task = new CheckedRunnable() {
231 >                    public void realRun() { done.countDown(); }};
232 >                
233 >                ScheduledFuture h =
234 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
235 >                done.await();
236 >                h.cancel(true);
237 >                double normalizedTime =
238 >                    (double) millisElapsedSince(startTime) / delay;
239 >                if (normalizedTime >= cycles - 1 &&
240 >                    normalizedTime <= cycles)
241 >                    return;
242 >            }
243 >            throw new AssertionError("unexpected execution rate");
244 >        } finally {
245 >            joinPool(p);
246 >        }
247      }
248  
249      /**
# Line 370 | Line 394 | public class ScheduledExecutorSubclassTe
394                      threadProceed.await();
395                      threadDone.countDown();
396                  }});
397 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
397 >            await(threadStarted);
398              assertEquals(0, p.getCompletedTaskCount());
399              threadProceed.countDown();
400              threadDone.await();
# Line 1204 | Line 1228 | public class ScheduledExecutorSubclassTe
1228              l.add(new StringTask());
1229              List<Future<String>> futures =
1230                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1231 <            assertEquals(3, futures.size());
1232 <            Iterator<Future<String>> it = futures.iterator();
1233 <            Future<String> f1 = it.next();
1234 <            Future<String> f2 = it.next();
1235 <            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());
1231 >            assertEquals(l.size(), futures.size());
1232 >            for (Future future : futures)
1233 >                assertTrue(future.isDone());
1234 >            assertFalse(futures.get(0).isCancelled());
1235 >            assertTrue(futures.get(1).isCancelled());
1236          } finally {
1237              joinPool(e);
1238          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines