1 |
/* |
2 |
* 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 |
*/ |
8 |
|
9 |
|
10 |
import junit.framework.*; |
11 |
import java.util.*; |
12 |
import java.util.concurrent.*; |
13 |
import java.math.BigInteger; |
14 |
import java.security.*; |
15 |
|
16 |
public class ExecutorsTest extends JSR166TestCase{ |
17 |
public static void main(String[] args) { |
18 |
junit.textui.TestRunner.run (suite()); |
19 |
} |
20 |
public static Test suite() { |
21 |
return new TestSuite(ExecutorsTest.class); |
22 |
} |
23 |
|
24 |
static class TimedCallable<T> implements Callable<T> { |
25 |
private final ExecutorService exec; |
26 |
private final Callable<T> func; |
27 |
private final long msecs; |
28 |
|
29 |
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
30 |
this.exec = exec; |
31 |
this.func = func; |
32 |
this.msecs = msecs; |
33 |
} |
34 |
|
35 |
public T call() throws Exception { |
36 |
Future<T> ftask = exec.submit(func); |
37 |
try { |
38 |
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
39 |
} finally { |
40 |
ftask.cancel(true); |
41 |
} |
42 |
} |
43 |
} |
44 |
|
45 |
|
46 |
private static class Fib implements Callable<BigInteger> { |
47 |
private final BigInteger n; |
48 |
Fib(long n) { |
49 |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
50 |
this.n = BigInteger.valueOf(n); |
51 |
} |
52 |
public BigInteger call() { |
53 |
BigInteger f1 = BigInteger.ONE; |
54 |
BigInteger f2 = f1; |
55 |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
56 |
BigInteger t = f1.add(f2); |
57 |
f1 = f2; |
58 |
f2 = t; |
59 |
} |
60 |
return f1; |
61 |
} |
62 |
}; |
63 |
|
64 |
/** |
65 |
* A newCachedThreadPool can execute runnables |
66 |
*/ |
67 |
public void testNewCachedThreadPool1() { |
68 |
ExecutorService e = Executors.newCachedThreadPool(); |
69 |
e.execute(new NoOpRunnable()); |
70 |
e.execute(new NoOpRunnable()); |
71 |
e.execute(new NoOpRunnable()); |
72 |
e.shutdown(); |
73 |
} |
74 |
|
75 |
/** |
76 |
* A newCachedThreadPool with given ThreadFactory can execute runnables |
77 |
*/ |
78 |
public void testNewCachedThreadPool2() { |
79 |
ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); |
80 |
e.execute(new NoOpRunnable()); |
81 |
e.execute(new NoOpRunnable()); |
82 |
e.execute(new NoOpRunnable()); |
83 |
e.shutdown(); |
84 |
} |
85 |
|
86 |
/** |
87 |
* A newCachedThreadPool with null ThreadFactory throws NPE |
88 |
*/ |
89 |
public void testNewCachedThreadPool3() { |
90 |
try { |
91 |
ExecutorService e = Executors.newCachedThreadPool(null); |
92 |
shouldThrow(); |
93 |
} |
94 |
catch(NullPointerException success) { |
95 |
} |
96 |
} |
97 |
|
98 |
|
99 |
/** |
100 |
* A new SingleThreadExecutor can execute runnables |
101 |
*/ |
102 |
public void testNewSingleThreadExecutor1() { |
103 |
ExecutorService e = Executors.newSingleThreadExecutor(); |
104 |
e.execute(new NoOpRunnable()); |
105 |
e.execute(new NoOpRunnable()); |
106 |
e.execute(new NoOpRunnable()); |
107 |
e.shutdown(); |
108 |
} |
109 |
|
110 |
/** |
111 |
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
112 |
*/ |
113 |
public void testNewSingleThreadExecutor2() { |
114 |
ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); |
115 |
e.execute(new NoOpRunnable()); |
116 |
e.execute(new NoOpRunnable()); |
117 |
e.execute(new NoOpRunnable()); |
118 |
e.shutdown(); |
119 |
} |
120 |
|
121 |
/** |
122 |
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
123 |
*/ |
124 |
public void testNewSingleThreadExecutor3() { |
125 |
try { |
126 |
ExecutorService e = Executors.newSingleThreadExecutor(null); |
127 |
shouldThrow(); |
128 |
} |
129 |
catch(NullPointerException success) { |
130 |
} |
131 |
} |
132 |
|
133 |
/** |
134 |
* A new SingleThreadExecutor cannot be casted to concrete implementation |
135 |
*/ |
136 |
public void testCastNewSingleThreadExecutor() { |
137 |
ExecutorService e = Executors.newSingleThreadExecutor(); |
138 |
try { |
139 |
ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; |
140 |
} catch (ClassCastException success) { |
141 |
} finally { |
142 |
joinPool(e); |
143 |
} |
144 |
} |
145 |
|
146 |
|
147 |
/** |
148 |
* A new newFixedThreadPool can execute runnables |
149 |
*/ |
150 |
public void testNewFixedThreadPool1() { |
151 |
ExecutorService e = Executors.newFixedThreadPool(2); |
152 |
e.execute(new NoOpRunnable()); |
153 |
e.execute(new NoOpRunnable()); |
154 |
e.execute(new NoOpRunnable()); |
155 |
e.shutdown(); |
156 |
} |
157 |
|
158 |
/** |
159 |
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
160 |
*/ |
161 |
public void testNewFixedThreadPool2() { |
162 |
ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); |
163 |
e.execute(new NoOpRunnable()); |
164 |
e.execute(new NoOpRunnable()); |
165 |
e.execute(new NoOpRunnable()); |
166 |
e.shutdown(); |
167 |
} |
168 |
|
169 |
/** |
170 |
* A new newFixedThreadPool with null ThreadFactory throws NPE |
171 |
*/ |
172 |
public void testNewFixedThreadPool3() { |
173 |
try { |
174 |
ExecutorService e = Executors.newFixedThreadPool(2, null); |
175 |
shouldThrow(); |
176 |
} |
177 |
catch(NullPointerException success) { |
178 |
} |
179 |
} |
180 |
|
181 |
/** |
182 |
* A new newFixedThreadPool with 0 threads throws IAE |
183 |
*/ |
184 |
public void testNewFixedThreadPool4() { |
185 |
try { |
186 |
ExecutorService e = Executors.newFixedThreadPool(0); |
187 |
shouldThrow(); |
188 |
} |
189 |
catch(IllegalArgumentException success) { |
190 |
} |
191 |
} |
192 |
|
193 |
|
194 |
/** |
195 |
* An unconfigurable newFixedThreadPool can execute runnables |
196 |
*/ |
197 |
public void testunconfigurableExecutorService() { |
198 |
ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); |
199 |
e.execute(new NoOpRunnable()); |
200 |
e.execute(new NoOpRunnable()); |
201 |
e.execute(new NoOpRunnable()); |
202 |
e.shutdown(); |
203 |
} |
204 |
|
205 |
/** |
206 |
* unconfigurableExecutorService(null) throws NPE |
207 |
*/ |
208 |
public void testunconfigurableExecutorServiceNPE() { |
209 |
try { |
210 |
ExecutorService e = Executors.unconfigurableExecutorService(null); |
211 |
} |
212 |
catch (NullPointerException success) { |
213 |
} |
214 |
} |
215 |
|
216 |
/** |
217 |
* unconfigurableScheduledExecutorService(null) throws NPE |
218 |
*/ |
219 |
public void testunconfigurableScheduledExecutorServiceNPE() { |
220 |
try { |
221 |
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
222 |
} |
223 |
catch (NullPointerException success) { |
224 |
} |
225 |
} |
226 |
|
227 |
|
228 |
/** |
229 |
* a newSingleThreadScheduledExecutor successfully runs delayed task |
230 |
*/ |
231 |
public void testNewSingleThreadScheduledExecutor() { |
232 |
try { |
233 |
TrackedCallable callable = new TrackedCallable(); |
234 |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
235 |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
236 |
assertFalse(callable.done); |
237 |
Thread.sleep(MEDIUM_DELAY_MS); |
238 |
assertTrue(callable.done); |
239 |
assertEquals(Boolean.TRUE, f.get()); |
240 |
p1.shutdown(); |
241 |
joinPool(p1); |
242 |
} catch(RejectedExecutionException e){} |
243 |
catch(Exception e){ |
244 |
e.printStackTrace(); |
245 |
unexpectedException(); |
246 |
} |
247 |
} |
248 |
|
249 |
/** |
250 |
* a newScheduledThreadPool successfully runs delayed task |
251 |
*/ |
252 |
public void testnewScheduledThreadPool() { |
253 |
try { |
254 |
TrackedCallable callable = new TrackedCallable(); |
255 |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
256 |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
257 |
assertFalse(callable.done); |
258 |
Thread.sleep(MEDIUM_DELAY_MS); |
259 |
assertTrue(callable.done); |
260 |
assertEquals(Boolean.TRUE, f.get()); |
261 |
p1.shutdown(); |
262 |
joinPool(p1); |
263 |
} catch(RejectedExecutionException e){} |
264 |
catch(Exception e){ |
265 |
e.printStackTrace(); |
266 |
unexpectedException(); |
267 |
} |
268 |
} |
269 |
|
270 |
/** |
271 |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
272 |
*/ |
273 |
public void testunconfigurableScheduledExecutorService() { |
274 |
try { |
275 |
TrackedCallable callable = new TrackedCallable(); |
276 |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
277 |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
278 |
assertFalse(callable.done); |
279 |
Thread.sleep(MEDIUM_DELAY_MS); |
280 |
assertTrue(callable.done); |
281 |
assertEquals(Boolean.TRUE, f.get()); |
282 |
p1.shutdown(); |
283 |
joinPool(p1); |
284 |
} catch(RejectedExecutionException e){} |
285 |
catch(Exception e){ |
286 |
e.printStackTrace(); |
287 |
unexpectedException(); |
288 |
} |
289 |
} |
290 |
|
291 |
/** |
292 |
* timeouts from execute will time out if they compute too long. |
293 |
*/ |
294 |
public void testTimedCallable() { |
295 |
int N = 10000; |
296 |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
297 |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
298 |
try { |
299 |
long startTime = System.currentTimeMillis(); |
300 |
|
301 |
long i = 0; |
302 |
while (tasks.size() < N) { |
303 |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
304 |
i += 10; |
305 |
} |
306 |
|
307 |
int iters = 0; |
308 |
BigInteger sum = BigInteger.ZERO; |
309 |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
310 |
try { |
311 |
++iters; |
312 |
sum = sum.add(it.next().call()); |
313 |
} |
314 |
catch (TimeoutException success) { |
315 |
assertTrue(iters > 0); |
316 |
return; |
317 |
} |
318 |
catch (Exception e) { |
319 |
unexpectedException(); |
320 |
} |
321 |
} |
322 |
// if by chance we didn't ever time out, total time must be small |
323 |
long elapsed = System.currentTimeMillis() - startTime; |
324 |
assertTrue(elapsed < N); |
325 |
} |
326 |
finally { |
327 |
joinPool(executor); |
328 |
} |
329 |
} |
330 |
|
331 |
|
332 |
/** |
333 |
* ThreadPoolExecutor using defaultThreadFactory has |
334 |
* specified group, priority, daemon status, and name |
335 |
*/ |
336 |
public void testDefaultThreadFactory() { |
337 |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
338 |
Runnable r = new Runnable() { |
339 |
public void run() { |
340 |
try { |
341 |
Thread current = Thread.currentThread(); |
342 |
threadAssertTrue(!current.isDaemon()); |
343 |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
344 |
ThreadGroup g = current.getThreadGroup(); |
345 |
SecurityManager s = System.getSecurityManager(); |
346 |
if (s != null) |
347 |
threadAssertTrue(g == s.getThreadGroup()); |
348 |
else |
349 |
threadAssertTrue(g == egroup); |
350 |
String name = current.getName(); |
351 |
threadAssertTrue(name.endsWith("thread-1")); |
352 |
} catch (SecurityException ok) { |
353 |
// Also pass if not allowed to change setting |
354 |
} |
355 |
} |
356 |
}; |
357 |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
358 |
|
359 |
e.execute(r); |
360 |
e.shutdown(); |
361 |
try { |
362 |
Thread.sleep(SHORT_DELAY_MS); |
363 |
} catch (Exception eX) { |
364 |
unexpectedException(); |
365 |
} finally { |
366 |
joinPool(e); |
367 |
} |
368 |
} |
369 |
|
370 |
/** |
371 |
* ThreadPoolExecutor using privilegedThreadFactory has |
372 |
* specified group, priority, daemon status, name, |
373 |
* access control context and context class loader |
374 |
*/ |
375 |
public void testPrivilegedThreadFactory() { |
376 |
Policy savedPolicy = Policy.getPolicy(); |
377 |
AdjustablePolicy policy = new AdjustablePolicy(); |
378 |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
379 |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
380 |
Policy.setPolicy(policy); |
381 |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
382 |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
383 |
final AccessControlContext thisacc = AccessController.getContext(); |
384 |
Runnable r = new Runnable() { |
385 |
public void run() { |
386 |
try { |
387 |
Thread current = Thread.currentThread(); |
388 |
threadAssertTrue(!current.isDaemon()); |
389 |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
390 |
ThreadGroup g = current.getThreadGroup(); |
391 |
SecurityManager s = System.getSecurityManager(); |
392 |
if (s != null) |
393 |
threadAssertTrue(g == s.getThreadGroup()); |
394 |
else |
395 |
threadAssertTrue(g == egroup); |
396 |
String name = current.getName(); |
397 |
threadAssertTrue(name.endsWith("thread-1")); |
398 |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
399 |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
400 |
} catch(SecurityException ok) { |
401 |
// Also pass if not allowed to change settings |
402 |
} |
403 |
}; |
404 |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
405 |
|
406 |
Policy.setPolicy(savedPolicy); |
407 |
e.execute(r); |
408 |
e.shutdown(); |
409 |
try { |
410 |
Thread.sleep(SHORT_DELAY_MS); |
411 |
} catch (Exception ex) { |
412 |
unexpectedException(); |
413 |
} finally { |
414 |
joinPool(e); |
415 |
} |
416 |
|
417 |
} |
418 |
|
419 |
static class CheckCCL implements Callable<Object> { |
420 |
public Object call() { |
421 |
AccessControlContext acc = AccessController.getContext(); |
422 |
acc.checkPermission(new RuntimePermission("getContextClassLoader")); |
423 |
return null; |
424 |
} |
425 |
} |
426 |
|
427 |
|
428 |
/** |
429 |
* Without class loader permissions, creating |
430 |
* privilegedCallableUsingCurrentClassLoader throws ACE |
431 |
*/ |
432 |
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
433 |
Policy savedPolicy = Policy.getPolicy(); |
434 |
AdjustablePolicy policy = new AdjustablePolicy(); |
435 |
Policy.setPolicy(policy); |
436 |
try { |
437 |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
438 |
shouldThrow(); |
439 |
} catch(AccessControlException success) { |
440 |
} catch(Exception ex) { |
441 |
unexpectedException(); |
442 |
} |
443 |
finally { |
444 |
Policy.setPolicy(savedPolicy); |
445 |
} |
446 |
} |
447 |
|
448 |
/** |
449 |
* Without class loader permissions, calling |
450 |
* privilegedCallableUsingCurrentClassLoader throws ACE |
451 |
*/ |
452 |
public void testprivilegedCallableUsingCCLWithPrivs() { |
453 |
Policy savedPolicy = Policy.getPolicy(); |
454 |
AdjustablePolicy policy = new AdjustablePolicy(); |
455 |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
456 |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
457 |
Policy.setPolicy(policy); |
458 |
try { |
459 |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
460 |
task.call(); |
461 |
} catch(Exception ex) { |
462 |
unexpectedException(); |
463 |
} |
464 |
finally { |
465 |
Policy.setPolicy(savedPolicy); |
466 |
} |
467 |
} |
468 |
|
469 |
/** |
470 |
* Without permissions, calling privilegedCallable throws ACE |
471 |
*/ |
472 |
public void testprivilegedCallableWithNoPrivs() { |
473 |
Policy savedPolicy = Policy.getPolicy(); |
474 |
AdjustablePolicy policy = new AdjustablePolicy(); |
475 |
Policy.setPolicy(policy); |
476 |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
477 |
Policy.setPolicy(savedPolicy); |
478 |
try { |
479 |
task.call(); |
480 |
shouldThrow(); |
481 |
} catch(AccessControlException success) { |
482 |
} catch(Exception ex) { |
483 |
unexpectedException(); |
484 |
} finally { |
485 |
} |
486 |
} |
487 |
|
488 |
/** |
489 |
* With permissions, calling privilegedCallable succeeds |
490 |
*/ |
491 |
public void testprivilegedCallableWithPrivs() { |
492 |
Policy savedPolicy = Policy.getPolicy(); |
493 |
AdjustablePolicy policy = new AdjustablePolicy(); |
494 |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
495 |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
496 |
Policy.setPolicy(policy); |
497 |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
498 |
try { |
499 |
task.call(); |
500 |
} catch(Exception ex) { |
501 |
unexpectedException(); |
502 |
} finally { |
503 |
Policy.setPolicy(savedPolicy); |
504 |
} |
505 |
} |
506 |
|
507 |
/** |
508 |
* callable(Runnable) returns null when called |
509 |
*/ |
510 |
public void testCallable1() { |
511 |
try { |
512 |
Callable c = Executors.callable(new NoOpRunnable()); |
513 |
assertNull(c.call()); |
514 |
} catch(Exception ex) { |
515 |
unexpectedException(); |
516 |
} |
517 |
|
518 |
} |
519 |
|
520 |
/** |
521 |
* callable(Runnable, result) returns result when called |
522 |
*/ |
523 |
public void testCallable2() { |
524 |
try { |
525 |
Callable c = Executors.callable(new NoOpRunnable(), one); |
526 |
assertEquals(one, c.call()); |
527 |
} catch(Exception ex) { |
528 |
unexpectedException(); |
529 |
} |
530 |
} |
531 |
|
532 |
/** |
533 |
* callable(PrivilegedAction) returns its result when called |
534 |
*/ |
535 |
public void testCallable3() { |
536 |
try { |
537 |
Callable c = Executors.callable(new PrivilegedAction() { |
538 |
public Object run() { return one; }}); |
539 |
assertEquals(one, c.call()); |
540 |
} catch(Exception ex) { |
541 |
unexpectedException(); |
542 |
} |
543 |
} |
544 |
|
545 |
/** |
546 |
* callable(PrivilegedExceptionAction) returns its result when called |
547 |
*/ |
548 |
public void testCallable4() { |
549 |
try { |
550 |
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
551 |
public Object run() { return one; }}); |
552 |
assertEquals(one, c.call()); |
553 |
} catch(Exception ex) { |
554 |
unexpectedException(); |
555 |
} |
556 |
} |
557 |
|
558 |
|
559 |
/** |
560 |
* callable(null Runnable) throws NPE |
561 |
*/ |
562 |
public void testCallableNPE1() { |
563 |
try { |
564 |
Runnable r = null; |
565 |
Callable c = Executors.callable(r); |
566 |
} catch (NullPointerException success) { |
567 |
} |
568 |
} |
569 |
|
570 |
/** |
571 |
* callable(null, result) throws NPE |
572 |
*/ |
573 |
public void testCallableNPE2() { |
574 |
try { |
575 |
Runnable r = null; |
576 |
Callable c = Executors.callable(r, one); |
577 |
} catch (NullPointerException success) { |
578 |
} |
579 |
} |
580 |
|
581 |
/** |
582 |
* callable(null PrivilegedAction) throws NPE |
583 |
*/ |
584 |
public void testCallableNPE3() { |
585 |
try { |
586 |
PrivilegedAction r = null; |
587 |
Callable c = Executors.callable(r); |
588 |
} catch (NullPointerException success) { |
589 |
} |
590 |
} |
591 |
|
592 |
/** |
593 |
* callable(null PrivilegedExceptionAction) throws NPE |
594 |
*/ |
595 |
public void testCallableNPE4() { |
596 |
try { |
597 |
PrivilegedExceptionAction r = null; |
598 |
Callable c = Executors.callable(r); |
599 |
} catch (NullPointerException success) { |
600 |
} |
601 |
} |
602 |
|
603 |
|
604 |
} |