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.79 by jsr166, Mon Feb 22 20:40:12 2016 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 periodicTask =
162 >                final ScheduledFuture periodicTask =
163                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
164 <                await(done);
164 >                final int totalDelayMillis = (cycles - 1) * delay;
165 >                await(done, totalDelayMillis + LONG_DELAY_MS);
166                  periodicTask.cancel(true);
167 <                double elapsedDelays =
168 <                    (double) millisElapsedSince(startTime) / delay;
169 <                if (elapsedDelays >= cycles - 1 &&
164 <                    elapsedDelays <= cycles)
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              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 periodicTask =
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);
210 >                final int totalDelayMillis = (cycles - 1) * delay;
211 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
212                  periodicTask.cancel(true);
213 <                double elapsedDelays =
214 <                    (double) millisElapsedSince(startTime) / delay;
215 <                if (elapsedDelays >= cycles - 1 &&
190 <                    elapsedDelays <= cycles)
213 >                final long elapsedMillis = millisElapsedSince(startTime);
214 >                assertTrue(elapsedMillis >= totalDelayMillis);
215 >                if (!tryLongerDelay.get())
216                      return;
217 +                // else retry with longer delay
218              }
219              fail("unexpected execution rate");
220          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines