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 |
< |
throw new AssertionError("unexpected execution rate"); |
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 |
< |
throw new AssertionError("unexpected execution rate"); |
218 |
> |
fail("unexpected execution rate"); |
219 |
|
} |
220 |
|
} |
221 |
|
|
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 |
|
/** |
1070 |
|
public void testTimedInvokeAny4() throws Exception { |
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>>(); |
1075 |
|
l.add(new NPETask()); |
1076 |
|
try { |
1077 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1077 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1078 |
|
shouldThrow(); |
1079 |
|
} catch (ExecutionException success) { |
1080 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1081 |
|
} |
1082 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1083 |
|
} |
1084 |
|
} |
1085 |
|
|
1089 |
|
public void testTimedInvokeAny5() throws Exception { |
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>>(); |
1094 |
|
l.add(new StringTask()); |
1095 |
|
l.add(new StringTask()); |
1096 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1096 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1097 |
|
assertSame(TEST_STRING, result); |
1098 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1099 |
|
} |
1100 |
|
} |
1101 |
|
|
1196 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1197 |
|
*/ |
1198 |
|
public void testTimedInvokeAll6() throws Exception { |
1199 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1200 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1201 |
< |
for (long timeout = timeoutMillis();;) { |
1199 |
> |
for (long timeout = timeoutMillis();;) { |
1200 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1201 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1202 |
> |
public String realCall() { |
1203 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1204 |
> |
catch (InterruptedException ok) {} |
1205 |
> |
return "1"; }}; |
1206 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1207 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1208 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1209 |
|
tasks.add(new StringTask("0")); |
1210 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1210 |
> |
tasks.add(waiter); |
1211 |
|
tasks.add(new StringTask("2")); |
1212 |
|
long startTime = System.nanoTime(); |
1213 |
|
List<Future<String>> futures = |
1214 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1214 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1215 |
|
assertEquals(tasks.size(), futures.size()); |
1216 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1217 |
|
for (Future future : futures) |
1230 |
|
} |
1231 |
|
} |
1232 |
|
|
1233 |
+ |
/** |
1234 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1235 |
+ |
* one-shot task from executing. |
1236 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1237 |
+ |
*/ |
1238 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1239 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1240 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1241 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1242 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1243 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1244 |
+ |
immediateDone.countDown(); |
1245 |
+ |
}}; |
1246 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1247 |
+ |
delayedDone.countDown(); |
1248 |
+ |
p.submit(immediate); |
1249 |
+ |
}}; |
1250 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1251 |
+ |
await(delayedDone); |
1252 |
+ |
await(immediateDone); |
1253 |
+ |
} |
1254 |
+ |
} |
1255 |
+ |
|
1256 |
|
} |