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; |
24 |
|
import java.util.concurrent.ScheduledFuture; |
25 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
|
import java.util.concurrent.ThreadFactory; |
27 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
28 |
|
import java.util.concurrent.ThreadPoolExecutor; |
29 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
31 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
32 |
+ |
import java.util.stream.Stream; |
33 |
|
|
34 |
|
import junit.framework.Test; |
35 |
|
import junit.framework.TestSuite; |
46 |
|
* execute successfully executes a runnable |
47 |
|
*/ |
48 |
|
public void testExecute() throws InterruptedException { |
49 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
50 |
< |
final CountDownLatch done = new CountDownLatch(1); |
51 |
< |
final Runnable task = new CheckedRunnable() { |
52 |
< |
public void realRun() { |
53 |
< |
done.countDown(); |
50 |
< |
}}; |
51 |
< |
try { |
49 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
50 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
51 |
> |
final CountDownLatch done = new CountDownLatch(1); |
52 |
> |
final Runnable task = new CheckedRunnable() { |
53 |
> |
public void realRun() { done.countDown(); }}; |
54 |
|
p.execute(task); |
55 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
54 |
< |
} finally { |
55 |
< |
joinPool(p); |
55 |
> |
await(done); |
56 |
|
} |
57 |
|
} |
58 |
|
|
60 |
|
* delayed schedule of callable successfully executes after delay |
61 |
|
*/ |
62 |
|
public void testSchedule1() throws Exception { |
63 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
64 |
< |
final long startTime = System.nanoTime(); |
65 |
< |
final CountDownLatch done = new CountDownLatch(1); |
66 |
< |
try { |
63 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
64 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
65 |
> |
final long startTime = System.nanoTime(); |
66 |
> |
final CountDownLatch done = new CountDownLatch(1); |
67 |
|
Callable task = new CheckedCallable<Boolean>() { |
68 |
|
public Boolean realCall() { |
69 |
|
done.countDown(); |
74 |
|
assertSame(Boolean.TRUE, f.get()); |
75 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
77 |
– |
} finally { |
78 |
– |
joinPool(p); |
77 |
|
} |
78 |
|
} |
79 |
|
|
81 |
|
* delayed schedule of runnable successfully executes after delay |
82 |
|
*/ |
83 |
|
public void testSchedule3() throws Exception { |
84 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
85 |
< |
final long startTime = System.nanoTime(); |
86 |
< |
final CountDownLatch done = new CountDownLatch(1); |
87 |
< |
try { |
84 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
85 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
86 |
> |
final long startTime = System.nanoTime(); |
87 |
> |
final CountDownLatch done = new CountDownLatch(1); |
88 |
|
Runnable task = new CheckedRunnable() { |
89 |
|
public void realRun() { |
90 |
|
done.countDown(); |
94 |
|
await(done); |
95 |
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
96 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
99 |
– |
} finally { |
100 |
– |
joinPool(p); |
97 |
|
} |
98 |
|
} |
99 |
|
|
101 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
102 |
|
*/ |
103 |
|
public void testSchedule4() throws Exception { |
104 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
105 |
< |
final long startTime = System.nanoTime(); |
106 |
< |
final CountDownLatch done = new CountDownLatch(1); |
107 |
< |
try { |
104 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
105 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
106 |
> |
final long startTime = System.nanoTime(); |
107 |
> |
final CountDownLatch done = new CountDownLatch(1); |
108 |
|
Runnable task = new CheckedRunnable() { |
109 |
|
public void realRun() { |
110 |
|
done.countDown(); |
116 |
|
await(done); |
117 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
118 |
|
f.cancel(true); |
123 |
– |
} finally { |
124 |
– |
joinPool(p); |
119 |
|
} |
120 |
|
} |
121 |
|
|
123 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
124 |
|
*/ |
125 |
|
public void testSchedule5() throws Exception { |
126 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
127 |
< |
final long startTime = System.nanoTime(); |
128 |
< |
final CountDownLatch done = new CountDownLatch(1); |
129 |
< |
try { |
126 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
127 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
128 |
> |
final long startTime = System.nanoTime(); |
129 |
> |
final CountDownLatch done = new CountDownLatch(1); |
130 |
|
Runnable task = new CheckedRunnable() { |
131 |
|
public void realRun() { |
132 |
|
done.countDown(); |
138 |
|
await(done); |
139 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
140 |
|
f.cancel(true); |
147 |
– |
} finally { |
148 |
– |
joinPool(p); |
141 |
|
} |
142 |
|
} |
143 |
|
|
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
150 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
151 |
> |
* Eventually, it must hold that: |
152 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
153 |
|
*/ |
154 |
|
public void testFixedRateSequence() throws InterruptedException { |
155 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
156 |
< |
try { |
155 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
156 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
157 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
158 |
< |
long startTime = System.nanoTime(); |
159 |
< |
int cycles = 10; |
158 |
> |
final long startTime = System.nanoTime(); |
159 |
> |
final int cycles = 8; |
160 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
161 |
< |
Runnable task = new CheckedRunnable() { |
161 |
> |
final Runnable task = new CheckedRunnable() { |
162 |
|
public void realRun() { done.countDown(); }}; |
163 |
< |
ScheduledFuture h = |
163 |
> |
final ScheduledFuture periodicTask = |
164 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
165 |
< |
done.await(); |
166 |
< |
h.cancel(true); |
167 |
< |
double normalizedTime = |
168 |
< |
(double) millisElapsedSince(startTime) / delay; |
169 |
< |
if (normalizedTime >= cycles - 1 && |
170 |
< |
normalizedTime <= cycles) |
165 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
166 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
167 |
> |
periodicTask.cancel(true); |
168 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
169 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
170 |
> |
if (elapsedMillis <= cycles * delay) |
171 |
|
return; |
172 |
+ |
// else retry with longer delay |
173 |
|
} |
174 |
< |
throw new AssertionError("unexpected execution rate"); |
180 |
< |
} finally { |
181 |
< |
joinPool(p); |
174 |
> |
fail("unexpected execution rate"); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
179 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
180 |
> |
* Eventually, it must hold that each task starts at least delay and at |
181 |
> |
* most 2 * delay after the termination of the previous task. |
182 |
|
*/ |
183 |
|
public void testFixedDelaySequence() throws InterruptedException { |
184 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
185 |
< |
try { |
184 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
185 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
186 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
187 |
< |
long startTime = System.nanoTime(); |
188 |
< |
int cycles = 10; |
187 |
> |
final long startTime = System.nanoTime(); |
188 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
189 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
190 |
> |
final int cycles = 8; |
191 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
192 |
< |
Runnable task = new CheckedRunnable() { |
193 |
< |
public void realRun() { done.countDown(); }}; |
194 |
< |
ScheduledFuture h = |
192 |
> |
final int d = delay; |
193 |
> |
final Runnable task = new CheckedRunnable() { |
194 |
> |
public void realRun() { |
195 |
> |
long now = System.nanoTime(); |
196 |
> |
long elapsedMillis |
197 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
198 |
> |
if (done.getCount() == cycles) { // first execution |
199 |
> |
if (elapsedMillis >= d) |
200 |
> |
tryLongerDelay.set(true); |
201 |
> |
} else { |
202 |
> |
assertTrue(elapsedMillis >= d); |
203 |
> |
if (elapsedMillis >= 2 * d) |
204 |
> |
tryLongerDelay.set(true); |
205 |
> |
} |
206 |
> |
previous.set(now); |
207 |
> |
done.countDown(); |
208 |
> |
}}; |
209 |
> |
final ScheduledFuture periodicTask = |
210 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
211 |
< |
done.await(); |
212 |
< |
h.cancel(true); |
213 |
< |
double normalizedTime = |
214 |
< |
(double) millisElapsedSince(startTime) / delay; |
215 |
< |
if (normalizedTime >= cycles - 1 && |
216 |
< |
normalizedTime <= cycles) |
211 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
212 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
213 |
> |
periodicTask.cancel(true); |
214 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
215 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
216 |
> |
if (!tryLongerDelay.get()) |
217 |
|
return; |
218 |
+ |
// else retry with longer delay |
219 |
|
} |
220 |
< |
throw new AssertionError("unexpected execution rate"); |
208 |
< |
} finally { |
209 |
< |
joinPool(p); |
220 |
> |
fail("unexpected execution rate"); |
221 |
|
} |
222 |
|
} |
223 |
|
|
225 |
|
* execute(null) throws NPE |
226 |
|
*/ |
227 |
|
public void testExecuteNull() throws InterruptedException { |
228 |
< |
ScheduledThreadPoolExecutor se = null; |
229 |
< |
try { |
230 |
< |
se = new ScheduledThreadPoolExecutor(1); |
231 |
< |
se.execute(null); |
232 |
< |
shouldThrow(); |
233 |
< |
} catch (NullPointerException success) {} |
234 |
< |
|
224 |
< |
joinPool(se); |
228 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
229 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
230 |
> |
try { |
231 |
> |
p.execute(null); |
232 |
> |
shouldThrow(); |
233 |
> |
} catch (NullPointerException success) {} |
234 |
> |
} |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
|
* schedule(null) throws NPE |
239 |
|
*/ |
240 |
|
public void testScheduleNull() throws InterruptedException { |
241 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
242 |
< |
try { |
243 |
< |
TrackedCallable callable = null; |
244 |
< |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
245 |
< |
shouldThrow(); |
246 |
< |
} catch (NullPointerException success) {} |
247 |
< |
joinPool(se); |
241 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
242 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
243 |
> |
try { |
244 |
> |
TrackedCallable callable = null; |
245 |
> |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
246 |
> |
shouldThrow(); |
247 |
> |
} catch (NullPointerException success) {} |
248 |
> |
} |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
|
* execute throws RejectedExecutionException if shutdown |
253 |
|
*/ |
254 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
255 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
256 |
< |
try { |
257 |
< |
se.shutdown(); |
258 |
< |
se.schedule(new NoOpRunnable(), |
259 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
260 |
< |
shouldThrow(); |
261 |
< |
} catch (RejectedExecutionException success) { |
262 |
< |
} catch (SecurityException ok) { |
255 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
256 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
257 |
> |
try { |
258 |
> |
p.shutdown(); |
259 |
> |
p.schedule(new NoOpRunnable(), |
260 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
261 |
> |
shouldThrow(); |
262 |
> |
} catch (RejectedExecutionException success) { |
263 |
> |
} catch (SecurityException ok) {} |
264 |
|
} |
253 |
– |
|
254 |
– |
joinPool(se); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
|
* schedule throws RejectedExecutionException if shutdown |
269 |
|
*/ |
270 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
271 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
272 |
< |
try { |
273 |
< |
se.shutdown(); |
274 |
< |
se.schedule(new NoOpCallable(), |
275 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
276 |
< |
shouldThrow(); |
277 |
< |
} catch (RejectedExecutionException success) { |
278 |
< |
} catch (SecurityException ok) { |
271 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
272 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
273 |
> |
try { |
274 |
> |
p.shutdown(); |
275 |
> |
p.schedule(new NoOpCallable(), |
276 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
277 |
> |
shouldThrow(); |
278 |
> |
} catch (RejectedExecutionException success) { |
279 |
> |
} catch (SecurityException ok) {} |
280 |
|
} |
270 |
– |
joinPool(se); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
|
* schedule callable throws RejectedExecutionException if shutdown |
285 |
|
*/ |
286 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
287 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
288 |
< |
try { |
289 |
< |
se.shutdown(); |
290 |
< |
se.schedule(new NoOpCallable(), |
291 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
292 |
< |
shouldThrow(); |
293 |
< |
} catch (RejectedExecutionException success) { |
294 |
< |
} catch (SecurityException ok) { |
287 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
288 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
289 |
> |
try { |
290 |
> |
p.shutdown(); |
291 |
> |
p.schedule(new NoOpCallable(), |
292 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
293 |
> |
shouldThrow(); |
294 |
> |
} catch (RejectedExecutionException success) { |
295 |
> |
} catch (SecurityException ok) {} |
296 |
|
} |
286 |
– |
joinPool(se); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
301 |
|
*/ |
302 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
303 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
304 |
< |
try { |
305 |
< |
se.shutdown(); |
306 |
< |
se.scheduleAtFixedRate(new NoOpRunnable(), |
307 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
308 |
< |
shouldThrow(); |
309 |
< |
} catch (RejectedExecutionException success) { |
310 |
< |
} catch (SecurityException ok) { |
303 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
304 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
305 |
> |
try { |
306 |
> |
p.shutdown(); |
307 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), |
308 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
309 |
> |
shouldThrow(); |
310 |
> |
} catch (RejectedExecutionException success) { |
311 |
> |
} catch (SecurityException ok) {} |
312 |
|
} |
302 |
– |
joinPool(se); |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
317 |
|
*/ |
318 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
319 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
320 |
< |
try { |
321 |
< |
se.shutdown(); |
322 |
< |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
323 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
324 |
< |
shouldThrow(); |
325 |
< |
} catch (RejectedExecutionException success) { |
326 |
< |
} catch (SecurityException ok) { |
319 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
320 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
321 |
> |
try { |
322 |
> |
p.shutdown(); |
323 |
> |
p.scheduleWithFixedDelay(new NoOpRunnable(), |
324 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
325 |
> |
shouldThrow(); |
326 |
> |
} catch (RejectedExecutionException success) { |
327 |
> |
} catch (SecurityException ok) {} |
328 |
|
} |
318 |
– |
joinPool(se); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
333 |
|
* thread becomes active |
334 |
|
*/ |
335 |
|
public void testGetActiveCount() throws InterruptedException { |
326 |
– |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
327 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
336 |
|
final CountDownLatch done = new CountDownLatch(1); |
337 |
< |
try { |
337 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
338 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
339 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
340 |
|
assertEquals(0, p.getActiveCount()); |
341 |
|
p.execute(new CheckedRunnable() { |
342 |
|
public void realRun() throws InterruptedException { |
343 |
|
threadStarted.countDown(); |
344 |
|
assertEquals(1, p.getActiveCount()); |
345 |
< |
done.await(); |
345 |
> |
await(done); |
346 |
|
}}); |
347 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
347 |
> |
await(threadStarted); |
348 |
|
assertEquals(1, p.getActiveCount()); |
339 |
– |
} finally { |
340 |
– |
done.countDown(); |
341 |
– |
joinPool(p); |
349 |
|
} |
350 |
|
} |
351 |
|
|
355 |
|
*/ |
356 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
357 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
358 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
359 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
360 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
361 |
< |
try { |
358 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
359 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
360 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
361 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
362 |
|
assertEquals(0, p.getCompletedTaskCount()); |
363 |
|
p.execute(new CheckedRunnable() { |
364 |
|
public void realRun() throws InterruptedException { |
365 |
|
threadStarted.countDown(); |
366 |
|
assertEquals(0, p.getCompletedTaskCount()); |
367 |
< |
threadProceed.await(); |
367 |
> |
await(threadProceed); |
368 |
|
threadDone.countDown(); |
369 |
|
}}); |
370 |
|
await(threadStarted); |
371 |
|
assertEquals(0, p.getCompletedTaskCount()); |
372 |
|
threadProceed.countDown(); |
373 |
< |
threadDone.await(); |
373 |
> |
await(threadDone); |
374 |
|
long startTime = System.nanoTime(); |
375 |
|
while (p.getCompletedTaskCount() != 1) { |
376 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
377 |
|
fail("timed out"); |
378 |
|
Thread.yield(); |
379 |
|
} |
373 |
– |
} finally { |
374 |
– |
joinPool(p); |
380 |
|
} |
381 |
|
} |
382 |
|
|
385 |
|
*/ |
386 |
|
public void testGetCorePoolSize() throws InterruptedException { |
387 |
|
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
388 |
< |
assertEquals(1, p.getCorePoolSize()); |
389 |
< |
joinPool(p); |
388 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
389 |
> |
assertEquals(1, p.getCorePoolSize()); |
390 |
> |
} |
391 |
|
} |
392 |
|
|
393 |
|
/** |
399 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
400 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
401 |
|
final CountDownLatch done = new CountDownLatch(1); |
402 |
< |
try { |
402 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
403 |
|
assertEquals(0, p.getLargestPoolSize()); |
404 |
|
for (int i = 0; i < THREADS; i++) |
405 |
|
p.execute(new CheckedRunnable() { |
406 |
|
public void realRun() throws InterruptedException { |
407 |
|
threadsStarted.countDown(); |
408 |
< |
done.await(); |
408 |
> |
await(done); |
409 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
410 |
|
}}); |
411 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
406 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
407 |
< |
} finally { |
408 |
< |
done.countDown(); |
409 |
< |
joinPool(p); |
411 |
> |
await(threadsStarted); |
412 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
413 |
|
} |
414 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
422 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
423 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
424 |
|
final CountDownLatch done = new CountDownLatch(1); |
425 |
< |
try { |
425 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
426 |
|
assertEquals(0, p.getPoolSize()); |
427 |
|
p.execute(new CheckedRunnable() { |
428 |
|
public void realRun() throws InterruptedException { |
429 |
|
threadStarted.countDown(); |
430 |
|
assertEquals(1, p.getPoolSize()); |
431 |
< |
done.await(); |
431 |
> |
await(done); |
432 |
|
}}); |
433 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
433 |
> |
await(threadStarted); |
434 |
|
assertEquals(1, p.getPoolSize()); |
432 |
– |
} finally { |
433 |
– |
done.countDown(); |
434 |
– |
joinPool(p); |
435 |
|
} |
436 |
|
} |
437 |
|
|
440 |
|
* submitted |
441 |
|
*/ |
442 |
|
public void testGetTaskCount() throws InterruptedException { |
443 |
< |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
444 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
443 |
> |
final int TASKS = 3; |
444 |
|
final CountDownLatch done = new CountDownLatch(1); |
445 |
< |
final int TASKS = 5; |
446 |
< |
try { |
445 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
446 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
447 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
448 |
|
assertEquals(0, p.getTaskCount()); |
449 |
< |
for (int i = 0; i < TASKS; i++) |
449 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
450 |
> |
p.execute(new CheckedRunnable() { |
451 |
> |
public void realRun() throws InterruptedException { |
452 |
> |
threadStarted.countDown(); |
453 |
> |
await(done); |
454 |
> |
}}); |
455 |
> |
await(threadStarted); |
456 |
> |
assertEquals(1, p.getTaskCount()); |
457 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
458 |
> |
for (int i = 0; i < TASKS; i++) { |
459 |
> |
assertEquals(1 + i, p.getTaskCount()); |
460 |
|
p.execute(new CheckedRunnable() { |
461 |
|
public void realRun() throws InterruptedException { |
462 |
|
threadStarted.countDown(); |
463 |
< |
done.await(); |
463 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
464 |
> |
await(done); |
465 |
|
}}); |
466 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
467 |
< |
assertEquals(TASKS, p.getTaskCount()); |
468 |
< |
} finally { |
458 |
< |
done.countDown(); |
459 |
< |
joinPool(p); |
466 |
> |
} |
467 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
468 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
469 |
|
} |
470 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
471 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
|
* getThreadFactory returns factory in constructor if not set |
476 |
|
*/ |
477 |
|
public void testGetThreadFactory() throws InterruptedException { |
478 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
479 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
480 |
< |
assertSame(tf, p.getThreadFactory()); |
481 |
< |
joinPool(p); |
478 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
479 |
> |
final ScheduledThreadPoolExecutor p = |
480 |
> |
new ScheduledThreadPoolExecutor(1, threadFactory); |
481 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
482 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
483 |
> |
} |
484 |
|
} |
485 |
|
|
486 |
|
/** |
487 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
488 |
|
*/ |
489 |
|
public void testSetThreadFactory() throws InterruptedException { |
490 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
491 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
492 |
< |
p.setThreadFactory(tf); |
493 |
< |
assertSame(tf, p.getThreadFactory()); |
494 |
< |
joinPool(p); |
490 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
491 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
492 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
493 |
> |
p.setThreadFactory(threadFactory); |
494 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
495 |
> |
} |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
|
* setThreadFactory(null) throws NPE |
500 |
|
*/ |
501 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
502 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
503 |
< |
try { |
504 |
< |
p.setThreadFactory(null); |
505 |
< |
shouldThrow(); |
506 |
< |
} catch (NullPointerException success) { |
507 |
< |
} finally { |
508 |
< |
joinPool(p); |
502 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
503 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
504 |
> |
try { |
505 |
> |
p.setThreadFactory(null); |
506 |
> |
shouldThrow(); |
507 |
> |
} catch (NullPointerException success) {} |
508 |
> |
} |
509 |
> |
} |
510 |
> |
|
511 |
> |
/** |
512 |
> |
* The default rejected execution handler is AbortPolicy. |
513 |
> |
*/ |
514 |
> |
public void testDefaultRejectedExecutionHandler() { |
515 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
516 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
517 |
> |
assertTrue(p.getRejectedExecutionHandler() |
518 |
> |
instanceof ThreadPoolExecutor.AbortPolicy); |
519 |
|
} |
520 |
|
} |
521 |
|
|
523 |
|
* isShutdown is false before shutdown, true after |
524 |
|
*/ |
525 |
|
public void testIsShutdown() { |
526 |
< |
|
527 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
528 |
< |
try { |
529 |
< |
assertFalse(p.isShutdown()); |
530 |
< |
} |
531 |
< |
finally { |
532 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
526 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
527 |
> |
assertFalse(p.isShutdown()); |
528 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
> |
try { |
530 |
> |
p.shutdown(); |
531 |
> |
assertTrue(p.isShutdown()); |
532 |
> |
} catch (SecurityException ok) {} |
533 |
|
} |
510 |
– |
assertTrue(p.isShutdown()); |
534 |
|
} |
535 |
|
|
536 |
|
/** |
538 |
|
*/ |
539 |
|
public void testIsTerminated() throws InterruptedException { |
540 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
541 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
542 |
< |
final CountDownLatch done = new CountDownLatch(1); |
543 |
< |
assertFalse(p.isTerminated()); |
544 |
< |
try { |
541 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
542 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
543 |
> |
final CountDownLatch done = new CountDownLatch(1); |
544 |
> |
assertFalse(p.isTerminated()); |
545 |
|
p.execute(new CheckedRunnable() { |
546 |
|
public void realRun() throws InterruptedException { |
547 |
|
assertFalse(p.isTerminated()); |
548 |
|
threadStarted.countDown(); |
549 |
< |
done.await(); |
549 |
> |
await(done); |
550 |
|
}}); |
551 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
551 |
> |
await(threadStarted); |
552 |
|
assertFalse(p.isTerminating()); |
553 |
|
done.countDown(); |
531 |
– |
} finally { |
554 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
555 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
556 |
+ |
assertTrue(p.isTerminated()); |
557 |
|
} |
534 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
535 |
– |
assertTrue(p.isTerminated()); |
558 |
|
} |
559 |
|
|
560 |
|
/** |
564 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
565 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
566 |
|
final CountDownLatch done = new CountDownLatch(1); |
567 |
< |
try { |
567 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
568 |
|
assertFalse(p.isTerminating()); |
569 |
|
p.execute(new CheckedRunnable() { |
570 |
|
public void realRun() throws InterruptedException { |
571 |
|
assertFalse(p.isTerminating()); |
572 |
|
threadStarted.countDown(); |
573 |
< |
done.await(); |
573 |
> |
await(done); |
574 |
|
}}); |
575 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
575 |
> |
await(threadStarted); |
576 |
|
assertFalse(p.isTerminating()); |
577 |
|
done.countDown(); |
556 |
– |
} finally { |
578 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
579 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
580 |
+ |
assertTrue(p.isTerminated()); |
581 |
+ |
assertFalse(p.isTerminating()); |
582 |
|
} |
559 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
560 |
– |
assertTrue(p.isTerminated()); |
561 |
– |
assertFalse(p.isTerminating()); |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
|
* getQueue returns the work queue, which contains queued tasks |
587 |
|
*/ |
588 |
|
public void testGetQueue() throws InterruptedException { |
568 |
– |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
569 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
589 |
|
final CountDownLatch done = new CountDownLatch(1); |
590 |
< |
try { |
590 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
591 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
592 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
593 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
594 |
|
for (int i = 0; i < tasks.length; i++) { |
595 |
|
Runnable r = new CheckedRunnable() { |
596 |
|
public void realRun() throws InterruptedException { |
597 |
|
threadStarted.countDown(); |
598 |
< |
done.await(); |
598 |
> |
await(done); |
599 |
|
}}; |
600 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
601 |
|
} |
602 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
602 |
> |
await(threadStarted); |
603 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
604 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
605 |
|
assertFalse(q.contains(tasks[0])); |
585 |
– |
} finally { |
586 |
– |
done.countDown(); |
587 |
– |
joinPool(p); |
606 |
|
} |
607 |
|
} |
608 |
|
|
610 |
|
* remove(task) removes queued task, and fails to remove active task |
611 |
|
*/ |
612 |
|
public void testRemove() throws InterruptedException { |
595 |
– |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
596 |
– |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
597 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
613 |
|
final CountDownLatch done = new CountDownLatch(1); |
614 |
< |
try { |
614 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
615 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
616 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
617 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
618 |
|
for (int i = 0; i < tasks.length; i++) { |
619 |
|
Runnable r = new CheckedRunnable() { |
620 |
|
public void realRun() throws InterruptedException { |
621 |
|
threadStarted.countDown(); |
622 |
< |
done.await(); |
622 |
> |
await(done); |
623 |
|
}}; |
624 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
625 |
|
} |
626 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
626 |
> |
await(threadStarted); |
627 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
628 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
629 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
634 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
635 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
636 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
619 |
– |
} finally { |
620 |
– |
done.countDown(); |
621 |
– |
joinPool(p); |
637 |
|
} |
638 |
|
} |
639 |
|
|
641 |
|
* purge eventually removes cancelled tasks from the queue |
642 |
|
*/ |
643 |
|
public void testPurge() throws InterruptedException { |
644 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
645 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
646 |
< |
for (int i = 0; i < tasks.length; i++) |
647 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
648 |
< |
LONG_DELAY_MS, MILLISECONDS); |
649 |
< |
try { |
644 |
> |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
645 |
> |
final Runnable releaser = new Runnable() { public void run() { |
646 |
> |
for (ScheduledFuture task : tasks) |
647 |
> |
if (task != null) task.cancel(true); }}; |
648 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
649 |
> |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
650 |
> |
for (int i = 0; i < tasks.length; i++) |
651 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
652 |
> |
LONG_DELAY_MS, MILLISECONDS); |
653 |
|
int max = tasks.length; |
654 |
|
if (tasks[4].cancel(true)) --max; |
655 |
|
if (tasks[3].cancel(true)) --max; |
661 |
|
long count = p.getTaskCount(); |
662 |
|
if (count == max) |
663 |
|
return; |
664 |
< |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
664 |
> |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
665 |
|
fail("Purge failed to remove cancelled tasks"); |
648 |
– |
} finally { |
649 |
– |
for (ScheduledFuture task : tasks) |
650 |
– |
task.cancel(true); |
651 |
– |
joinPool(p); |
666 |
|
} |
667 |
|
} |
668 |
|
|
676 |
|
final AtomicInteger ran = new AtomicInteger(0); |
677 |
|
final ScheduledThreadPoolExecutor p = |
678 |
|
new ScheduledThreadPoolExecutor(poolSize); |
679 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
680 |
< |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
679 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
680 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
681 |
|
threadsStarted.countDown(); |
682 |
|
try { |
683 |
|
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
686 |
|
}}; |
687 |
|
for (int i = 0; i < count; i++) |
688 |
|
p.execute(waiter); |
689 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
689 |
> |
await(threadsStarted); |
690 |
> |
assertEquals(poolSize, p.getActiveCount()); |
691 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
692 |
|
final List<Runnable> queuedTasks; |
693 |
|
try { |
694 |
|
queuedTasks = p.shutdownNow(); |
701 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
702 |
|
assertTrue(p.isTerminated()); |
703 |
|
assertEquals(poolSize, ran.get()); |
704 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
709 |
|
* and those tasks are drained from the queue |
710 |
|
*/ |
711 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
712 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
712 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
713 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
714 |
|
for (int i = 0; i < 3; i++) { |
715 |
|
Runnable r = new NoOpRunnable(); |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
< |
* In default setting, shutdown cancels periodic but not delayed |
743 |
< |
* tasks at shutdown |
744 |
< |
*/ |
745 |
< |
public void testShutdown1() throws InterruptedException { |
746 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
747 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
748 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
749 |
< |
|
750 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
751 |
< |
for (int i = 0; i < tasks.length; i++) |
752 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
753 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
754 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
755 |
< |
BlockingQueue<Runnable> q = p.getQueue(); |
756 |
< |
for (ScheduledFuture task : tasks) { |
757 |
< |
assertFalse(task.isDone()); |
758 |
< |
assertFalse(task.isCancelled()); |
759 |
< |
assertTrue(q.contains(task)); |
742 |
> |
* By default, periodic tasks are cancelled at shutdown. |
743 |
> |
* By default, delayed tasks keep running after shutdown. |
744 |
> |
* Check that changing the default values work: |
745 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
746 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
747 |
> |
*/ |
748 |
> |
public void testShutdown_cancellation() throws Exception { |
749 |
> |
final int poolSize = 6; |
750 |
> |
final ScheduledThreadPoolExecutor p |
751 |
> |
= new ScheduledThreadPoolExecutor(poolSize); |
752 |
> |
final BlockingQueue<Runnable> q = p.getQueue(); |
753 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
754 |
> |
final boolean effectiveDelayedPolicy; |
755 |
> |
final boolean effectivePeriodicPolicy; |
756 |
> |
final boolean effectiveRemovePolicy; |
757 |
> |
|
758 |
> |
if (rnd.nextBoolean()) |
759 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
760 |
> |
effectiveDelayedPolicy = rnd.nextBoolean()); |
761 |
> |
else |
762 |
> |
effectiveDelayedPolicy = true; |
763 |
> |
assertEquals(effectiveDelayedPolicy, |
764 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
765 |
> |
|
766 |
> |
if (rnd.nextBoolean()) |
767 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
768 |
> |
effectivePeriodicPolicy = rnd.nextBoolean()); |
769 |
> |
else |
770 |
> |
effectivePeriodicPolicy = false; |
771 |
> |
assertEquals(effectivePeriodicPolicy, |
772 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
773 |
> |
|
774 |
> |
if (rnd.nextBoolean()) |
775 |
> |
p.setRemoveOnCancelPolicy( |
776 |
> |
effectiveRemovePolicy = rnd.nextBoolean()); |
777 |
> |
else |
778 |
> |
effectiveRemovePolicy = false; |
779 |
> |
assertEquals(effectiveRemovePolicy, |
780 |
> |
p.getRemoveOnCancelPolicy()); |
781 |
> |
|
782 |
> |
// System.err.println("effectiveDelayedPolicy="+effectiveDelayedPolicy); |
783 |
> |
// System.err.println("effectivePeriodicPolicy="+effectivePeriodicPolicy); |
784 |
> |
// System.err.println("effectiveRemovePolicy="+effectiveRemovePolicy); |
785 |
> |
|
786 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
787 |
> |
// then add a second wave that waits in the queue. |
788 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
789 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
790 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
791 |
> |
|
792 |
> |
class Task extends CheckedRunnable { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
ran.getAndIncrement(); |
795 |
> |
poolBlocked.countDown(); |
796 |
> |
await(unblock); |
797 |
> |
} |
798 |
|
} |
799 |
< |
assertTrue(p.isShutdown()); |
800 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
801 |
< |
assertTrue(p.isTerminated()); |
802 |
< |
for (ScheduledFuture task : tasks) { |
803 |
< |
assertTrue(task.isDone()); |
804 |
< |
assertFalse(task.isCancelled()); |
799 |
> |
|
800 |
> |
class PeriodicTask extends Task { |
801 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
802 |
> |
int rounds; |
803 |
> |
public void realRun() throws InterruptedException { |
804 |
> |
if (--rounds == 0) super.realRun(); |
805 |
> |
} |
806 |
|
} |
751 |
– |
} |
807 |
|
|
808 |
< |
/** |
809 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
810 |
< |
* delayed tasks are cancelled at shutdown |
811 |
< |
*/ |
812 |
< |
public void testShutdown2() throws InterruptedException { |
813 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
814 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
815 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
816 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
817 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
818 |
< |
for (int i = 0; i < tasks.length; i++) |
819 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
820 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
821 |
< |
BlockingQueue q = p.getQueue(); |
822 |
< |
assertEquals(tasks.length, q.size()); |
823 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
824 |
< |
assertTrue(p.isShutdown()); |
808 |
> |
Runnable task = new Task(); |
809 |
> |
|
810 |
> |
List<Future<?>> immediates = new ArrayList<>(); |
811 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
812 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
813 |
> |
|
814 |
> |
immediates.add(p.submit(task)); |
815 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
816 |
> |
for (int rounds : new int[] { 1, 2 }) { |
817 |
> |
periodics.add(p.scheduleAtFixedRate( |
818 |
> |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
819 |
> |
periodics.add(p.scheduleWithFixedDelay( |
820 |
> |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
821 |
> |
} |
822 |
> |
|
823 |
> |
await(poolBlocked); |
824 |
> |
|
825 |
> |
assertEquals(poolSize, ran.get()); |
826 |
|
assertTrue(q.isEmpty()); |
827 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
828 |
< |
assertTrue(p.isTerminated()); |
829 |
< |
for (ScheduledFuture task : tasks) { |
830 |
< |
assertTrue(task.isDone()); |
831 |
< |
assertTrue(task.isCancelled()); |
827 |
> |
|
828 |
> |
// Add second wave of tasks. |
829 |
> |
immediates.add(p.submit(task)); |
830 |
> |
long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS; |
831 |
> |
delayeds.add(p.schedule(task, delay_ms, MILLISECONDS)); |
832 |
> |
for (int rounds : new int[] { 1, 2 }) { |
833 |
> |
periodics.add(p.scheduleAtFixedRate( |
834 |
> |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
835 |
> |
periodics.add(p.scheduleWithFixedDelay( |
836 |
> |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
837 |
|
} |
777 |
– |
} |
838 |
|
|
839 |
< |
/** |
840 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
841 |
< |
* periodic tasks are cancelled at shutdown |
842 |
< |
*/ |
843 |
< |
public void testShutdown3() throws InterruptedException { |
844 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
845 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
846 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
847 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
788 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
789 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
790 |
< |
long initialDelay = LONG_DELAY_MS; |
791 |
< |
ScheduledFuture task = |
792 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
793 |
< |
5, MILLISECONDS); |
839 |
> |
assertEquals(poolSize, q.size()); |
840 |
> |
assertEquals(poolSize, ran.get()); |
841 |
> |
|
842 |
> |
immediates.forEach( |
843 |
> |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
844 |
> |
|
845 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
846 |
> |
.forEach(f -> assertFalse(f.isDone())); |
847 |
> |
|
848 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
849 |
|
assertTrue(p.isShutdown()); |
850 |
< |
assertTrue(p.getQueue().isEmpty()); |
851 |
< |
assertTrue(task.isDone()); |
798 |
< |
assertTrue(task.isCancelled()); |
799 |
< |
joinPool(p); |
800 |
< |
} |
850 |
> |
assertTrue(p.isTerminating()); |
851 |
> |
assertFalse(p.isTerminated()); |
852 |
|
|
853 |
< |
/** |
854 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
855 |
< |
* periodic tasks are not cancelled at shutdown |
856 |
< |
*/ |
857 |
< |
public void testShutdown4() throws InterruptedException { |
858 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
859 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
860 |
< |
try { |
861 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
862 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
863 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
864 |
< |
final Runnable r = new CheckedRunnable() { |
865 |
< |
public void realRun() { |
866 |
< |
counter.countDown(); |
867 |
< |
}}; |
868 |
< |
ScheduledFuture task = |
869 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
870 |
< |
assertFalse(task.isDone()); |
871 |
< |
assertFalse(task.isCancelled()); |
872 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
873 |
< |
assertFalse(task.isCancelled()); |
874 |
< |
assertFalse(p.isTerminated()); |
875 |
< |
assertTrue(p.isShutdown()); |
876 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
877 |
< |
assertFalse(task.isCancelled()); |
878 |
< |
assertTrue(task.cancel(false)); |
879 |
< |
assertTrue(task.isDone()); |
880 |
< |
assertTrue(task.isCancelled()); |
881 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
882 |
< |
assertTrue(p.isTerminated()); |
883 |
< |
} |
884 |
< |
finally { |
885 |
< |
joinPool(p); |
853 |
> |
if (rnd.nextBoolean()) |
854 |
> |
assertThrows( |
855 |
> |
RejectedExecutionException.class, |
856 |
> |
() -> p.submit(task), |
857 |
> |
() -> p.schedule(task, 1, SECONDS), |
858 |
> |
() -> p.scheduleAtFixedRate( |
859 |
> |
new PeriodicTask(1), 1, 1, SECONDS), |
860 |
> |
() -> p.scheduleWithFixedDelay( |
861 |
> |
new PeriodicTask(2), 1, 1, SECONDS)); |
862 |
> |
|
863 |
> |
assertTrue(q.contains(immediates.get(1))); |
864 |
> |
assertTrue(!effectiveDelayedPolicy |
865 |
> |
^ q.contains(delayeds.get(1))); |
866 |
> |
assertTrue(!effectivePeriodicPolicy |
867 |
> |
^ q.containsAll(periodics.subList(4, 8))); |
868 |
> |
|
869 |
> |
immediates.forEach(f -> assertFalse(f.isDone())); |
870 |
> |
|
871 |
> |
assertFalse(delayeds.get(0).isDone()); |
872 |
> |
if (effectiveDelayedPolicy) |
873 |
> |
assertFalse(delayeds.get(1).isDone()); |
874 |
> |
else |
875 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
876 |
> |
|
877 |
> |
if (testImplementationDetails) { |
878 |
> |
if (effectivePeriodicPolicy) |
879 |
> |
// TODO: ensure periodic tasks continue executing |
880 |
> |
periodics.forEach( |
881 |
> |
f -> { |
882 |
> |
assertFalse(f.isDone()); |
883 |
> |
assertTrue(f.cancel(false)); |
884 |
> |
}); |
885 |
> |
else { |
886 |
> |
periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); |
887 |
> |
periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); |
888 |
> |
} |
889 |
|
} |
890 |
+ |
|
891 |
+ |
unblock.countDown(); // Release all pool threads |
892 |
+ |
|
893 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
894 |
+ |
assertFalse(p.isTerminating()); |
895 |
+ |
assertTrue(p.isTerminated()); |
896 |
+ |
|
897 |
+ |
assertTrue(q.isEmpty()); |
898 |
+ |
|
899 |
+ |
for (Future<?> f : immediates) assertNull(f.get()); |
900 |
+ |
|
901 |
+ |
assertNull(delayeds.get(0).get()); |
902 |
+ |
if (effectiveDelayedPolicy) |
903 |
+ |
assertNull(delayeds.get(1).get()); |
904 |
+ |
else |
905 |
+ |
assertTrue(delayeds.get(1).isCancelled()); |
906 |
+ |
|
907 |
+ |
periodics.forEach(f -> assertTrue(f.isDone())); |
908 |
+ |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
909 |
+ |
|
910 |
+ |
assertEquals(poolSize + 1 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
911 |
|
} |
912 |
|
|
913 |
|
/** |
914 |
|
* completed submit of callable returns result |
915 |
|
*/ |
916 |
|
public void testSubmitCallable() throws Exception { |
917 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
918 |
< |
try { |
917 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
918 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
919 |
|
Future<String> future = e.submit(new StringTask()); |
920 |
|
String result = future.get(); |
921 |
|
assertSame(TEST_STRING, result); |
847 |
– |
} finally { |
848 |
– |
joinPool(e); |
922 |
|
} |
923 |
|
} |
924 |
|
|
926 |
|
* completed submit of runnable returns successfully |
927 |
|
*/ |
928 |
|
public void testSubmitRunnable() throws Exception { |
929 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
930 |
< |
try { |
929 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
930 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
931 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
932 |
|
future.get(); |
933 |
|
assertTrue(future.isDone()); |
861 |
– |
} finally { |
862 |
– |
joinPool(e); |
934 |
|
} |
935 |
|
} |
936 |
|
|
938 |
|
* completed submit of (runnable, result) returns result |
939 |
|
*/ |
940 |
|
public void testSubmitRunnable2() throws Exception { |
941 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
942 |
< |
try { |
941 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
942 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
943 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
944 |
|
String result = future.get(); |
945 |
|
assertSame(TEST_STRING, result); |
875 |
– |
} finally { |
876 |
– |
joinPool(e); |
946 |
|
} |
947 |
|
} |
948 |
|
|
950 |
|
* invokeAny(null) throws NPE |
951 |
|
*/ |
952 |
|
public void testInvokeAny1() throws Exception { |
953 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
954 |
< |
try { |
955 |
< |
e.invokeAny(null); |
956 |
< |
shouldThrow(); |
957 |
< |
} catch (NullPointerException success) { |
958 |
< |
} finally { |
890 |
< |
joinPool(e); |
953 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
954 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
955 |
> |
try { |
956 |
> |
e.invokeAny(null); |
957 |
> |
shouldThrow(); |
958 |
> |
} catch (NullPointerException success) {} |
959 |
|
} |
960 |
|
} |
961 |
|
|
963 |
|
* invokeAny(empty collection) throws IAE |
964 |
|
*/ |
965 |
|
public void testInvokeAny2() throws Exception { |
966 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
967 |
< |
try { |
968 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
969 |
< |
shouldThrow(); |
970 |
< |
} catch (IllegalArgumentException success) { |
971 |
< |
} finally { |
904 |
< |
joinPool(e); |
966 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
967 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
968 |
> |
try { |
969 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
970 |
> |
shouldThrow(); |
971 |
> |
} catch (IllegalArgumentException success) {} |
972 |
|
} |
973 |
|
} |
974 |
|
|
977 |
|
*/ |
978 |
|
public void testInvokeAny3() throws Exception { |
979 |
|
CountDownLatch latch = new CountDownLatch(1); |
980 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
981 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
982 |
< |
l.add(latchAwaitingStringTask(latch)); |
983 |
< |
l.add(null); |
984 |
< |
try { |
985 |
< |
e.invokeAny(l); |
986 |
< |
shouldThrow(); |
987 |
< |
} catch (NullPointerException success) { |
988 |
< |
} finally { |
980 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
981 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
982 |
> |
List<Callable<String>> l = new ArrayList<>(); |
983 |
> |
l.add(latchAwaitingStringTask(latch)); |
984 |
> |
l.add(null); |
985 |
> |
try { |
986 |
> |
e.invokeAny(l); |
987 |
> |
shouldThrow(); |
988 |
> |
} catch (NullPointerException success) {} |
989 |
|
latch.countDown(); |
923 |
– |
joinPool(e); |
990 |
|
} |
991 |
|
} |
992 |
|
|
994 |
|
* invokeAny(c) throws ExecutionException if no task completes |
995 |
|
*/ |
996 |
|
public void testInvokeAny4() throws Exception { |
997 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
998 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
< |
l.add(new NPETask()); |
1000 |
< |
try { |
1001 |
< |
e.invokeAny(l); |
1002 |
< |
shouldThrow(); |
1003 |
< |
} catch (ExecutionException success) { |
1004 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1005 |
< |
} finally { |
1006 |
< |
joinPool(e); |
997 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
998 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
999 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1000 |
> |
l.add(new NPETask()); |
1001 |
> |
try { |
1002 |
> |
e.invokeAny(l); |
1003 |
> |
shouldThrow(); |
1004 |
> |
} catch (ExecutionException success) { |
1005 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1006 |
> |
} |
1007 |
|
} |
1008 |
|
} |
1009 |
|
|
1011 |
|
* invokeAny(c) returns result of some task |
1012 |
|
*/ |
1013 |
|
public void testInvokeAny5() throws Exception { |
1014 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1015 |
< |
try { |
1016 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1014 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1015 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1016 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1017 |
|
l.add(new StringTask()); |
1018 |
|
l.add(new StringTask()); |
1019 |
|
String result = e.invokeAny(l); |
1020 |
|
assertSame(TEST_STRING, result); |
955 |
– |
} finally { |
956 |
– |
joinPool(e); |
1021 |
|
} |
1022 |
|
} |
1023 |
|
|
1025 |
|
* invokeAll(null) throws NPE |
1026 |
|
*/ |
1027 |
|
public void testInvokeAll1() throws Exception { |
1028 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1029 |
< |
try { |
1030 |
< |
e.invokeAll(null); |
1031 |
< |
shouldThrow(); |
1032 |
< |
} catch (NullPointerException success) { |
1033 |
< |
} finally { |
970 |
< |
joinPool(e); |
1028 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1029 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1030 |
> |
try { |
1031 |
> |
e.invokeAll(null); |
1032 |
> |
shouldThrow(); |
1033 |
> |
} catch (NullPointerException success) {} |
1034 |
|
} |
1035 |
|
} |
1036 |
|
|
1038 |
|
* invokeAll(empty collection) returns empty collection |
1039 |
|
*/ |
1040 |
|
public void testInvokeAll2() throws Exception { |
1041 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1042 |
< |
try { |
1041 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1042 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1043 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1044 |
|
assertTrue(r.isEmpty()); |
982 |
– |
} finally { |
983 |
– |
joinPool(e); |
1045 |
|
} |
1046 |
|
} |
1047 |
|
|
1049 |
|
* invokeAll(c) throws NPE if c has null elements |
1050 |
|
*/ |
1051 |
|
public void testInvokeAll3() throws Exception { |
1052 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1053 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1054 |
< |
l.add(new StringTask()); |
1055 |
< |
l.add(null); |
1056 |
< |
try { |
1057 |
< |
e.invokeAll(l); |
1058 |
< |
shouldThrow(); |
1059 |
< |
} catch (NullPointerException success) { |
1060 |
< |
} finally { |
1000 |
< |
joinPool(e); |
1052 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1053 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1054 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1055 |
> |
l.add(new StringTask()); |
1056 |
> |
l.add(null); |
1057 |
> |
try { |
1058 |
> |
e.invokeAll(l); |
1059 |
> |
shouldThrow(); |
1060 |
> |
} catch (NullPointerException success) {} |
1061 |
|
} |
1062 |
|
} |
1063 |
|
|
1065 |
|
* get of invokeAll(c) throws exception on failed task |
1066 |
|
*/ |
1067 |
|
public void testInvokeAll4() throws Exception { |
1068 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1069 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1070 |
< |
l.add(new NPETask()); |
1071 |
< |
List<Future<String>> futures = e.invokeAll(l); |
1072 |
< |
assertEquals(1, futures.size()); |
1073 |
< |
try { |
1074 |
< |
futures.get(0).get(); |
1075 |
< |
shouldThrow(); |
1076 |
< |
} catch (ExecutionException success) { |
1077 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1078 |
< |
} finally { |
1079 |
< |
joinPool(e); |
1068 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1069 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1070 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1071 |
> |
l.add(new NPETask()); |
1072 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1073 |
> |
assertEquals(1, futures.size()); |
1074 |
> |
try { |
1075 |
> |
futures.get(0).get(); |
1076 |
> |
shouldThrow(); |
1077 |
> |
} catch (ExecutionException success) { |
1078 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1079 |
> |
} |
1080 |
|
} |
1081 |
|
} |
1082 |
|
|
1084 |
|
* invokeAll(c) returns results of all completed tasks |
1085 |
|
*/ |
1086 |
|
public void testInvokeAll5() throws Exception { |
1087 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1088 |
< |
try { |
1089 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1087 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1088 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1089 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1090 |
|
l.add(new StringTask()); |
1091 |
|
l.add(new StringTask()); |
1092 |
|
List<Future<String>> futures = e.invokeAll(l); |
1093 |
|
assertEquals(2, futures.size()); |
1094 |
|
for (Future<String> future : futures) |
1095 |
|
assertSame(TEST_STRING, future.get()); |
1036 |
– |
} finally { |
1037 |
– |
joinPool(e); |
1096 |
|
} |
1097 |
|
} |
1098 |
|
|
1100 |
|
* timed invokeAny(null) throws NPE |
1101 |
|
*/ |
1102 |
|
public void testTimedInvokeAny1() throws Exception { |
1103 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1104 |
< |
try { |
1105 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1106 |
< |
shouldThrow(); |
1107 |
< |
} catch (NullPointerException success) { |
1108 |
< |
} finally { |
1051 |
< |
joinPool(e); |
1103 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1104 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1105 |
> |
try { |
1106 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1107 |
> |
shouldThrow(); |
1108 |
> |
} catch (NullPointerException success) {} |
1109 |
|
} |
1110 |
|
} |
1111 |
|
|
1113 |
|
* timed invokeAny(,,null) throws NPE |
1114 |
|
*/ |
1115 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1116 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1117 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1118 |
< |
l.add(new StringTask()); |
1119 |
< |
try { |
1120 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1121 |
< |
shouldThrow(); |
1122 |
< |
} catch (NullPointerException success) { |
1123 |
< |
} finally { |
1067 |
< |
joinPool(e); |
1116 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1117 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1118 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1119 |
> |
l.add(new StringTask()); |
1120 |
> |
try { |
1121 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1122 |
> |
shouldThrow(); |
1123 |
> |
} catch (NullPointerException success) {} |
1124 |
|
} |
1125 |
|
} |
1126 |
|
|
1128 |
|
* timed invokeAny(empty collection) throws IAE |
1129 |
|
*/ |
1130 |
|
public void testTimedInvokeAny2() throws Exception { |
1131 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1132 |
< |
try { |
1133 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1134 |
< |
shouldThrow(); |
1135 |
< |
} catch (IllegalArgumentException success) { |
1136 |
< |
} finally { |
1081 |
< |
joinPool(e); |
1131 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1132 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1133 |
> |
try { |
1134 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1135 |
> |
shouldThrow(); |
1136 |
> |
} catch (IllegalArgumentException success) {} |
1137 |
|
} |
1138 |
|
} |
1139 |
|
|
1142 |
|
*/ |
1143 |
|
public void testTimedInvokeAny3() throws Exception { |
1144 |
|
CountDownLatch latch = new CountDownLatch(1); |
1145 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1146 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1147 |
< |
l.add(latchAwaitingStringTask(latch)); |
1148 |
< |
l.add(null); |
1149 |
< |
try { |
1150 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1151 |
< |
shouldThrow(); |
1152 |
< |
} catch (NullPointerException success) { |
1153 |
< |
} finally { |
1145 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1146 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1147 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1148 |
> |
l.add(latchAwaitingStringTask(latch)); |
1149 |
> |
l.add(null); |
1150 |
> |
try { |
1151 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1152 |
> |
shouldThrow(); |
1153 |
> |
} catch (NullPointerException success) {} |
1154 |
|
latch.countDown(); |
1100 |
– |
joinPool(e); |
1155 |
|
} |
1156 |
|
} |
1157 |
|
|
1159 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1160 |
|
*/ |
1161 |
|
public void testTimedInvokeAny4() throws Exception { |
1162 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1163 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1164 |
< |
l.add(new NPETask()); |
1165 |
< |
try { |
1166 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1167 |
< |
shouldThrow(); |
1168 |
< |
} catch (ExecutionException success) { |
1169 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1170 |
< |
} finally { |
1171 |
< |
joinPool(e); |
1162 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1163 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1164 |
> |
long startTime = System.nanoTime(); |
1165 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1166 |
> |
l.add(new NPETask()); |
1167 |
> |
try { |
1168 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1169 |
> |
shouldThrow(); |
1170 |
> |
} catch (ExecutionException success) { |
1171 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1172 |
> |
} |
1173 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1174 |
|
} |
1175 |
|
} |
1176 |
|
|
1178 |
|
* timed invokeAny(c) returns result of some task |
1179 |
|
*/ |
1180 |
|
public void testTimedInvokeAny5() throws Exception { |
1181 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1182 |
< |
try { |
1183 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1181 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1182 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1183 |
> |
long startTime = System.nanoTime(); |
1184 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1185 |
|
l.add(new StringTask()); |
1186 |
|
l.add(new StringTask()); |
1187 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1187 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1188 |
|
assertSame(TEST_STRING, result); |
1189 |
< |
} finally { |
1133 |
< |
joinPool(e); |
1189 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1190 |
|
} |
1191 |
|
} |
1192 |
|
|
1194 |
|
* timed invokeAll(null) throws NPE |
1195 |
|
*/ |
1196 |
|
public void testTimedInvokeAll1() throws Exception { |
1197 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1198 |
< |
try { |
1199 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1200 |
< |
shouldThrow(); |
1201 |
< |
} catch (NullPointerException success) { |
1202 |
< |
} finally { |
1147 |
< |
joinPool(e); |
1197 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1198 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1199 |
> |
try { |
1200 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1201 |
> |
shouldThrow(); |
1202 |
> |
} catch (NullPointerException success) {} |
1203 |
|
} |
1204 |
|
} |
1205 |
|
|
1207 |
|
* timed invokeAll(,,null) throws NPE |
1208 |
|
*/ |
1209 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1210 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1211 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1212 |
< |
l.add(new StringTask()); |
1213 |
< |
try { |
1214 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1215 |
< |
shouldThrow(); |
1216 |
< |
} catch (NullPointerException success) { |
1217 |
< |
} finally { |
1163 |
< |
joinPool(e); |
1210 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1211 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1212 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1213 |
> |
l.add(new StringTask()); |
1214 |
> |
try { |
1215 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1216 |
> |
shouldThrow(); |
1217 |
> |
} catch (NullPointerException success) {} |
1218 |
|
} |
1219 |
|
} |
1220 |
|
|
1222 |
|
* timed invokeAll(empty collection) returns empty collection |
1223 |
|
*/ |
1224 |
|
public void testTimedInvokeAll2() throws Exception { |
1225 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1226 |
< |
try { |
1227 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1225 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1226 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1227 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1228 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1229 |
|
assertTrue(r.isEmpty()); |
1175 |
– |
} finally { |
1176 |
– |
joinPool(e); |
1230 |
|
} |
1231 |
|
} |
1232 |
|
|
1234 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1235 |
|
*/ |
1236 |
|
public void testTimedInvokeAll3() throws Exception { |
1237 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1238 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1239 |
< |
l.add(new StringTask()); |
1240 |
< |
l.add(null); |
1241 |
< |
try { |
1242 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1243 |
< |
shouldThrow(); |
1244 |
< |
} catch (NullPointerException success) { |
1245 |
< |
} finally { |
1193 |
< |
joinPool(e); |
1237 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1238 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1239 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1240 |
> |
l.add(new StringTask()); |
1241 |
> |
l.add(null); |
1242 |
> |
try { |
1243 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1244 |
> |
shouldThrow(); |
1245 |
> |
} catch (NullPointerException success) {} |
1246 |
|
} |
1247 |
|
} |
1248 |
|
|
1250 |
|
* get of element of invokeAll(c) throws exception on failed task |
1251 |
|
*/ |
1252 |
|
public void testTimedInvokeAll4() throws Exception { |
1253 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1254 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1255 |
< |
l.add(new NPETask()); |
1256 |
< |
List<Future<String>> futures = |
1257 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1258 |
< |
assertEquals(1, futures.size()); |
1259 |
< |
try { |
1260 |
< |
futures.get(0).get(); |
1261 |
< |
shouldThrow(); |
1262 |
< |
} catch (ExecutionException success) { |
1263 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1264 |
< |
} finally { |
1265 |
< |
joinPool(e); |
1253 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1254 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1255 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1256 |
> |
l.add(new NPETask()); |
1257 |
> |
List<Future<String>> futures = |
1258 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1259 |
> |
assertEquals(1, futures.size()); |
1260 |
> |
try { |
1261 |
> |
futures.get(0).get(); |
1262 |
> |
shouldThrow(); |
1263 |
> |
} catch (ExecutionException success) { |
1264 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1265 |
> |
} |
1266 |
|
} |
1267 |
|
} |
1268 |
|
|
1270 |
|
* timed invokeAll(c) returns results of all completed tasks |
1271 |
|
*/ |
1272 |
|
public void testTimedInvokeAll5() throws Exception { |
1273 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1274 |
< |
try { |
1275 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1273 |
> |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1274 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1275 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1276 |
|
l.add(new StringTask()); |
1277 |
|
l.add(new StringTask()); |
1278 |
|
List<Future<String>> futures = |
1279 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1279 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1280 |
|
assertEquals(2, futures.size()); |
1281 |
|
for (Future<String> future : futures) |
1282 |
|
assertSame(TEST_STRING, future.get()); |
1231 |
– |
} finally { |
1232 |
– |
joinPool(e); |
1283 |
|
} |
1284 |
|
} |
1285 |
|
|
1287 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1288 |
|
*/ |
1289 |
|
public void testTimedInvokeAll6() throws Exception { |
1290 |
< |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1291 |
< |
try { |
1292 |
< |
for (long timeout = timeoutMillis();;) { |
1290 |
> |
for (long timeout = timeoutMillis();;) { |
1291 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1292 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1293 |
> |
public String realCall() { |
1294 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1295 |
> |
catch (InterruptedException ok) {} |
1296 |
> |
return "1"; }}; |
1297 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1298 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1299 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1300 |
|
tasks.add(new StringTask("0")); |
1301 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1301 |
> |
tasks.add(waiter); |
1302 |
|
tasks.add(new StringTask("2")); |
1303 |
|
long startTime = System.nanoTime(); |
1304 |
|
List<Future<String>> futures = |
1305 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1305 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1306 |
|
assertEquals(tasks.size(), futures.size()); |
1307 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1308 |
|
for (Future future : futures) |
1318 |
|
fail("expected exactly one task to be cancelled"); |
1319 |
|
} |
1320 |
|
} |
1321 |
< |
} finally { |
1322 |
< |
joinPool(e); |
1321 |
> |
} |
1322 |
> |
} |
1323 |
> |
|
1324 |
> |
/** |
1325 |
> |
* A fixed delay task with overflowing period should not prevent a |
1326 |
> |
* one-shot task from executing. |
1327 |
> |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1328 |
> |
*/ |
1329 |
> |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1330 |
> |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1331 |
> |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1332 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1333 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1334 |
> |
final Runnable immediate = new Runnable() { public void run() { |
1335 |
> |
immediateDone.countDown(); |
1336 |
> |
}}; |
1337 |
> |
final Runnable delayed = new Runnable() { public void run() { |
1338 |
> |
delayedDone.countDown(); |
1339 |
> |
p.submit(immediate); |
1340 |
> |
}}; |
1341 |
> |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1342 |
> |
await(delayedDone); |
1343 |
> |
await(immediateDone); |
1344 |
|
} |
1345 |
|
} |
1346 |
|
|