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; |
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; |
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; |
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 |
|
} |
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 |
|
/** |
814 |
|
assertTrue(periodic.isDone()); |
815 |
|
} |
816 |
|
} |
817 |
+ |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
818 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
819 |
|
assertTrue(p.isTerminated()); |
820 |
|
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
889 |
|
CountDownLatch latch = new CountDownLatch(1); |
890 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
891 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
892 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
892 |
> |
List<Callable<String>> l = new ArrayList<>(); |
893 |
|
l.add(latchAwaitingStringTask(latch)); |
894 |
|
l.add(null); |
895 |
|
try { |
906 |
|
public void testInvokeAny4() throws Exception { |
907 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
908 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
909 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
909 |
> |
List<Callable<String>> l = new ArrayList<>(); |
910 |
|
l.add(new NPETask()); |
911 |
|
try { |
912 |
|
e.invokeAny(l); |
923 |
|
public void testInvokeAny5() throws Exception { |
924 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
925 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
926 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
926 |
> |
List<Callable<String>> l = new ArrayList<>(); |
927 |
|
l.add(new StringTask()); |
928 |
|
l.add(new StringTask()); |
929 |
|
String result = e.invokeAny(l); |
961 |
|
public void testInvokeAll3() throws Exception { |
962 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
963 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
964 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
964 |
> |
List<Callable<String>> l = new ArrayList<>(); |
965 |
|
l.add(new StringTask()); |
966 |
|
l.add(null); |
967 |
|
try { |
977 |
|
public void testInvokeAll4() throws Exception { |
978 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
979 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
980 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
980 |
> |
List<Callable<String>> l = new ArrayList<>(); |
981 |
|
l.add(new NPETask()); |
982 |
|
List<Future<String>> futures = e.invokeAll(l); |
983 |
|
assertEquals(1, futures.size()); |
996 |
|
public void testInvokeAll5() throws Exception { |
997 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
998 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
999 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1000 |
|
l.add(new StringTask()); |
1001 |
|
l.add(new StringTask()); |
1002 |
|
List<Future<String>> futures = e.invokeAll(l); |
1025 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1026 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1027 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1028 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1028 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1029 |
|
l.add(new StringTask()); |
1030 |
|
try { |
1031 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1054 |
|
CountDownLatch latch = new CountDownLatch(1); |
1055 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1056 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1057 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1057 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1058 |
|
l.add(latchAwaitingStringTask(latch)); |
1059 |
|
l.add(null); |
1060 |
|
try { |
1072 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1073 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1074 |
|
long startTime = System.nanoTime(); |
1075 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1075 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1076 |
|
l.add(new NPETask()); |
1077 |
|
try { |
1078 |
|
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1091 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1092 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1093 |
|
long startTime = System.nanoTime(); |
1094 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1094 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1095 |
|
l.add(new StringTask()); |
1096 |
|
l.add(new StringTask()); |
1097 |
|
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1119 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1120 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1121 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1122 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1122 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1123 |
|
l.add(new StringTask()); |
1124 |
|
try { |
1125 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1146 |
|
public void testTimedInvokeAll3() throws Exception { |
1147 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1148 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1149 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1149 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1150 |
|
l.add(new StringTask()); |
1151 |
|
l.add(null); |
1152 |
|
try { |
1162 |
|
public void testTimedInvokeAll4() throws Exception { |
1163 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1164 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1165 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1165 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1166 |
|
l.add(new NPETask()); |
1167 |
|
List<Future<String>> futures = |
1168 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1182 |
|
public void testTimedInvokeAll5() throws Exception { |
1183 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1184 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1185 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1185 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1186 |
|
l.add(new StringTask()); |
1187 |
|
l.add(new StringTask()); |
1188 |
|
List<Future<String>> futures = |