ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.76 by jsr166, Thu Oct 8 03:08:37 2015 UTC vs.
Revision 1.80 by jsr166, Mon Feb 22 23:16:06 2016 UTC

# Line 7 | Line 7
7   */
8  
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
11   import static java.util.concurrent.TimeUnit.SECONDS;
12  
13   import java.util.ArrayList;
# Line 25 | Line 26 | import java.util.concurrent.ScheduledFut
26   import java.util.concurrent.ScheduledThreadPoolExecutor;
27   import java.util.concurrent.ThreadFactory;
28   import java.util.concurrent.ThreadPoolExecutor;
29 + import java.util.concurrent.atomic.AtomicBoolean;
30   import java.util.concurrent.atomic.AtomicInteger;
31 + import java.util.concurrent.atomic.AtomicLong;
32  
33   import junit.framework.Test;
34   import junit.framework.TestSuite;
# Line 143 | Line 146 | public class ScheduledExecutorTest exten
146      }
147  
148      /**
149 <     * scheduleAtFixedRate executes series of tasks at given rate
149 >     * scheduleAtFixedRate executes series of tasks at given rate.
150 >     * Eventually, it must hold that:
151 >     *   cycles - 1 <= elapsedMillis/delay < cycles
152       */
153      public void testFixedRateSequence() throws InterruptedException {
154          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
155          try (PoolCleaner cleaner = cleaner(p)) {
156              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
157 <                long startTime = System.nanoTime();
158 <                int cycles = 10;
157 >                final long startTime = System.nanoTime();
158 >                final int cycles = 8;
159                  final CountDownLatch done = new CountDownLatch(cycles);
160 <                Runnable task = new CheckedRunnable() {
160 >                final Runnable task = new CheckedRunnable() {
161                      public void realRun() { done.countDown(); }};
162 <                ScheduledFuture h =
162 >                final ScheduledFuture periodicTask =
163                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
164 <                await(done);
165 <                h.cancel(true);
166 <                double normalizedTime =
167 <                    (double) millisElapsedSince(startTime) / delay;
168 <                if (normalizedTime >= cycles - 1 &&
169 <                    normalizedTime <= cycles)
164 >                final int totalDelayMillis = (cycles - 1) * delay;
165 >                await(done, totalDelayMillis + LONG_DELAY_MS);
166 >                periodicTask.cancel(true);
167 >                final long elapsedMillis = millisElapsedSince(startTime);
168 >                assertTrue(elapsedMillis >= totalDelayMillis);
169 >                if (elapsedMillis <= cycles * delay)
170                      return;
171 +                // else retry with longer delay
172              }
173 <            throw new AssertionError("unexpected execution rate");
173 >            fail("unexpected execution rate");
174          }
175      }
176  
177      /**
178 <     * scheduleWithFixedDelay executes series of tasks with given period
178 >     * scheduleWithFixedDelay executes series of tasks with given period.
179 >     * Eventually, it must hold that each task starts at least delay and at
180 >     * most 2 * delay after the termination of the previous task.
181       */
182      public void testFixedDelaySequence() throws InterruptedException {
183          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
184          try (PoolCleaner cleaner = cleaner(p)) {
185              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
186 <                long startTime = System.nanoTime();
187 <                int cycles = 10;
186 >                final long startTime = System.nanoTime();
187 >                final AtomicLong previous = new AtomicLong(startTime);
188 >                final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
189 >                final int cycles = 8;
190                  final CountDownLatch done = new CountDownLatch(cycles);
191 <                Runnable task = new CheckedRunnable() {
192 <                    public void realRun() { done.countDown(); }};
193 <                ScheduledFuture h =
191 >                final int d = delay;
192 >                final Runnable task = new CheckedRunnable() {
193 >                    public void realRun() {
194 >                        long now = System.nanoTime();
195 >                        long elapsedMillis
196 >                            = NANOSECONDS.toMillis(now - previous.get());
197 >                        if (done.getCount() == cycles) { // first execution
198 >                            if (elapsedMillis >= d)
199 >                                tryLongerDelay.set(true);
200 >                        } else {
201 >                            assertTrue(elapsedMillis >= d);
202 >                            if (elapsedMillis >= 2 * d)
203 >                                tryLongerDelay.set(true);
204 >                        }
205 >                        previous.set(now);
206 >                        done.countDown();
207 >                    }};
208 >                final ScheduledFuture periodicTask =
209                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
210 <                await(done);
211 <                h.cancel(true);
212 <                double normalizedTime =
213 <                    (double) millisElapsedSince(startTime) / delay;
214 <                if (normalizedTime >= cycles - 1 &&
215 <                    normalizedTime <= cycles)
210 >                final int totalDelayMillis = (cycles - 1) * delay;
211 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
212 >                periodicTask.cancel(true);
213 >                final long elapsedMillis = millisElapsedSince(startTime);
214 >                assertTrue(elapsedMillis >= totalDelayMillis);
215 >                if (!tryLongerDelay.get())
216                      return;
217 +                // else retry with longer delay
218              }
219 <            throw new AssertionError("unexpected execution rate");
219 >            fail("unexpected execution rate");
220          }
221      }
222  
# Line 1206 | Line 1232 | public class ScheduledExecutorTest exten
1232          }
1233      }
1234  
1235 +    /**
1236 +     * A fixed delay task with overflowing period should not prevent a
1237 +     * one-shot task from executing.
1238 +     * https://bugs.openjdk.java.net/browse/JDK-8051859
1239 +     */
1240 +    public void testScheduleWithFixedDelay_overflow() throws Exception {
1241 +        final CountDownLatch delayedDone = new CountDownLatch(1);
1242 +        final CountDownLatch immediateDone = new CountDownLatch(1);
1243 +        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
1244 +        try (PoolCleaner cleaner = cleaner(p)) {
1245 +            final Runnable immediate = new Runnable() { public void run() {
1246 +                immediateDone.countDown();
1247 +            }};
1248 +            final Runnable delayed = new Runnable() { public void run() {
1249 +                delayedDone.countDown();
1250 +                p.submit(immediate);
1251 +            }};
1252 +            p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS);
1253 +            await(delayedDone);
1254 +            await(immediateDone);
1255 +        }
1256 +    }
1257 +
1258   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines