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; |
17 |
|
import java.util.concurrent.CountDownLatch; |
18 |
|
import java.util.concurrent.Delayed; |
19 |
|
import java.util.concurrent.ExecutionException; |
19 |
– |
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.Future; |
22 |
|
import java.util.concurrent.RejectedExecutionException; |
28 |
|
import java.util.concurrent.ThreadPoolExecutor; |
29 |
|
import java.util.concurrent.TimeoutException; |
30 |
|
import java.util.concurrent.TimeUnit; |
31 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
32 |
|
import java.util.concurrent.atomic.AtomicInteger; |
33 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
34 |
|
|
35 |
|
import junit.framework.Test; |
36 |
|
import junit.framework.TestSuite; |
44 |
|
} |
45 |
|
|
46 |
|
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
47 |
< |
RunnableScheduledFuture<V> task; |
47 |
> |
private final RunnableScheduledFuture<V> task; |
48 |
|
volatile boolean ran; |
49 |
< |
CustomTask(RunnableScheduledFuture<V> t) { task = t; } |
49 |
> |
CustomTask(RunnableScheduledFuture<V> task) { this.task = task; } |
50 |
|
public boolean isPeriodic() { return task.isPeriodic(); } |
51 |
|
public void run() { |
52 |
|
ran = true; |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
204 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
205 |
> |
* Eventually, it must hold that: |
206 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
207 |
|
*/ |
208 |
|
public void testFixedRateSequence() throws InterruptedException { |
209 |
|
final CustomExecutor p = new CustomExecutor(1); |
210 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
211 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
212 |
< |
long startTime = System.nanoTime(); |
213 |
< |
int cycles = 10; |
212 |
> |
final long startTime = System.nanoTime(); |
213 |
> |
final int cycles = 8; |
214 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
215 |
< |
Runnable task = new CheckedRunnable() { |
215 |
> |
final Runnable task = new CheckedRunnable() { |
216 |
|
public void realRun() { done.countDown(); }}; |
217 |
< |
ScheduledFuture h = |
217 |
> |
final ScheduledFuture periodicTask = |
218 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
219 |
< |
await(done); |
220 |
< |
h.cancel(true); |
221 |
< |
double normalizedTime = |
222 |
< |
(double) millisElapsedSince(startTime) / delay; |
223 |
< |
if (normalizedTime >= cycles - 1 && |
224 |
< |
normalizedTime <= cycles) |
219 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
220 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
221 |
> |
periodicTask.cancel(true); |
222 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
223 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
224 |
> |
if (elapsedMillis <= cycles * delay) |
225 |
|
return; |
226 |
+ |
// else retry with longer delay |
227 |
|
} |
228 |
< |
throw new AssertionError("unexpected execution rate"); |
228 |
> |
fail("unexpected execution rate"); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
233 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
234 |
> |
* Eventually, it must hold that each task starts at least delay and at |
235 |
> |
* most 2 * delay after the termination of the previous task. |
236 |
|
*/ |
237 |
|
public void testFixedDelaySequence() throws InterruptedException { |
238 |
|
final CustomExecutor p = new CustomExecutor(1); |
239 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
240 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
241 |
< |
long startTime = System.nanoTime(); |
242 |
< |
int cycles = 10; |
241 |
> |
final long startTime = System.nanoTime(); |
242 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
243 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
244 |
> |
final int cycles = 8; |
245 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
246 |
< |
Runnable task = new CheckedRunnable() { |
247 |
< |
public void realRun() { done.countDown(); }}; |
248 |
< |
ScheduledFuture h = |
246 |
> |
final int d = delay; |
247 |
> |
final Runnable task = new CheckedRunnable() { |
248 |
> |
public void realRun() { |
249 |
> |
long now = System.nanoTime(); |
250 |
> |
long elapsedMillis |
251 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
252 |
> |
if (done.getCount() == cycles) { // first execution |
253 |
> |
if (elapsedMillis >= d) |
254 |
> |
tryLongerDelay.set(true); |
255 |
> |
} else { |
256 |
> |
assertTrue(elapsedMillis >= d); |
257 |
> |
if (elapsedMillis >= 2 * d) |
258 |
> |
tryLongerDelay.set(true); |
259 |
> |
} |
260 |
> |
previous.set(now); |
261 |
> |
done.countDown(); |
262 |
> |
}}; |
263 |
> |
final ScheduledFuture periodicTask = |
264 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
265 |
< |
await(done); |
266 |
< |
h.cancel(true); |
267 |
< |
double normalizedTime = |
268 |
< |
(double) millisElapsedSince(startTime) / delay; |
269 |
< |
if (normalizedTime >= cycles - 1 && |
270 |
< |
normalizedTime <= cycles) |
265 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
266 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
267 |
> |
periodicTask.cancel(true); |
268 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
269 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
270 |
> |
if (!tryLongerDelay.get()) |
271 |
|
return; |
272 |
+ |
// else retry with longer delay |
273 |
|
} |
274 |
< |
throw new AssertionError("unexpected execution rate"); |
274 |
> |
fail("unexpected execution rate"); |
275 |
|
} |
276 |
|
} |
277 |
|
|
940 |
|
final CountDownLatch latch = new CountDownLatch(1); |
941 |
|
final ExecutorService e = new CustomExecutor(2); |
942 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
943 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
943 |
> |
List<Callable<String>> l = new ArrayList<>(); |
944 |
|
l.add(latchAwaitingStringTask(latch)); |
945 |
|
l.add(null); |
946 |
|
try { |
957 |
|
public void testInvokeAny4() throws Exception { |
958 |
|
final ExecutorService e = new CustomExecutor(2); |
959 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
960 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
960 |
> |
List<Callable<String>> l = new ArrayList<>(); |
961 |
|
l.add(new NPETask()); |
962 |
|
try { |
963 |
|
e.invokeAny(l); |
974 |
|
public void testInvokeAny5() throws Exception { |
975 |
|
final ExecutorService e = new CustomExecutor(2); |
976 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
977 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
977 |
> |
List<Callable<String>> l = new ArrayList<>(); |
978 |
|
l.add(new StringTask()); |
979 |
|
l.add(new StringTask()); |
980 |
|
String result = e.invokeAny(l); |
1012 |
|
public void testInvokeAll3() throws Exception { |
1013 |
|
final ExecutorService e = new CustomExecutor(2); |
1014 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1015 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1015 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1016 |
|
l.add(new StringTask()); |
1017 |
|
l.add(null); |
1018 |
|
try { |
1028 |
|
public void testInvokeAll4() throws Exception { |
1029 |
|
final ExecutorService e = new CustomExecutor(2); |
1030 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1031 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1031 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1032 |
|
l.add(new NPETask()); |
1033 |
|
List<Future<String>> futures = e.invokeAll(l); |
1034 |
|
assertEquals(1, futures.size()); |
1047 |
|
public void testInvokeAll5() throws Exception { |
1048 |
|
final ExecutorService e = new CustomExecutor(2); |
1049 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1050 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1050 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1051 |
|
l.add(new StringTask()); |
1052 |
|
l.add(new StringTask()); |
1053 |
|
List<Future<String>> futures = e.invokeAll(l); |
1076 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1077 |
|
final ExecutorService e = new CustomExecutor(2); |
1078 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1079 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1079 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1080 |
|
l.add(new StringTask()); |
1081 |
|
try { |
1082 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1105 |
|
CountDownLatch latch = new CountDownLatch(1); |
1106 |
|
final ExecutorService e = new CustomExecutor(2); |
1107 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1108 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1108 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1109 |
|
l.add(latchAwaitingStringTask(latch)); |
1110 |
|
l.add(null); |
1111 |
|
try { |
1123 |
|
final ExecutorService e = new CustomExecutor(2); |
1124 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1125 |
|
long startTime = System.nanoTime(); |
1126 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1126 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1127 |
|
l.add(new NPETask()); |
1128 |
|
try { |
1129 |
|
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1142 |
|
final ExecutorService e = new CustomExecutor(2); |
1143 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1144 |
|
long startTime = System.nanoTime(); |
1145 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1145 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1146 |
|
l.add(new StringTask()); |
1147 |
|
l.add(new StringTask()); |
1148 |
|
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1170 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1171 |
|
final ExecutorService e = new CustomExecutor(2); |
1172 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1173 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1173 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1174 |
|
l.add(new StringTask()); |
1175 |
|
try { |
1176 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1196 |
|
public void testTimedInvokeAll3() throws Exception { |
1197 |
|
final ExecutorService e = new CustomExecutor(2); |
1198 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1199 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1199 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1200 |
|
l.add(new StringTask()); |
1201 |
|
l.add(null); |
1202 |
|
try { |
1212 |
|
public void testTimedInvokeAll4() throws Exception { |
1213 |
|
final ExecutorService e = new CustomExecutor(2); |
1214 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1215 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1215 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1216 |
|
l.add(new NPETask()); |
1217 |
|
List<Future<String>> futures = |
1218 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1232 |
|
public void testTimedInvokeAll5() throws Exception { |
1233 |
|
final ExecutorService e = new CustomExecutor(2); |
1234 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1235 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1235 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1236 |
|
l.add(new StringTask()); |
1237 |
|
l.add(new StringTask()); |
1238 |
|
List<Future<String>> futures = |