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.25 by jsr166, Fri May 27 19:43:27 2011 UTC vs.
Revision 1.33 by jsr166, Wed Sep 25 07:39:17 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 95 | Line 95 | public class ScheduledExecutorSubclassTe
95       */
96      public void testSchedule1() throws Exception {
97          CustomExecutor p = new CustomExecutor(1);
98 <        final long t0 = System.nanoTime();
99 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
98 >        final long startTime = System.nanoTime();
99          final CountDownLatch done = new CountDownLatch(1);
100          try {
101              Callable task = new CheckedCallable<Boolean>() {
102                  public Boolean realCall() {
103                      done.countDown();
104 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
104 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
105                      return Boolean.TRUE;
106                  }};
107 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
108 <            assertEquals(Boolean.TRUE, f.get());
109 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
107 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
108 >            assertSame(Boolean.TRUE, f.get());
109 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
110              assertTrue(done.await(0L, MILLISECONDS));
111          } finally {
112              joinPool(p);
# Line 119 | Line 118 | public class ScheduledExecutorSubclassTe
118       */
119      public void testSchedule3() throws Exception {
120          CustomExecutor p = new CustomExecutor(1);
121 <        final long t0 = System.nanoTime();
123 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
121 >        final long startTime = System.nanoTime();
122          final CountDownLatch done = new CountDownLatch(1);
123          try {
124              Runnable task = new CheckedRunnable() {
125                  public void realRun() {
126                      done.countDown();
127 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
127 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
128                  }};
129 <            Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS);
130 <            assertNull(f.get());
131 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
132 <            assertTrue(done.await(0L, MILLISECONDS));
129 >            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
130 >            await(done);
131 >            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
132 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
133          } finally {
134              joinPool(p);
135          }
# Line 142 | Line 140 | public class ScheduledExecutorSubclassTe
140       */
141      public void testSchedule4() throws InterruptedException {
142          CustomExecutor p = new CustomExecutor(1);
143 <        final long t0 = System.nanoTime();
146 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
143 >        final long startTime = System.nanoTime();
144          final CountDownLatch done = new CountDownLatch(1);
145          try {
146              Runnable task = new CheckedRunnable() {
147                  public void realRun() {
148                      done.countDown();
149 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
149 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
150                  }};
151              ScheduledFuture f =
152 <                p.scheduleAtFixedRate(task, SHORT_DELAY_MS,
153 <                                      SHORT_DELAY_MS, MILLISECONDS);
154 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
155 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
152 >                p.scheduleAtFixedRate(task, timeoutMillis(),
153 >                                      LONG_DELAY_MS, MILLISECONDS);
154 >            await(done);
155 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
156              f.cancel(true);
157          } finally {
158              joinPool(p);
# Line 167 | Line 164 | public class ScheduledExecutorSubclassTe
164       */
165      public void testSchedule5() throws InterruptedException {
166          CustomExecutor p = new CustomExecutor(1);
167 <        final long t0 = System.nanoTime();
171 <        final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
167 >        final long startTime = System.nanoTime();
168          final CountDownLatch done = new CountDownLatch(1);
169          try {
170              Runnable task = new CheckedRunnable() {
171                  public void realRun() {
172                      done.countDown();
173 <                    assertTrue(System.nanoTime() - t0 >= timeoutNanos);
173 >                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
174                  }};
175              ScheduledFuture f =
176 <                p.scheduleWithFixedDelay(task, SHORT_DELAY_MS,
177 <                                         SHORT_DELAY_MS, MILLISECONDS);
178 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
179 <            assertTrue(System.nanoTime() - t0 >= timeoutNanos);
176 >                p.scheduleWithFixedDelay(task, timeoutMillis(),
177 >                                         LONG_DELAY_MS, MILLISECONDS);
178 >            await(done);
179 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
180              f.cancel(true);
181          } finally {
182              joinPool(p);
# Line 197 | 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 >                Runnable task = new CheckedRunnable() {
202 >                    public void realRun() { done.countDown(); }};
203 >                ScheduledFuture h =
204 >                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
205 >                done.await();
206 >                h.cancel(true);
207 >                double normalizedTime =
208 >                    (double) millisElapsedSince(startTime) / delay;
209 >                if (normalizedTime >= cycles - 1 &&
210 >                    normalizedTime <= cycles)
211 >                    return;
212 >            }
213 >            throw new AssertionError("unexpected execution rate");
214 >        } finally {
215 >            joinPool(p);
216 >        }
217      }
218  
219      /**
# Line 215 | Line 221 | public class ScheduledExecutorSubclassTe
221       */
222      public void testFixedDelaySequence() throws InterruptedException {
223          CustomExecutor p = new CustomExecutor(1);
224 <        RunnableCounter counter = new RunnableCounter();
225 <        ScheduledFuture h =
226 <            p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
227 <        delay(SMALL_DELAY_MS);
228 <        h.cancel(true);
229 <        int c = counter.count.get();
230 <        assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
231 <        assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
232 <        joinPool(p);
224 >        try {
225 >            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
226 >                long startTime = System.nanoTime();
227 >                int cycles = 10;
228 >                final CountDownLatch done = new CountDownLatch(cycles);
229 >                Runnable task = new CheckedRunnable() {
230 >                    public void realRun() { done.countDown(); }};
231 >                ScheduledFuture h =
232 >                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
233 >                done.await();
234 >                h.cancel(true);
235 >                double normalizedTime =
236 >                    (double) millisElapsedSince(startTime) / delay;
237 >                if (normalizedTime >= cycles - 1 &&
238 >                    normalizedTime <= cycles)
239 >                    return;
240 >            }
241 >            throw new AssertionError("unexpected execution rate");
242 >        } finally {
243 >            joinPool(p);
244 >        }
245      }
246  
247      /**
# Line 374 | Line 392 | public class ScheduledExecutorSubclassTe
392                      threadProceed.await();
393                      threadDone.countDown();
394                  }});
395 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
395 >            await(threadStarted);
396              assertEquals(0, p.getCompletedTaskCount());
397              threadProceed.countDown();
398              threadDone.await();
399 <            delay(SHORT_DELAY_MS);
400 <            assertEquals(1, p.getCompletedTaskCount());
399 >            long startTime = System.nanoTime();
400 >            while (p.getCompletedTaskCount() != 1) {
401 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
402 >                    fail("timed out");
403 >                Thread.yield();
404 >            }
405          } finally {
406              joinPool(p);
407          }
# Line 1204 | Line 1226 | public class ScheduledExecutorSubclassTe
1226              l.add(new StringTask());
1227              List<Future<String>> futures =
1228                  e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1229 <            assertEquals(3, futures.size());
1230 <            Iterator<Future<String>> it = futures.iterator();
1231 <            Future<String> f1 = it.next();
1232 <            Future<String> f2 = it.next();
1233 <            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());
1229 >            assertEquals(l.size(), futures.size());
1230 >            for (Future future : futures)
1231 >                assertTrue(future.isDone());
1232 >            assertFalse(futures.get(0).isCancelled());
1233 >            assertTrue(futures.get(1).isCancelled());
1234          } finally {
1235              joinPool(e);
1236          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines