154 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
155 |
|
Runnable task = new CheckedRunnable() { |
156 |
|
public void realRun() { done.countDown(); }}; |
157 |
< |
ScheduledFuture h = |
157 |
> |
ScheduledFuture periodicTask = |
158 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
159 |
|
await(done); |
160 |
< |
h.cancel(true); |
161 |
< |
double normalizedTime = |
160 |
> |
periodicTask.cancel(true); |
161 |
> |
double elapsedDelays = |
162 |
|
(double) millisElapsedSince(startTime) / delay; |
163 |
< |
if (normalizedTime >= cycles - 1 && |
164 |
< |
normalizedTime <= cycles) |
163 |
> |
if (elapsedDelays >= cycles - 1 && |
164 |
> |
elapsedDelays <= cycles) |
165 |
|
return; |
166 |
|
} |
167 |
< |
throw new AssertionError("unexpected execution rate"); |
167 |
> |
fail("unexpected execution rate"); |
168 |
|
} |
169 |
|
} |
170 |
|
|
180 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
181 |
|
Runnable task = new CheckedRunnable() { |
182 |
|
public void realRun() { done.countDown(); }}; |
183 |
< |
ScheduledFuture h = |
183 |
> |
ScheduledFuture periodicTask = |
184 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
185 |
|
await(done); |
186 |
< |
h.cancel(true); |
187 |
< |
double normalizedTime = |
186 |
> |
periodicTask.cancel(true); |
187 |
> |
double elapsedDelays = |
188 |
|
(double) millisElapsedSince(startTime) / delay; |
189 |
< |
if (normalizedTime >= cycles - 1 && |
190 |
< |
normalizedTime <= cycles) |
189 |
> |
if (elapsedDelays >= cycles - 1 && |
190 |
> |
elapsedDelays <= cycles) |
191 |
|
return; |
192 |
|
} |
193 |
< |
throw new AssertionError("unexpected execution rate"); |
193 |
> |
fail("unexpected execution rate"); |
194 |
|
} |
195 |
|
} |
196 |
|
|
1046 |
|
public void testTimedInvokeAny4() throws Exception { |
1047 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1048 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1049 |
+ |
long startTime = System.nanoTime(); |
1050 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1051 |
|
l.add(new NPETask()); |
1052 |
|
try { |
1053 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1053 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1054 |
|
shouldThrow(); |
1055 |
|
} catch (ExecutionException success) { |
1056 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1057 |
|
} |
1058 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1059 |
|
} |
1060 |
|
} |
1061 |
|
|
1065 |
|
public void testTimedInvokeAny5() throws Exception { |
1066 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1067 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1068 |
+ |
long startTime = System.nanoTime(); |
1069 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1070 |
|
l.add(new StringTask()); |
1071 |
|
l.add(new StringTask()); |
1072 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1072 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1073 |
|
assertSame(TEST_STRING, result); |
1074 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1075 |
|
} |
1076 |
|
} |
1077 |
|
|
1172 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1173 |
|
*/ |
1174 |
|
public void testTimedInvokeAll6() throws Exception { |
1175 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1176 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1177 |
< |
for (long timeout = timeoutMillis();;) { |
1175 |
> |
for (long timeout = timeoutMillis();;) { |
1176 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1177 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1178 |
> |
public String realCall() { |
1179 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1180 |
> |
catch (InterruptedException ok) {} |
1181 |
> |
return "1"; }}; |
1182 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1183 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1184 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1185 |
|
tasks.add(new StringTask("0")); |
1186 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1186 |
> |
tasks.add(waiter); |
1187 |
|
tasks.add(new StringTask("2")); |
1188 |
|
long startTime = System.nanoTime(); |
1189 |
|
List<Future<String>> futures = |
1190 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1190 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1191 |
|
assertEquals(tasks.size(), futures.size()); |
1192 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1193 |
|
for (Future future : futures) |
1206 |
|
} |
1207 |
|
} |
1208 |
|
|
1209 |
+ |
/** |
1210 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1211 |
+ |
* one-shot task from executing. |
1212 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1213 |
+ |
*/ |
1214 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1215 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1216 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1217 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1218 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1219 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1220 |
+ |
immediateDone.countDown(); |
1221 |
+ |
}}; |
1222 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1223 |
+ |
delayedDone.countDown(); |
1224 |
+ |
p.submit(immediate); |
1225 |
+ |
}}; |
1226 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1227 |
+ |
await(delayedDone); |
1228 |
+ |
await(immediateDone); |
1229 |
+ |
} |
1230 |
+ |
} |
1231 |
+ |
|
1232 |
|
} |