1 |
dl |
1.1 |
/* |
2 |
dl |
1.13 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
|
|
|
13 |
dl |
1.4 |
public class ScheduledExecutorTest extends JSR166TestCase { |
14 |
dl |
1.1 |
public static void main(String[] args) { |
15 |
|
|
junit.textui.TestRunner.run (suite()); |
16 |
|
|
} |
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(ScheduledExecutorTest.class); |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
|
22 |
dl |
1.5 |
/** |
23 |
dl |
1.6 |
* execute successfully executes a runnable |
24 |
dl |
1.5 |
*/ |
25 |
|
|
public void testExecute() { |
26 |
|
|
try { |
27 |
dl |
1.8 |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
28 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
29 |
dl |
1.5 |
p1.execute(runnable); |
30 |
dl |
1.4 |
assertFalse(runnable.done); |
31 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
32 |
dl |
1.5 |
p1.shutdown(); |
33 |
|
|
try { |
34 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
35 |
|
|
} catch(InterruptedException e){ |
36 |
dl |
1.5 |
unexpectedException(); |
37 |
dl |
1.2 |
} |
38 |
dl |
1.1 |
assertTrue(runnable.done); |
39 |
dl |
1.5 |
p1.shutdown(); |
40 |
|
|
joinPool(p1); |
41 |
dl |
1.1 |
} |
42 |
|
|
catch(Exception e){ |
43 |
dl |
1.5 |
unexpectedException(); |
44 |
dl |
1.1 |
} |
45 |
dl |
1.4 |
|
46 |
dl |
1.1 |
} |
47 |
|
|
|
48 |
dl |
1.6 |
|
49 |
dl |
1.5 |
/** |
50 |
dl |
1.6 |
* delayed schedule of callable successfully executes after delay |
51 |
dl |
1.5 |
*/ |
52 |
|
|
public void testSchedule1() { |
53 |
|
|
try { |
54 |
dl |
1.7 |
TrackedCallable callable = new TrackedCallable(); |
55 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
56 |
dl |
1.5 |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
57 |
dl |
1.4 |
assertFalse(callable.done); |
58 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
59 |
dl |
1.1 |
assertTrue(callable.done); |
60 |
|
|
assertEquals(Boolean.TRUE, f.get()); |
61 |
dl |
1.5 |
p1.shutdown(); |
62 |
|
|
joinPool(p1); |
63 |
|
|
} catch(RejectedExecutionException e){} |
64 |
dl |
1.2 |
catch(Exception e){ |
65 |
dl |
1.9 |
e.printStackTrace(); |
66 |
dl |
1.5 |
unexpectedException(); |
67 |
dl |
1.2 |
} |
68 |
dl |
1.1 |
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
dl |
1.6 |
* delayed schedule of runnable successfully executes after delay |
72 |
dl |
1.1 |
*/ |
73 |
dl |
1.5 |
public void testSchedule3() { |
74 |
|
|
try { |
75 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
76 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
77 |
dl |
1.5 |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
78 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
79 |
|
|
assertFalse(runnable.done); |
80 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
81 |
dl |
1.1 |
assertTrue(runnable.done); |
82 |
dl |
1.5 |
p1.shutdown(); |
83 |
|
|
joinPool(p1); |
84 |
dl |
1.1 |
} catch(Exception e){ |
85 |
dl |
1.5 |
unexpectedException(); |
86 |
dl |
1.1 |
} |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
/** |
90 |
dl |
1.6 |
* scheduleAtFixedRate executes runnable after given initial delay |
91 |
dl |
1.1 |
*/ |
92 |
dl |
1.5 |
public void testSchedule4() { |
93 |
|
|
try { |
94 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
95 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
96 |
|
|
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
97 |
dl |
1.6 |
assertFalse(runnable.done); |
98 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
99 |
|
|
assertTrue(runnable.done); |
100 |
|
|
h.cancel(true); |
101 |
|
|
p1.shutdown(); |
102 |
|
|
joinPool(p1); |
103 |
|
|
} catch(Exception e){ |
104 |
|
|
unexpectedException(); |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
* scheduleWithFixedDelay executes runnable after given initial delay |
110 |
|
|
*/ |
111 |
|
|
public void testSchedule5() { |
112 |
|
|
try { |
113 |
dl |
1.8 |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
114 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
115 |
|
|
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
116 |
dl |
1.4 |
assertFalse(runnable.done); |
117 |
dl |
1.2 |
Thread.sleep(MEDIUM_DELAY_MS); |
118 |
dl |
1.1 |
assertTrue(runnable.done); |
119 |
dl |
1.6 |
h.cancel(true); |
120 |
dl |
1.5 |
p1.shutdown(); |
121 |
|
|
joinPool(p1); |
122 |
dl |
1.1 |
} catch(Exception e){ |
123 |
dl |
1.5 |
unexpectedException(); |
124 |
dl |
1.1 |
} |
125 |
|
|
} |
126 |
|
|
|
127 |
dl |
1.6 |
/** |
128 |
|
|
* execute (null) throws NPE |
129 |
|
|
*/ |
130 |
|
|
public void testExecuteNull() { |
131 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = null; |
132 |
dl |
1.6 |
try { |
133 |
dl |
1.9 |
se = new ScheduledThreadPoolExecutor(1); |
134 |
dl |
1.6 |
se.execute(null); |
135 |
|
|
shouldThrow(); |
136 |
|
|
} catch(NullPointerException success){} |
137 |
|
|
catch(Exception e){ |
138 |
|
|
unexpectedException(); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
joinPool(se); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* schedule (null) throws NPE |
146 |
|
|
*/ |
147 |
|
|
public void testScheduleNull() { |
148 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
149 |
dl |
1.6 |
try { |
150 |
dl |
1.7 |
TrackedCallable callable = null; |
151 |
dl |
1.6 |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
152 |
|
|
shouldThrow(); |
153 |
|
|
} catch(NullPointerException success){} |
154 |
|
|
catch(Exception e){ |
155 |
|
|
unexpectedException(); |
156 |
|
|
} |
157 |
|
|
joinPool(se); |
158 |
|
|
} |
159 |
dl |
1.1 |
|
160 |
|
|
/** |
161 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
162 |
dl |
1.1 |
*/ |
163 |
dl |
1.5 |
public void testSchedule1_RejectedExecutionException() { |
164 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
165 |
dl |
1.5 |
try { |
166 |
dl |
1.1 |
se.shutdown(); |
167 |
dl |
1.4 |
se.schedule(new NoOpRunnable(), |
168 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
169 |
dl |
1.5 |
shouldThrow(); |
170 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
171 |
|
|
} |
172 |
|
|
joinPool(se); |
173 |
|
|
|
174 |
dl |
1.1 |
} |
175 |
|
|
|
176 |
|
|
/** |
177 |
dl |
1.6 |
* schedule throws RejectedExecutionException if shutdown |
178 |
dl |
1.1 |
*/ |
179 |
dl |
1.5 |
public void testSchedule2_RejectedExecutionException() { |
180 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
181 |
dl |
1.5 |
try { |
182 |
dl |
1.1 |
se.shutdown(); |
183 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
184 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
185 |
dl |
1.5 |
shouldThrow(); |
186 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
187 |
|
|
} |
188 |
|
|
joinPool(se); |
189 |
dl |
1.1 |
} |
190 |
|
|
|
191 |
|
|
/** |
192 |
dl |
1.6 |
* schedule callable throws RejectedExecutionException if shutdown |
193 |
dl |
1.1 |
*/ |
194 |
dl |
1.5 |
public void testSchedule3_RejectedExecutionException() { |
195 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
196 |
dl |
1.5 |
try { |
197 |
dl |
1.1 |
se.shutdown(); |
198 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
199 |
|
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
200 |
dl |
1.5 |
shouldThrow(); |
201 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
202 |
|
|
} |
203 |
|
|
joinPool(se); |
204 |
dl |
1.1 |
} |
205 |
|
|
|
206 |
|
|
/** |
207 |
dl |
1.6 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
208 |
dl |
1.1 |
*/ |
209 |
dl |
1.5 |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
210 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
211 |
dl |
1.5 |
try { |
212 |
dl |
1.1 |
se.shutdown(); |
213 |
dl |
1.4 |
se.scheduleAtFixedRate(new NoOpRunnable(), |
214 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
215 |
dl |
1.5 |
shouldThrow(); |
216 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
217 |
|
|
} |
218 |
|
|
joinPool(se); |
219 |
dl |
1.1 |
} |
220 |
|
|
|
221 |
|
|
/** |
222 |
dl |
1.6 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
223 |
dl |
1.1 |
*/ |
224 |
dl |
1.5 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
225 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
226 |
dl |
1.5 |
try { |
227 |
dl |
1.1 |
se.shutdown(); |
228 |
dl |
1.4 |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
229 |
|
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
230 |
dl |
1.5 |
shouldThrow(); |
231 |
dl |
1.4 |
} catch(RejectedExecutionException success){ |
232 |
|
|
} |
233 |
|
|
joinPool(se); |
234 |
dl |
1.1 |
} |
235 |
|
|
|
236 |
|
|
/** |
237 |
dl |
1.6 |
* getActiveCount increases but doesn't overestimate, when a |
238 |
|
|
* thread becomes active |
239 |
dl |
1.2 |
*/ |
240 |
dl |
1.5 |
public void testGetActiveCount() { |
241 |
dl |
1.9 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
242 |
dl |
1.5 |
assertEquals(0, p2.getActiveCount()); |
243 |
|
|
p2.execute(new SmallRunnable()); |
244 |
|
|
try { |
245 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
246 |
|
|
} catch(Exception e){ |
247 |
dl |
1.5 |
unexpectedException(); |
248 |
dl |
1.2 |
} |
249 |
dl |
1.5 |
assertEquals(1, p2.getActiveCount()); |
250 |
|
|
joinPool(p2); |
251 |
dl |
1.2 |
} |
252 |
|
|
|
253 |
|
|
/** |
254 |
dl |
1.6 |
* getCompletedTaskCount increases, but doesn't overestimate, |
255 |
|
|
* when tasks complete |
256 |
dl |
1.2 |
*/ |
257 |
dl |
1.5 |
public void testGetCompletedTaskCount() { |
258 |
dl |
1.9 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
259 |
dl |
1.5 |
assertEquals(0, p2.getCompletedTaskCount()); |
260 |
|
|
p2.execute(new SmallRunnable()); |
261 |
|
|
try { |
262 |
dl |
1.4 |
Thread.sleep(MEDIUM_DELAY_MS); |
263 |
|
|
} catch(Exception e){ |
264 |
dl |
1.5 |
unexpectedException(); |
265 |
dl |
1.2 |
} |
266 |
dl |
1.5 |
assertEquals(1, p2.getCompletedTaskCount()); |
267 |
|
|
joinPool(p2); |
268 |
dl |
1.2 |
} |
269 |
|
|
|
270 |
|
|
/** |
271 |
dl |
1.6 |
* getCorePoolSize returns size given in constructor if not otherwise set |
272 |
dl |
1.2 |
*/ |
273 |
dl |
1.5 |
public void testGetCorePoolSize() { |
274 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
275 |
dl |
1.5 |
assertEquals(1, p1.getCorePoolSize()); |
276 |
|
|
joinPool(p1); |
277 |
dl |
1.2 |
} |
278 |
|
|
|
279 |
|
|
/** |
280 |
dl |
1.6 |
* getLargestPoolSize increases, but doesn't overestimate, when |
281 |
|
|
* multiple threads active |
282 |
dl |
1.2 |
*/ |
283 |
dl |
1.5 |
public void testGetLargestPoolSize() { |
284 |
dl |
1.9 |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
285 |
dl |
1.5 |
assertEquals(0, p2.getLargestPoolSize()); |
286 |
|
|
p2.execute(new SmallRunnable()); |
287 |
|
|
p2.execute(new SmallRunnable()); |
288 |
|
|
try { |
289 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
290 |
|
|
} catch(Exception e){ |
291 |
dl |
1.5 |
unexpectedException(); |
292 |
dl |
1.2 |
} |
293 |
dl |
1.5 |
assertEquals(2, p2.getLargestPoolSize()); |
294 |
|
|
joinPool(p2); |
295 |
dl |
1.2 |
} |
296 |
|
|
|
297 |
|
|
/** |
298 |
dl |
1.6 |
* getPoolSize increases, but doesn't overestimate, when threads |
299 |
|
|
* become active |
300 |
dl |
1.2 |
*/ |
301 |
dl |
1.5 |
public void testGetPoolSize() { |
302 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
303 |
dl |
1.5 |
assertEquals(0, p1.getPoolSize()); |
304 |
|
|
p1.execute(new SmallRunnable()); |
305 |
|
|
assertEquals(1, p1.getPoolSize()); |
306 |
|
|
joinPool(p1); |
307 |
dl |
1.2 |
} |
308 |
|
|
|
309 |
|
|
/** |
310 |
dl |
1.6 |
* getTaskCount increases, but doesn't overestimate, when tasks |
311 |
|
|
* submitted |
312 |
dl |
1.2 |
*/ |
313 |
dl |
1.5 |
public void testGetTaskCount() { |
314 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
315 |
dl |
1.5 |
assertEquals(0, p1.getTaskCount()); |
316 |
dl |
1.4 |
for(int i = 0; i < 5; i++) |
317 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
318 |
|
|
try { |
319 |
dl |
1.4 |
Thread.sleep(SHORT_DELAY_MS); |
320 |
|
|
} catch(Exception e){ |
321 |
dl |
1.5 |
unexpectedException(); |
322 |
dl |
1.2 |
} |
323 |
dl |
1.5 |
assertEquals(5, p1.getTaskCount()); |
324 |
|
|
joinPool(p1); |
325 |
dl |
1.2 |
} |
326 |
dl |
1.8 |
|
327 |
|
|
/** |
328 |
|
|
* getThreadFactory returns factory in constructor if not set |
329 |
|
|
*/ |
330 |
|
|
public void testGetThreadFactory() { |
331 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
332 |
dl |
1.9 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
333 |
dl |
1.8 |
assertSame(tf, p.getThreadFactory()); |
334 |
|
|
p.shutdown(); |
335 |
|
|
joinPool(p); |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
/** |
339 |
|
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
340 |
|
|
*/ |
341 |
|
|
public void testSetThreadFactory() { |
342 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
343 |
dl |
1.9 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
344 |
dl |
1.8 |
p.setThreadFactory(tf); |
345 |
|
|
assertSame(tf, p.getThreadFactory()); |
346 |
|
|
p.shutdown(); |
347 |
|
|
joinPool(p); |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
/** |
351 |
|
|
* setThreadFactory(null) throws NPE |
352 |
|
|
*/ |
353 |
|
|
public void testSetThreadFactoryNull() { |
354 |
dl |
1.9 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
355 |
dl |
1.8 |
try { |
356 |
|
|
p.setThreadFactory(null); |
357 |
|
|
shouldThrow(); |
358 |
|
|
} catch (NullPointerException success) { |
359 |
|
|
} finally { |
360 |
|
|
joinPool(p); |
361 |
|
|
} |
362 |
|
|
} |
363 |
dl |
1.2 |
|
364 |
|
|
/** |
365 |
dl |
1.6 |
* is isShutDown is false before shutdown, true after |
366 |
dl |
1.2 |
*/ |
367 |
dl |
1.5 |
public void testIsShutdown() { |
368 |
dl |
1.2 |
|
369 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
370 |
dl |
1.2 |
try { |
371 |
dl |
1.5 |
assertFalse(p1.isShutdown()); |
372 |
dl |
1.2 |
} |
373 |
|
|
finally { |
374 |
dl |
1.5 |
p1.shutdown(); |
375 |
dl |
1.2 |
} |
376 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
377 |
dl |
1.2 |
} |
378 |
|
|
|
379 |
|
|
|
380 |
|
|
/** |
381 |
dl |
1.6 |
* isTerminated is false before termination, true after |
382 |
dl |
1.2 |
*/ |
383 |
dl |
1.5 |
public void testIsTerminated() { |
384 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
385 |
dl |
1.2 |
try { |
386 |
dl |
1.5 |
p1.execute(new SmallRunnable()); |
387 |
dl |
1.2 |
} finally { |
388 |
dl |
1.5 |
p1.shutdown(); |
389 |
dl |
1.2 |
} |
390 |
dl |
1.4 |
try { |
391 |
dl |
1.5 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
392 |
|
|
assertTrue(p1.isTerminated()); |
393 |
dl |
1.2 |
} catch(Exception e){ |
394 |
dl |
1.5 |
unexpectedException(); |
395 |
|
|
} |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
/** |
399 |
dl |
1.6 |
* isTerminating is not true when running or when terminated |
400 |
dl |
1.5 |
*/ |
401 |
|
|
public void testIsTerminating() { |
402 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
403 |
dl |
1.5 |
assertFalse(p1.isTerminating()); |
404 |
|
|
try { |
405 |
|
|
p1.execute(new SmallRunnable()); |
406 |
|
|
assertFalse(p1.isTerminating()); |
407 |
|
|
} finally { |
408 |
|
|
p1.shutdown(); |
409 |
|
|
} |
410 |
|
|
try { |
411 |
|
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
412 |
|
|
assertTrue(p1.isTerminated()); |
413 |
|
|
assertFalse(p1.isTerminating()); |
414 |
|
|
} catch(Exception e){ |
415 |
|
|
unexpectedException(); |
416 |
dl |
1.2 |
} |
417 |
|
|
} |
418 |
|
|
|
419 |
|
|
/** |
420 |
dl |
1.8 |
* getQueue returns the work queue, which contains queued tasks |
421 |
|
|
*/ |
422 |
|
|
public void testGetQueue() { |
423 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
424 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
425 |
dl |
1.8 |
for(int i = 0; i < 5; i++){ |
426 |
|
|
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
427 |
|
|
} |
428 |
|
|
try { |
429 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
430 |
|
|
BlockingQueue<Runnable> q = p1.getQueue(); |
431 |
|
|
assertTrue(q.contains(tasks[4])); |
432 |
|
|
assertFalse(q.contains(tasks[0])); |
433 |
|
|
p1.shutdownNow(); |
434 |
|
|
} catch(Exception e) { |
435 |
|
|
unexpectedException(); |
436 |
|
|
} finally { |
437 |
|
|
joinPool(p1); |
438 |
|
|
} |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
/** |
442 |
|
|
* remove(task) removes queued task, and fails to remove active task |
443 |
|
|
*/ |
444 |
|
|
public void testRemove() { |
445 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
446 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
447 |
dl |
1.8 |
for(int i = 0; i < 5; i++){ |
448 |
|
|
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
449 |
|
|
} |
450 |
|
|
try { |
451 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
452 |
|
|
BlockingQueue<Runnable> q = p1.getQueue(); |
453 |
|
|
assertFalse(p1.remove((Runnable)tasks[0])); |
454 |
|
|
assertTrue(q.contains((Runnable)tasks[4])); |
455 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
456 |
|
|
assertTrue(p1.remove((Runnable)tasks[4])); |
457 |
|
|
assertFalse(p1.remove((Runnable)tasks[4])); |
458 |
|
|
assertFalse(q.contains((Runnable)tasks[4])); |
459 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
460 |
|
|
assertTrue(p1.remove((Runnable)tasks[3])); |
461 |
|
|
assertFalse(q.contains((Runnable)tasks[3])); |
462 |
|
|
p1.shutdownNow(); |
463 |
|
|
} catch(Exception e) { |
464 |
|
|
unexpectedException(); |
465 |
|
|
} finally { |
466 |
|
|
joinPool(p1); |
467 |
|
|
} |
468 |
|
|
} |
469 |
|
|
|
470 |
|
|
/** |
471 |
|
|
* purge removes cancelled tasks from the queue |
472 |
dl |
1.2 |
*/ |
473 |
dl |
1.5 |
public void testPurge() { |
474 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
475 |
|
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
476 |
dl |
1.4 |
for(int i = 0; i < 5; i++){ |
477 |
dl |
1.8 |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
478 |
dl |
1.2 |
} |
479 |
dl |
1.4 |
int max = 5; |
480 |
|
|
if (tasks[4].cancel(true)) --max; |
481 |
|
|
if (tasks[3].cancel(true)) --max; |
482 |
dl |
1.5 |
p1.purge(); |
483 |
|
|
long count = p1.getTaskCount(); |
484 |
dl |
1.4 |
assertTrue(count > 0 && count <= max); |
485 |
dl |
1.5 |
joinPool(p1); |
486 |
dl |
1.2 |
} |
487 |
|
|
|
488 |
|
|
/** |
489 |
dl |
1.6 |
* shutDownNow returns a list containing tasks that were not run |
490 |
dl |
1.2 |
*/ |
491 |
dl |
1.5 |
public void testShutDownNow() { |
492 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
493 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
494 |
dl |
1.8 |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
495 |
dl |
1.5 |
List l = p1.shutdownNow(); |
496 |
|
|
assertTrue(p1.isShutdown()); |
497 |
dl |
1.2 |
assertTrue(l.size() > 0 && l.size() <= 5); |
498 |
dl |
1.5 |
joinPool(p1); |
499 |
dl |
1.2 |
} |
500 |
|
|
|
501 |
dl |
1.5 |
/** |
502 |
dl |
1.6 |
* In default setting, shutdown cancels periodic but not delayed |
503 |
|
|
* tasks at shutdown |
504 |
dl |
1.5 |
*/ |
505 |
|
|
public void testShutDown1() { |
506 |
dl |
1.2 |
try { |
507 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
508 |
dl |
1.5 |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
509 |
|
|
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
510 |
dl |
1.2 |
|
511 |
dl |
1.9 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
512 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
513 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
514 |
|
|
p1.shutdown(); |
515 |
|
|
BlockingQueue q = p1.getQueue(); |
516 |
dl |
1.2 |
for (Iterator it = q.iterator(); it.hasNext();) { |
517 |
dl |
1.9 |
ScheduledFuture t = (ScheduledFuture)it.next(); |
518 |
dl |
1.2 |
assertFalse(t.isCancelled()); |
519 |
|
|
} |
520 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
521 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
522 |
dl |
1.2 |
for (int i = 0; i < 5; ++i) { |
523 |
|
|
assertTrue(tasks[i].isDone()); |
524 |
|
|
assertFalse(tasks[i].isCancelled()); |
525 |
|
|
} |
526 |
|
|
|
527 |
|
|
} |
528 |
|
|
catch(Exception ex) { |
529 |
dl |
1.5 |
unexpectedException(); |
530 |
dl |
1.2 |
} |
531 |
|
|
} |
532 |
|
|
|
533 |
|
|
|
534 |
dl |
1.5 |
/** |
535 |
dl |
1.6 |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
536 |
|
|
* delayed tasks are cancelled at shutdown |
537 |
dl |
1.5 |
*/ |
538 |
|
|
public void testShutDown2() { |
539 |
dl |
1.2 |
try { |
540 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
541 |
dl |
1.5 |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
542 |
dl |
1.9 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
543 |
dl |
1.2 |
for(int i = 0; i < 5; i++) |
544 |
dl |
1.5 |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
545 |
|
|
p1.shutdown(); |
546 |
|
|
assertTrue(p1.isShutdown()); |
547 |
|
|
BlockingQueue q = p1.getQueue(); |
548 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
549 |
dl |
1.4 |
Thread.sleep(SMALL_DELAY_MS); |
550 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
551 |
dl |
1.2 |
} |
552 |
|
|
catch(Exception ex) { |
553 |
dl |
1.5 |
unexpectedException(); |
554 |
dl |
1.2 |
} |
555 |
|
|
} |
556 |
|
|
|
557 |
|
|
|
558 |
dl |
1.5 |
/** |
559 |
dl |
1.6 |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
560 |
|
|
* periodic tasks are not cancelled at shutdown |
561 |
dl |
1.5 |
*/ |
562 |
|
|
public void testShutDown3() { |
563 |
dl |
1.2 |
try { |
564 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
565 |
dl |
1.5 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
566 |
dl |
1.9 |
ScheduledFuture task = |
567 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
568 |
|
|
p1.shutdown(); |
569 |
|
|
assertTrue(p1.isShutdown()); |
570 |
|
|
BlockingQueue q = p1.getQueue(); |
571 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
572 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
573 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
574 |
dl |
1.2 |
} |
575 |
|
|
catch(Exception ex) { |
576 |
dl |
1.5 |
unexpectedException(); |
577 |
dl |
1.2 |
} |
578 |
|
|
} |
579 |
|
|
|
580 |
dl |
1.5 |
/** |
581 |
dl |
1.6 |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
582 |
|
|
* periodic tasks are cancelled at shutdown |
583 |
dl |
1.5 |
*/ |
584 |
|
|
public void testShutDown4() { |
585 |
dl |
1.9 |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
586 |
dl |
1.2 |
try { |
587 |
dl |
1.5 |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
588 |
dl |
1.9 |
ScheduledFuture task = |
589 |
dl |
1.5 |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
590 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
591 |
dl |
1.5 |
p1.shutdown(); |
592 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
593 |
dl |
1.5 |
assertFalse(p1.isTerminated()); |
594 |
|
|
assertTrue(p1.isShutdown()); |
595 |
dl |
1.2 |
Thread.sleep(SHORT_DELAY_MS); |
596 |
|
|
assertFalse(task.isCancelled()); |
597 |
|
|
task.cancel(true); |
598 |
|
|
assertTrue(task.isCancelled()); |
599 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
600 |
dl |
1.5 |
assertTrue(p1.isTerminated()); |
601 |
dl |
1.2 |
} |
602 |
|
|
catch(Exception ex) { |
603 |
dl |
1.5 |
unexpectedException(); |
604 |
dl |
1.2 |
} |
605 |
|
|
finally { |
606 |
dl |
1.5 |
p1.shutdownNow(); |
607 |
dl |
1.2 |
} |
608 |
dl |
1.1 |
} |
609 |
|
|
|
610 |
dl |
1.10 |
/** |
611 |
|
|
* completed submit of callable returns result |
612 |
|
|
*/ |
613 |
|
|
public void testSubmitCallable() { |
614 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
615 |
|
|
try { |
616 |
|
|
Future<String> future = e.submit(new StringTask()); |
617 |
|
|
String result = future.get(); |
618 |
|
|
assertSame(TEST_STRING, result); |
619 |
|
|
} |
620 |
|
|
catch (ExecutionException ex) { |
621 |
|
|
unexpectedException(); |
622 |
|
|
} |
623 |
|
|
catch (InterruptedException ex) { |
624 |
|
|
unexpectedException(); |
625 |
|
|
} finally { |
626 |
|
|
joinPool(e); |
627 |
|
|
} |
628 |
|
|
} |
629 |
|
|
|
630 |
|
|
/** |
631 |
|
|
* completed submit of runnable returns successfully |
632 |
|
|
*/ |
633 |
|
|
public void testSubmitRunnable() { |
634 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
635 |
|
|
try { |
636 |
|
|
Future<?> future = e.submit(new NoOpRunnable()); |
637 |
|
|
future.get(); |
638 |
|
|
assertTrue(future.isDone()); |
639 |
|
|
} |
640 |
|
|
catch (ExecutionException ex) { |
641 |
|
|
unexpectedException(); |
642 |
|
|
} |
643 |
|
|
catch (InterruptedException ex) { |
644 |
|
|
unexpectedException(); |
645 |
|
|
} finally { |
646 |
|
|
joinPool(e); |
647 |
|
|
} |
648 |
|
|
} |
649 |
|
|
|
650 |
|
|
/** |
651 |
|
|
* completed submit of (runnable, result) returns result |
652 |
|
|
*/ |
653 |
|
|
public void testSubmitRunnable2() { |
654 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
655 |
|
|
try { |
656 |
|
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
657 |
|
|
String result = future.get(); |
658 |
|
|
assertSame(TEST_STRING, result); |
659 |
|
|
} |
660 |
|
|
catch (ExecutionException ex) { |
661 |
|
|
unexpectedException(); |
662 |
|
|
} |
663 |
|
|
catch (InterruptedException ex) { |
664 |
|
|
unexpectedException(); |
665 |
|
|
} finally { |
666 |
|
|
joinPool(e); |
667 |
|
|
} |
668 |
|
|
} |
669 |
|
|
|
670 |
|
|
|
671 |
|
|
|
672 |
|
|
|
673 |
|
|
|
674 |
|
|
/** |
675 |
|
|
* invokeAny(null) throws NPE |
676 |
|
|
*/ |
677 |
|
|
public void testInvokeAny1() { |
678 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
679 |
|
|
try { |
680 |
|
|
e.invokeAny(null); |
681 |
|
|
} catch (NullPointerException success) { |
682 |
|
|
} catch(Exception ex) { |
683 |
|
|
unexpectedException(); |
684 |
|
|
} finally { |
685 |
|
|
joinPool(e); |
686 |
|
|
} |
687 |
|
|
} |
688 |
|
|
|
689 |
|
|
/** |
690 |
|
|
* invokeAny(empty collection) throws IAE |
691 |
|
|
*/ |
692 |
|
|
public void testInvokeAny2() { |
693 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
694 |
|
|
try { |
695 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
696 |
|
|
} catch (IllegalArgumentException success) { |
697 |
|
|
} catch(Exception ex) { |
698 |
|
|
unexpectedException(); |
699 |
|
|
} finally { |
700 |
|
|
joinPool(e); |
701 |
|
|
} |
702 |
|
|
} |
703 |
|
|
|
704 |
|
|
/** |
705 |
|
|
* invokeAny(c) throws NPE if c has null elements |
706 |
|
|
*/ |
707 |
|
|
public void testInvokeAny3() { |
708 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
709 |
|
|
try { |
710 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
711 |
|
|
l.add(new StringTask()); |
712 |
|
|
l.add(null); |
713 |
|
|
e.invokeAny(l); |
714 |
|
|
} catch (NullPointerException success) { |
715 |
|
|
} catch(Exception ex) { |
716 |
|
|
unexpectedException(); |
717 |
|
|
} finally { |
718 |
|
|
joinPool(e); |
719 |
|
|
} |
720 |
|
|
} |
721 |
|
|
|
722 |
|
|
/** |
723 |
|
|
* invokeAny(c) throws ExecutionException if no task completes |
724 |
|
|
*/ |
725 |
|
|
public void testInvokeAny4() { |
726 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
727 |
|
|
try { |
728 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
729 |
|
|
l.add(new NPETask()); |
730 |
|
|
e.invokeAny(l); |
731 |
|
|
} catch (ExecutionException success) { |
732 |
|
|
} catch(Exception ex) { |
733 |
|
|
unexpectedException(); |
734 |
|
|
} finally { |
735 |
|
|
joinPool(e); |
736 |
|
|
} |
737 |
|
|
} |
738 |
|
|
|
739 |
|
|
/** |
740 |
|
|
* invokeAny(c) returns result of some task |
741 |
|
|
*/ |
742 |
|
|
public void testInvokeAny5() { |
743 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
744 |
|
|
try { |
745 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
746 |
|
|
l.add(new StringTask()); |
747 |
|
|
l.add(new StringTask()); |
748 |
|
|
String result = e.invokeAny(l); |
749 |
|
|
assertSame(TEST_STRING, result); |
750 |
|
|
} catch (ExecutionException success) { |
751 |
|
|
} catch(Exception ex) { |
752 |
|
|
unexpectedException(); |
753 |
|
|
} finally { |
754 |
|
|
joinPool(e); |
755 |
|
|
} |
756 |
|
|
} |
757 |
|
|
|
758 |
|
|
/** |
759 |
|
|
* invokeAll(null) throws NPE |
760 |
|
|
*/ |
761 |
|
|
public void testInvokeAll1() { |
762 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
763 |
|
|
try { |
764 |
|
|
e.invokeAll(null); |
765 |
|
|
} catch (NullPointerException success) { |
766 |
|
|
} catch(Exception ex) { |
767 |
|
|
unexpectedException(); |
768 |
|
|
} finally { |
769 |
|
|
joinPool(e); |
770 |
|
|
} |
771 |
|
|
} |
772 |
|
|
|
773 |
|
|
/** |
774 |
|
|
* invokeAll(empty collection) returns empty collection |
775 |
|
|
*/ |
776 |
|
|
public void testInvokeAll2() { |
777 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
778 |
|
|
try { |
779 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
780 |
|
|
assertTrue(r.isEmpty()); |
781 |
|
|
} catch(Exception ex) { |
782 |
|
|
unexpectedException(); |
783 |
|
|
} finally { |
784 |
|
|
joinPool(e); |
785 |
|
|
} |
786 |
|
|
} |
787 |
|
|
|
788 |
|
|
/** |
789 |
|
|
* invokeAll(c) throws NPE if c has null elements |
790 |
|
|
*/ |
791 |
|
|
public void testInvokeAll3() { |
792 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
793 |
|
|
try { |
794 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
795 |
|
|
l.add(new StringTask()); |
796 |
|
|
l.add(null); |
797 |
|
|
e.invokeAll(l); |
798 |
|
|
} catch (NullPointerException success) { |
799 |
|
|
} catch(Exception ex) { |
800 |
|
|
unexpectedException(); |
801 |
|
|
} finally { |
802 |
|
|
joinPool(e); |
803 |
|
|
} |
804 |
|
|
} |
805 |
|
|
|
806 |
|
|
/** |
807 |
|
|
* get of invokeAll(c) throws exception on failed task |
808 |
|
|
*/ |
809 |
|
|
public void testInvokeAll4() { |
810 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
811 |
|
|
try { |
812 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
813 |
|
|
l.add(new NPETask()); |
814 |
|
|
List<Future<String>> result = e.invokeAll(l); |
815 |
|
|
assertEquals(1, result.size()); |
816 |
|
|
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
817 |
|
|
it.next().get(); |
818 |
|
|
} catch(ExecutionException success) { |
819 |
|
|
} catch(Exception ex) { |
820 |
|
|
unexpectedException(); |
821 |
|
|
} finally { |
822 |
|
|
joinPool(e); |
823 |
|
|
} |
824 |
|
|
} |
825 |
|
|
|
826 |
|
|
/** |
827 |
|
|
* invokeAll(c) returns results of all completed tasks |
828 |
|
|
*/ |
829 |
|
|
public void testInvokeAll5() { |
830 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
831 |
|
|
try { |
832 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
833 |
|
|
l.add(new StringTask()); |
834 |
|
|
l.add(new StringTask()); |
835 |
|
|
List<Future<String>> result = e.invokeAll(l); |
836 |
|
|
assertEquals(2, result.size()); |
837 |
|
|
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
838 |
|
|
assertSame(TEST_STRING, it.next().get()); |
839 |
|
|
} catch (ExecutionException success) { |
840 |
|
|
} catch(Exception ex) { |
841 |
|
|
unexpectedException(); |
842 |
|
|
} finally { |
843 |
|
|
joinPool(e); |
844 |
|
|
} |
845 |
|
|
} |
846 |
|
|
|
847 |
dl |
1.11 |
/** |
848 |
|
|
* timed invokeAny(null) throws NPE |
849 |
|
|
*/ |
850 |
|
|
public void testTimedInvokeAny1() { |
851 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
852 |
|
|
try { |
853 |
|
|
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
854 |
|
|
} catch (NullPointerException success) { |
855 |
|
|
} catch(Exception ex) { |
856 |
|
|
unexpectedException(); |
857 |
|
|
} finally { |
858 |
|
|
joinPool(e); |
859 |
|
|
} |
860 |
|
|
} |
861 |
|
|
|
862 |
|
|
/** |
863 |
|
|
* timed invokeAny(,,null) throws NPE |
864 |
|
|
*/ |
865 |
|
|
public void testTimedInvokeAnyNullTimeUnit() { |
866 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
867 |
|
|
try { |
868 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
869 |
|
|
l.add(new StringTask()); |
870 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
871 |
|
|
} catch (NullPointerException success) { |
872 |
|
|
} catch(Exception ex) { |
873 |
|
|
unexpectedException(); |
874 |
|
|
} finally { |
875 |
|
|
joinPool(e); |
876 |
|
|
} |
877 |
|
|
} |
878 |
|
|
|
879 |
|
|
/** |
880 |
|
|
* timed invokeAny(empty collection) throws IAE |
881 |
|
|
*/ |
882 |
|
|
public void testTimedInvokeAny2() { |
883 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
884 |
|
|
try { |
885 |
|
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
886 |
|
|
} catch (IllegalArgumentException success) { |
887 |
|
|
} catch(Exception ex) { |
888 |
|
|
unexpectedException(); |
889 |
|
|
} finally { |
890 |
|
|
joinPool(e); |
891 |
|
|
} |
892 |
|
|
} |
893 |
|
|
|
894 |
|
|
/** |
895 |
|
|
* timed invokeAny(c) throws NPE if c has null elements |
896 |
|
|
*/ |
897 |
|
|
public void testTimedInvokeAny3() { |
898 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
899 |
|
|
try { |
900 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
901 |
|
|
l.add(new StringTask()); |
902 |
|
|
l.add(null); |
903 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
904 |
|
|
} catch (NullPointerException success) { |
905 |
|
|
} catch(Exception ex) { |
906 |
|
|
ex.printStackTrace(); |
907 |
|
|
unexpectedException(); |
908 |
|
|
} finally { |
909 |
|
|
joinPool(e); |
910 |
|
|
} |
911 |
|
|
} |
912 |
|
|
|
913 |
|
|
/** |
914 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
915 |
|
|
*/ |
916 |
|
|
public void testTimedInvokeAny4() { |
917 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
918 |
|
|
try { |
919 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
920 |
|
|
l.add(new NPETask()); |
921 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
922 |
|
|
} catch(ExecutionException success) { |
923 |
|
|
} catch(Exception ex) { |
924 |
|
|
unexpectedException(); |
925 |
|
|
} finally { |
926 |
|
|
joinPool(e); |
927 |
|
|
} |
928 |
|
|
} |
929 |
|
|
|
930 |
|
|
/** |
931 |
|
|
* timed invokeAny(c) returns result of some task |
932 |
|
|
*/ |
933 |
|
|
public void testTimedInvokeAny5() { |
934 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
935 |
|
|
try { |
936 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
937 |
|
|
l.add(new StringTask()); |
938 |
|
|
l.add(new StringTask()); |
939 |
|
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
940 |
|
|
assertSame(TEST_STRING, result); |
941 |
|
|
} catch (ExecutionException success) { |
942 |
|
|
} catch(Exception ex) { |
943 |
|
|
unexpectedException(); |
944 |
|
|
} finally { |
945 |
|
|
joinPool(e); |
946 |
|
|
} |
947 |
|
|
} |
948 |
|
|
|
949 |
|
|
/** |
950 |
|
|
* timed invokeAll(null) throws NPE |
951 |
|
|
*/ |
952 |
|
|
public void testTimedInvokeAll1() { |
953 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
954 |
|
|
try { |
955 |
|
|
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
956 |
|
|
} catch (NullPointerException success) { |
957 |
|
|
} catch(Exception ex) { |
958 |
|
|
unexpectedException(); |
959 |
|
|
} finally { |
960 |
|
|
joinPool(e); |
961 |
|
|
} |
962 |
|
|
} |
963 |
|
|
|
964 |
|
|
/** |
965 |
|
|
* timed invokeAll(,,null) throws NPE |
966 |
|
|
*/ |
967 |
|
|
public void testTimedInvokeAllNullTimeUnit() { |
968 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
969 |
|
|
try { |
970 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
971 |
|
|
l.add(new StringTask()); |
972 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
973 |
|
|
} catch (NullPointerException success) { |
974 |
|
|
} catch(Exception ex) { |
975 |
|
|
unexpectedException(); |
976 |
|
|
} finally { |
977 |
|
|
joinPool(e); |
978 |
|
|
} |
979 |
|
|
} |
980 |
|
|
|
981 |
|
|
/** |
982 |
|
|
* timed invokeAll(empty collection) returns empty collection |
983 |
|
|
*/ |
984 |
|
|
public void testTimedInvokeAll2() { |
985 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
986 |
|
|
try { |
987 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
988 |
|
|
assertTrue(r.isEmpty()); |
989 |
|
|
} catch(Exception ex) { |
990 |
|
|
unexpectedException(); |
991 |
|
|
} finally { |
992 |
|
|
joinPool(e); |
993 |
|
|
} |
994 |
|
|
} |
995 |
|
|
|
996 |
|
|
/** |
997 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
998 |
|
|
*/ |
999 |
|
|
public void testTimedInvokeAll3() { |
1000 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1001 |
|
|
try { |
1002 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1003 |
|
|
l.add(new StringTask()); |
1004 |
|
|
l.add(null); |
1005 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1006 |
|
|
} catch (NullPointerException success) { |
1007 |
|
|
} catch(Exception ex) { |
1008 |
|
|
unexpectedException(); |
1009 |
|
|
} finally { |
1010 |
|
|
joinPool(e); |
1011 |
|
|
} |
1012 |
|
|
} |
1013 |
|
|
|
1014 |
|
|
/** |
1015 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1016 |
|
|
*/ |
1017 |
|
|
public void testTimedInvokeAll4() { |
1018 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1019 |
|
|
try { |
1020 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1021 |
|
|
l.add(new NPETask()); |
1022 |
|
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1023 |
|
|
assertEquals(1, result.size()); |
1024 |
|
|
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1025 |
|
|
it.next().get(); |
1026 |
|
|
} catch(ExecutionException success) { |
1027 |
|
|
} catch(Exception ex) { |
1028 |
|
|
unexpectedException(); |
1029 |
|
|
} finally { |
1030 |
|
|
joinPool(e); |
1031 |
|
|
} |
1032 |
|
|
} |
1033 |
|
|
|
1034 |
|
|
/** |
1035 |
|
|
* timed invokeAll(c) returns results of all completed tasks |
1036 |
|
|
*/ |
1037 |
|
|
public void testTimedInvokeAll5() { |
1038 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1039 |
|
|
try { |
1040 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1041 |
|
|
l.add(new StringTask()); |
1042 |
|
|
l.add(new StringTask()); |
1043 |
|
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1044 |
|
|
assertEquals(2, result.size()); |
1045 |
|
|
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1046 |
|
|
assertSame(TEST_STRING, it.next().get()); |
1047 |
|
|
} catch (ExecutionException success) { |
1048 |
|
|
} catch(Exception ex) { |
1049 |
|
|
unexpectedException(); |
1050 |
|
|
} finally { |
1051 |
|
|
joinPool(e); |
1052 |
|
|
} |
1053 |
|
|
} |
1054 |
|
|
|
1055 |
|
|
/** |
1056 |
|
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1057 |
|
|
*/ |
1058 |
|
|
public void testTimedInvokeAll6() { |
1059 |
|
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1060 |
|
|
try { |
1061 |
|
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1062 |
|
|
l.add(new StringTask()); |
1063 |
dl |
1.12 |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1064 |
dl |
1.11 |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1065 |
|
|
assertEquals(2, result.size()); |
1066 |
|
|
Iterator<Future<String>> it = result.iterator(); |
1067 |
|
|
Future<String> f1 = it.next(); |
1068 |
|
|
Future<String> f2 = it.next(); |
1069 |
|
|
assertTrue(f1.isDone()); |
1070 |
|
|
assertFalse(f1.isCancelled()); |
1071 |
|
|
assertTrue(f2.isDone()); |
1072 |
|
|
assertTrue(f2.isCancelled()); |
1073 |
|
|
} catch(Exception ex) { |
1074 |
|
|
unexpectedException(); |
1075 |
|
|
} finally { |
1076 |
|
|
joinPool(e); |
1077 |
|
|
} |
1078 |
|
|
} |
1079 |
|
|
|
1080 |
dl |
1.10 |
|
1081 |
dl |
1.1 |
} |