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.78 by jsr166, Sun Oct 25 02:58:25 2015 UTC vs.
Revision 1.83 by jsr166, Wed Jan 4 06:09:58 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 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 h =
161 >                final ScheduledFuture periodicTask =
162                      p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
163 <                await(done);
164 <                h.cancel(true);
165 <                double normalizedTime =
166 <                    (double) millisElapsedSince(startTime) / delay;
167 <                if (normalizedTime >= cycles - 1 &&
168 <                    normalizedTime <= cycles)
163 >                final int totalDelayMillis = (cycles - 1) * delay;
164 >                await(done, totalDelayMillis + LONG_DELAY_MS);
165 >                periodicTask.cancel(true);
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 h =
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);
210 <                h.cancel(true);
211 <                double normalizedTime =
212 <                    (double) millisElapsedSince(startTime) / delay;
213 <                if (normalizedTime >= cycles - 1 &&
214 <                    normalizedTime <= cycles)
209 >                final int totalDelayMillis = (cycles - 1) * delay;
210 >                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
211 >                periodicTask.cancel(true);
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 485 | Line 510 | public class ScheduledExecutorTest exten
510       * isShutdown is false before shutdown, true after
511       */
512      public void testIsShutdown() {
488
513          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
514 <        try {
515 <            assertFalse(p.isShutdown());
516 <        }
517 <        finally {
518 <            try { p.shutdown(); } catch (SecurityException ok) { return; }
514 >        assertFalse(p.isShutdown());
515 >        try (PoolCleaner cleaner = cleaner(p)) {
516 >            try {
517 >                p.shutdown();
518 >                assertTrue(p.isShutdown());
519 >            } catch (SecurityException ok) {}
520          }
496        assertTrue(p.isShutdown());
521      }
522  
523      /**
# Line 864 | Line 888 | public class ScheduledExecutorTest exten
888          CountDownLatch latch = new CountDownLatch(1);
889          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
890          try (PoolCleaner cleaner = cleaner(e)) {
891 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
891 >            List<Callable<String>> l = new ArrayList<>();
892              l.add(latchAwaitingStringTask(latch));
893              l.add(null);
894              try {
# Line 881 | Line 905 | public class ScheduledExecutorTest exten
905      public void testInvokeAny4() throws Exception {
906          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
907          try (PoolCleaner cleaner = cleaner(e)) {
908 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
908 >            List<Callable<String>> l = new ArrayList<>();
909              l.add(new NPETask());
910              try {
911                  e.invokeAny(l);
# Line 898 | Line 922 | public class ScheduledExecutorTest exten
922      public void testInvokeAny5() throws Exception {
923          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
924          try (PoolCleaner cleaner = cleaner(e)) {
925 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
925 >            List<Callable<String>> l = new ArrayList<>();
926              l.add(new StringTask());
927              l.add(new StringTask());
928              String result = e.invokeAny(l);
# Line 936 | Line 960 | public class ScheduledExecutorTest exten
960      public void testInvokeAll3() throws Exception {
961          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
962          try (PoolCleaner cleaner = cleaner(e)) {
963 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
963 >            List<Callable<String>> l = new ArrayList<>();
964              l.add(new StringTask());
965              l.add(null);
966              try {
# Line 952 | Line 976 | public class ScheduledExecutorTest exten
976      public void testInvokeAll4() throws Exception {
977          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
978          try (PoolCleaner cleaner = cleaner(e)) {
979 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
979 >            List<Callable<String>> l = new ArrayList<>();
980              l.add(new NPETask());
981              List<Future<String>> futures = e.invokeAll(l);
982              assertEquals(1, futures.size());
# Line 971 | Line 995 | public class ScheduledExecutorTest exten
995      public void testInvokeAll5() throws Exception {
996          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
997          try (PoolCleaner cleaner = cleaner(e)) {
998 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
998 >            List<Callable<String>> l = new ArrayList<>();
999              l.add(new StringTask());
1000              l.add(new StringTask());
1001              List<Future<String>> futures = e.invokeAll(l);
# Line 1000 | Line 1024 | public class ScheduledExecutorTest exten
1024      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1025          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1026          try (PoolCleaner cleaner = cleaner(e)) {
1027 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1027 >            List<Callable<String>> l = new ArrayList<>();
1028              l.add(new StringTask());
1029              try {
1030                  e.invokeAny(l, MEDIUM_DELAY_MS, null);
# Line 1029 | Line 1053 | public class ScheduledExecutorTest exten
1053          CountDownLatch latch = new CountDownLatch(1);
1054          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1055          try (PoolCleaner cleaner = cleaner(e)) {
1056 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1056 >            List<Callable<String>> l = new ArrayList<>();
1057              l.add(latchAwaitingStringTask(latch));
1058              l.add(null);
1059              try {
# Line 1047 | Line 1071 | public class ScheduledExecutorTest exten
1071          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1072          try (PoolCleaner cleaner = cleaner(e)) {
1073              long startTime = System.nanoTime();
1074 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1074 >            List<Callable<String>> l = new ArrayList<>();
1075              l.add(new NPETask());
1076              try {
1077                  e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1066 | Line 1090 | public class ScheduledExecutorTest exten
1090          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1091          try (PoolCleaner cleaner = cleaner(e)) {
1092              long startTime = System.nanoTime();
1093 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1093 >            List<Callable<String>> l = new ArrayList<>();
1094              l.add(new StringTask());
1095              l.add(new StringTask());
1096              String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1094 | Line 1118 | public class ScheduledExecutorTest exten
1118      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1119          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1120          try (PoolCleaner cleaner = cleaner(e)) {
1121 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1121 >            List<Callable<String>> l = new ArrayList<>();
1122              l.add(new StringTask());
1123              try {
1124                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 1121 | Line 1145 | public class ScheduledExecutorTest exten
1145      public void testTimedInvokeAll3() throws Exception {
1146          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1147          try (PoolCleaner cleaner = cleaner(e)) {
1148 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1148 >            List<Callable<String>> l = new ArrayList<>();
1149              l.add(new StringTask());
1150              l.add(null);
1151              try {
# Line 1137 | Line 1161 | public class ScheduledExecutorTest exten
1161      public void testTimedInvokeAll4() throws Exception {
1162          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1163          try (PoolCleaner cleaner = cleaner(e)) {
1164 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1164 >            List<Callable<String>> l = new ArrayList<>();
1165              l.add(new NPETask());
1166              List<Future<String>> futures =
1167                  e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
# Line 1157 | Line 1181 | public class ScheduledExecutorTest exten
1181      public void testTimedInvokeAll5() throws Exception {
1182          final ExecutorService e = new ScheduledThreadPoolExecutor(2);
1183          try (PoolCleaner cleaner = cleaner(e)) {
1184 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1184 >            List<Callable<String>> l = new ArrayList<>();
1185              l.add(new StringTask());
1186              l.add(new StringTask());
1187              List<Future<String>> futures =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines