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