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.86 by jsr166, Sat Mar 25 21:41:10 2017 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 17 | Line 18 | import java.util.concurrent.Callable;
18   import java.util.concurrent.CancellationException;
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.Future;
23   import java.util.concurrent.RejectedExecutionException;
# Line 25 | Line 25 | import java.util.concurrent.ScheduledFut
25   import java.util.concurrent.ScheduledThreadPoolExecutor;
26   import java.util.concurrent.ThreadFactory;
27   import java.util.concurrent.ThreadPoolExecutor;
28 + import java.util.concurrent.atomic.AtomicBoolean;
29   import java.util.concurrent.atomic.AtomicInteger;
30 + import java.util.concurrent.atomic.AtomicLong;
31  
32   import junit.framework.Test;
33   import junit.framework.TestSuite;
# Line 48 | Line 50 | public class ScheduledExecutorTest exten
50              final Runnable task = new CheckedRunnable() {
51                  public void realRun() { done.countDown(); }};
52              p.execute(task);
53 <            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
53 >            await(done);
54          }
55      }
56  
# Line 143 | Line 145 | public class ScheduledExecutorTest exten
145      }
146  
147      /**
148 <     * scheduleAtFixedRate executes series of tasks at given rate
148 >     * scheduleAtFixedRate executes series of tasks at given rate.
149 >     * Eventually, it must hold that:
150 >     *   cycles - 1 <= elapsedMillis/delay < cycles
151       */
152      public void testFixedRateSequence() throws InterruptedException {
153          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
154          try (PoolCleaner cleaner = cleaner(p)) {
155              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
156 <                long startTime = System.nanoTime();
157 <                int cycles = 10;
156 >                final long startTime = System.nanoTime();
157 >                final int cycles = 8;
158                  final CountDownLatch done = new CountDownLatch(cycles);
159 <                Runnable task = new CheckedRunnable() {
159 >                final Runnable task = new CheckedRunnable() {
160                      public void realRun() { done.countDown(); }};
161 <                ScheduledFuture periodicTask =
161 >                final ScheduledFuture periodicTask =
162                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
163 <                await(done);
163 >                final int totalDelayMillis = (cycles - 1) * delay;
164 >                await(done, totalDelayMillis + LONG_DELAY_MS);
165                  periodicTask.cancel(true);
166 <                double elapsedDelays =
167 <                    (double) millisElapsedSince(startTime) / delay;
168 <                if (elapsedDelays >= cycles - 1 &&
164 <                    elapsedDelays <= cycles)
166 >                final long elapsedMillis = millisElapsedSince(startTime);
167 >                assertTrue(elapsedMillis >= totalDelayMillis);
168 >                if (elapsedMillis <= cycles * delay)
169                      return;
170 +                // else retry with longer delay
171              }
172              fail("unexpected execution rate");
173          }
174      }
175  
176      /**
177 <     * scheduleWithFixedDelay executes series of tasks with given period
177 >     * scheduleWithFixedDelay executes series of tasks with given period.
178 >     * Eventually, it must hold that each task starts at least delay and at
179 >     * most 2 * delay after the termination of the previous task.
180       */
181      public void testFixedDelaySequence() throws InterruptedException {
182          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
183          try (PoolCleaner cleaner = cleaner(p)) {
184              for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
185 <                long startTime = System.nanoTime();
186 <                int cycles = 10;
185 >                final long startTime = System.nanoTime();
186 >                final AtomicLong previous = new AtomicLong(startTime);
187 >                final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
188 >                final int cycles = 8;
189                  final CountDownLatch done = new CountDownLatch(cycles);
190 <                Runnable task = new CheckedRunnable() {
191 <                    public void realRun() { done.countDown(); }};
192 <                ScheduledFuture periodicTask =
190 >                final int d = delay;
191 >                final Runnable task = new CheckedRunnable() {
192 >                    public void realRun() {
193 >                        long now = System.nanoTime();
194 >                        long elapsedMillis
195 >                            = NANOSECONDS.toMillis(now - previous.get());
196 >                        if (done.getCount() == cycles) { // first execution
197 >                            if (elapsedMillis >= d)
198 >                                tryLongerDelay.set(true);
199 >                        } else {
200 >                            assertTrue(elapsedMillis >= d);
201 >                            if (elapsedMillis >= 2 * d)
202 >                                tryLongerDelay.set(true);
203 >                        }
204 >                        previous.set(now);
205 >                        done.countDown();
206 >                    }};
207 >                final ScheduledFuture periodicTask =
208                      p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
209 <                await(done);
209 >                final int totalDelayMillis = (cycles - 1) * delay;
210 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
211                  periodicTask.cancel(true);
212 <                double elapsedDelays =
213 <                    (double) millisElapsedSince(startTime) / delay;
214 <                if (elapsedDelays >= cycles - 1 &&
190 <                    elapsedDelays <= cycles)
212 >                final long elapsedMillis = millisElapsedSince(startTime);
213 >                assertTrue(elapsedMillis >= totalDelayMillis);
214 >                if (!tryLongerDelay.get())
215                      return;
216 +                // else retry with longer delay
217              }
218              fail("unexpected execution rate");
219          }
# Line 337 | Line 362 | public class ScheduledExecutorTest exten
362                  public void realRun() throws InterruptedException {
363                      threadStarted.countDown();
364                      assertEquals(0, p.getCompletedTaskCount());
365 <                    threadProceed.await();
365 >                    await(threadProceed);
366                      threadDone.countDown();
367                  }});
368              await(threadStarted);
369              assertEquals(0, p.getCompletedTaskCount());
370              threadProceed.countDown();
371 <            threadDone.await();
371 >            await(threadDone);
372              long startTime = System.nanoTime();
373              while (p.getCompletedTaskCount() != 1) {
374                  if (millisElapsedSince(startTime) > LONG_DELAY_MS)
# Line 482 | Line 507 | public class ScheduledExecutorTest exten
507      }
508  
509      /**
510 +     * The default rejected execution handler is AbortPolicy.
511 +     */
512 +    public void testDefaultRejectedExecutionHandler() {
513 +        final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
514 +        try (PoolCleaner cleaner = cleaner(p)) {
515 +            assertTrue(p.getRejectedExecutionHandler()
516 +                       instanceof ThreadPoolExecutor.AbortPolicy);
517 +        }
518 +    }
519 +
520 +    /**
521       * isShutdown is false before shutdown, true after
522       */
523      public void testIsShutdown() {
488
524          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
525 <        try {
526 <            assertFalse(p.isShutdown());
527 <        }
528 <        finally {
529 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
525 >        assertFalse(p.isShutdown());
526 >        try (PoolCleaner cleaner = cleaner(p)) {
527 >            try {
528 >                p.shutdown();
529 >                assertTrue(p.isShutdown());
530 >            } catch (SecurityException ok) {}
531          }
496        assertTrue(p.isShutdown());
532      }
533  
534      /**
# Line 738 | Line 773 | public class ScheduledExecutorTest exten
773          Runnable task = new CheckedRunnable() { public void realRun()
774                                                      throws InterruptedException {
775              poolBlocked.countDown();
776 <            assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
776 >            await(unblock);
777              ran.getAndIncrement();
778          }};
779          List<Future<?>> blockers = new ArrayList<>();
# Line 746 | Line 781 | public class ScheduledExecutorTest exten
781          List<Future<?>> delayeds = new ArrayList<>();
782          for (int i = 0; i < poolSize; i++)
783              blockers.add(p.submit(task));
784 <        assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
784 >        await(poolBlocked);
785  
786          periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
787                                              1, 1, MILLISECONDS));
# Line 782 | Line 817 | public class ScheduledExecutorTest exten
817              }
818          }
819          if (effectivePeriodicPolicy) {
820 <            assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
821 <            assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
820 >            await(periodicLatch1);
821 >            await(periodicLatch2);
822              for (Future<?> periodic : periodics) {
823                  assertTrue(periodic.cancel(false));
824                  assertTrue(periodic.isCancelled());
825                  assertTrue(periodic.isDone());
826              }
827          }
828 +        for (Future<?> blocker : blockers) assertNull(blocker.get());
829          assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
830          assertTrue(p.isTerminated());
831          assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
# Line 864 | Line 900 | public class ScheduledExecutorTest exten
900          CountDownLatch latch = new CountDownLatch(1);
901          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
902          try (PoolCleaner cleaner = cleaner(e)) {
903 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
903 >            List<Callable<String>> l = new ArrayList<>();
904              l.add(latchAwaitingStringTask(latch));
905              l.add(null);
906              try {
# Line 881 | Line 917 | public class ScheduledExecutorTest exten
917      public void testInvokeAny4() throws Exception {
918          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
919          try (PoolCleaner cleaner = cleaner(e)) {
920 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
920 >            List<Callable<String>> l = new ArrayList<>();
921              l.add(new NPETask());
922              try {
923                  e.invokeAny(l);
# Line 898 | Line 934 | public class ScheduledExecutorTest exten
934      public void testInvokeAny5() throws Exception {
935          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
936          try (PoolCleaner cleaner = cleaner(e)) {
937 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
937 >            List<Callable<String>> l = new ArrayList<>();
938              l.add(new StringTask());
939              l.add(new StringTask());
940              String result = e.invokeAny(l);
# Line 936 | Line 972 | public class ScheduledExecutorTest exten
972      public void testInvokeAll3() throws Exception {
973          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
974          try (PoolCleaner cleaner = cleaner(e)) {
975 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
975 >            List<Callable<String>> l = new ArrayList<>();
976              l.add(new StringTask());
977              l.add(null);
978              try {
# Line 952 | Line 988 | public class ScheduledExecutorTest exten
988      public void testInvokeAll4() throws Exception {
989          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
990          try (PoolCleaner cleaner = cleaner(e)) {
991 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
991 >            List<Callable<String>> l = new ArrayList<>();
992              l.add(new NPETask());
993              List<Future<String>> futures = e.invokeAll(l);
994              assertEquals(1, futures.size());
# Line 971 | Line 1007 | public class ScheduledExecutorTest exten
1007      public void testInvokeAll5() throws Exception {
1008          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1009          try (PoolCleaner cleaner = cleaner(e)) {
1010 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1010 >            List<Callable<String>> l = new ArrayList<>();
1011              l.add(new StringTask());
1012              l.add(new StringTask());
1013              List<Future<String>> futures = e.invokeAll(l);
# Line 1000 | Line 1036 | public class ScheduledExecutorTest exten
1036      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1037          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1038          try (PoolCleaner cleaner = cleaner(e)) {
1039 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1039 >            List<Callable<String>> l = new ArrayList<>();
1040              l.add(new StringTask());
1041              try {
1042                  e.invokeAny(l, MEDIUM_DELAY_MS, null);
# Line 1029 | Line 1065 | public class ScheduledExecutorTest exten
1065          CountDownLatch latch = new CountDownLatch(1);
1066          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1067          try (PoolCleaner cleaner = cleaner(e)) {
1068 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1068 >            List<Callable<String>> l = new ArrayList<>();
1069              l.add(latchAwaitingStringTask(latch));
1070              l.add(null);
1071              try {
# Line 1047 | Line 1083 | public class ScheduledExecutorTest exten
1083          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1084          try (PoolCleaner cleaner = cleaner(e)) {
1085              long startTime = System.nanoTime();
1086 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1086 >            List<Callable<String>> l = new ArrayList<>();
1087              l.add(new NPETask());
1088              try {
1089                  e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1066 | Line 1102 | public class ScheduledExecutorTest exten
1102          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1103          try (PoolCleaner cleaner = cleaner(e)) {
1104              long startTime = System.nanoTime();
1105 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1105 >            List<Callable<String>> l = new ArrayList<>();
1106              l.add(new StringTask());
1107              l.add(new StringTask());
1108              String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1094 | Line 1130 | public class ScheduledExecutorTest exten
1130      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1131          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1132          try (PoolCleaner cleaner = cleaner(e)) {
1133 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1133 >            List<Callable<String>> l = new ArrayList<>();
1134              l.add(new StringTask());
1135              try {
1136                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 1121 | Line 1157 | public class ScheduledExecutorTest exten
1157      public void testTimedInvokeAll3() throws Exception {
1158          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1159          try (PoolCleaner cleaner = cleaner(e)) {
1160 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1160 >            List<Callable<String>> l = new ArrayList<>();
1161              l.add(new StringTask());
1162              l.add(null);
1163              try {
# Line 1137 | Line 1173 | public class ScheduledExecutorTest exten
1173      public void testTimedInvokeAll4() throws Exception {
1174          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1175          try (PoolCleaner cleaner = cleaner(e)) {
1176 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1176 >            List<Callable<String>> l = new ArrayList<>();
1177              l.add(new NPETask());
1178              List<Future<String>> futures =
1179                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1157 | Line 1193 | public class ScheduledExecutorTest exten
1193      public void testTimedInvokeAll5() throws Exception {
1194          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1195          try (PoolCleaner cleaner = cleaner(e)) {
1196 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1196 >            List<Callable<String>> l = new ArrayList<>();
1197              l.add(new StringTask());
1198              l.add(new StringTask());
1199              List<Future<String>> futures =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines