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.60 by jsr166, Mon Feb 22 20:40:11 2016 UTC vs.
Revision 1.61 by jsr166, Mon Feb 22 23:16:06 2016 UTC

# Line 5 | Line 5
5   */
6  
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
9   import static java.util.concurrent.TimeUnit.SECONDS;
10  
11   import java.util.ArrayList;
# Line 28 | Line 29 | import java.util.concurrent.ThreadFactor
29   import java.util.concurrent.ThreadPoolExecutor;
30   import java.util.concurrent.TimeoutException;
31   import java.util.concurrent.TimeUnit;
32 + import java.util.concurrent.atomic.AtomicBoolean;
33   import java.util.concurrent.atomic.AtomicInteger;
34 + import java.util.concurrent.atomic.AtomicLong;
35  
36   import junit.framework.Test;
37   import junit.framework.TestSuite;
# Line 199 | Line 202 | public class ScheduledExecutorSubclassTe
202      }
203  
204      /**
205 <     * scheduleAtFixedRate executes series of tasks at given rate
205 >     * scheduleAtFixedRate executes series of tasks at given rate.
206 >     * Eventually, it must hold that:
207 >     *   cycles - 1 <= elapsedMillis/delay < cycles
208       */
209      public void testFixedRateSequence() throws InterruptedException {
210          final CustomExecutor p = new CustomExecutor(1);
211          try (PoolCleaner cleaner = cleaner(p)) {
212              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
213 <                long startTime = System.nanoTime();
214 <                int cycles = 10;
213 >                final long startTime = System.nanoTime();
214 >                final int cycles = 8;
215                  final CountDownLatch done = new CountDownLatch(cycles);
216 <                Runnable task = new CheckedRunnable() {
216 >                final Runnable task = new CheckedRunnable() {
217                      public void realRun() { done.countDown(); }};
218 <                ScheduledFuture periodicTask =
218 >                final ScheduledFuture periodicTask =
219                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
220 <                await(done);
220 >                final int totalDelayMillis = (cycles - 1) * delay;
221 >                await(done, totalDelayMillis + LONG_DELAY_MS);
222                  periodicTask.cancel(true);
223 <                double elapsedDelays =
224 <                    (double) millisElapsedSince(startTime) / delay;
225 <                if (elapsedDelays >= cycles - 1 &&
220 <                    elapsedDelays <= cycles)
223 >                final long elapsedMillis = millisElapsedSince(startTime);
224 >                assertTrue(elapsedMillis >= totalDelayMillis);
225 >                if (elapsedMillis <= cycles * delay)
226                      return;
227 +                // else retry with longer delay
228              }
229              fail("unexpected execution rate");
230          }
231      }
232  
233      /**
234 <     * scheduleWithFixedDelay executes series of tasks with given period
234 >     * scheduleWithFixedDelay executes series of tasks with given period.
235 >     * Eventually, it must hold that each task starts at least delay and at
236 >     * most 2 * delay after the termination of the previous task.
237       */
238      public void testFixedDelaySequence() throws InterruptedException {
239          final CustomExecutor p = new CustomExecutor(1);
240          try (PoolCleaner cleaner = cleaner(p)) {
241              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
242 <                long startTime = System.nanoTime();
243 <                int cycles = 10;
242 >                final long startTime = System.nanoTime();
243 >                final AtomicLong previous = new AtomicLong(startTime);
244 >                final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
245 >                final int cycles = 8;
246                  final CountDownLatch done = new CountDownLatch(cycles);
247 <                Runnable task = new CheckedRunnable() {
248 <                    public void realRun() { done.countDown(); }};
249 <                ScheduledFuture periodicTask =
247 >                final int d = delay;
248 >                final Runnable task = new CheckedRunnable() {
249 >                    public void realRun() {
250 >                        long now = System.nanoTime();
251 >                        long elapsedMillis
252 >                            = NANOSECONDS.toMillis(now - previous.get());
253 >                        if (done.getCount() == cycles) { // first execution
254 >                            if (elapsedMillis >= d)
255 >                                tryLongerDelay.set(true);
256 >                        } else {
257 >                            assertTrue(elapsedMillis >= d);
258 >                            if (elapsedMillis >= 2 * d)
259 >                                tryLongerDelay.set(true);
260 >                        }
261 >                        previous.set(now);
262 >                        done.countDown();
263 >                    }};
264 >                final ScheduledFuture periodicTask =
265                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
266 <                await(done);
266 >                final int totalDelayMillis = (cycles - 1) * delay;
267 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
268                  periodicTask.cancel(true);
269 <                double elapsedDelays =
270 <                    (double) millisElapsedSince(startTime) / delay;
271 <                if (elapsedDelays >= cycles - 1 &&
246 <                    elapsedDelays <= cycles)
269 >                final long elapsedMillis = millisElapsedSince(startTime);
270 >                assertTrue(elapsedMillis >= totalDelayMillis);
271 >                if (!tryLongerDelay.get())
272                      return;
273 +                // else retry with longer delay
274              }
275              fail("unexpected execution rate");
276          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines