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.56 by jsr166, Tue Oct 6 16:39:06 2015 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 h =
218 >                final ScheduledFuture periodicTask =
219                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
220 <                await(done);
221 <                h.cancel(true);
222 <                double normalizedTime =
223 <                    (double) millisElapsedSince(startTime) / delay;
224 <                if (normalizedTime >= cycles - 1 &&
225 <                    normalizedTime <= cycles)
220 >                final int totalDelayMillis = (cycles - 1) * delay;
221 >                await(done, totalDelayMillis + LONG_DELAY_MS);
222 >                periodicTask.cancel(true);
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 <            throw new AssertionError("unexpected execution rate");
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 h =
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);
267 <                h.cancel(true);
268 <                double normalizedTime =
269 <                    (double) millisElapsedSince(startTime) / delay;
270 <                if (normalizedTime >= cycles - 1 &&
271 <                    normalizedTime <= cycles)
266 >                final int totalDelayMillis = (cycles - 1) * delay;
267 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
268 >                periodicTask.cancel(true);
269 >                final long elapsedMillis = millisElapsedSince(startTime);
270 >                assertTrue(elapsedMillis >= totalDelayMillis);
271 >                if (!tryLongerDelay.get())
272                      return;
273 +                // else retry with longer delay
274              }
275 <            throw new AssertionError("unexpected execution rate");
275 >            fail("unexpected execution rate");
276          }
277      }
278  
# Line 1097 | Line 1123 | public class ScheduledExecutorSubclassTe
1123      public void testTimedInvokeAny4() throws Exception {
1124          final ExecutorService e = new CustomExecutor(2);
1125          try (PoolCleaner cleaner = cleaner(e)) {
1126 +            long startTime = System.nanoTime();
1127              List<Callable<String>> l = new ArrayList<Callable<String>>();
1128              l.add(new NPETask());
1129              try {
1130 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1130 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1131                  shouldThrow();
1132              } catch (ExecutionException success) {
1133                  assertTrue(success.getCause() instanceof NullPointerException);
1134              }
1135 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1136          }
1137      }
1138  
# Line 1114 | Line 1142 | public class ScheduledExecutorSubclassTe
1142      public void testTimedInvokeAny5() throws Exception {
1143          final ExecutorService e = new CustomExecutor(2);
1144          try (PoolCleaner cleaner = cleaner(e)) {
1145 +            long startTime = System.nanoTime();
1146              List<Callable<String>> l = new ArrayList<Callable<String>>();
1147              l.add(new StringTask());
1148              l.add(new StringTask());
1149 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1149 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1150              assertSame(TEST_STRING, result);
1151 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1152          }
1153      }
1154  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines