14 |
|
|
15 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(ThreadPoolExecutorTest.class); |
20 |
> |
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
static class CustomTask<V> implements RunnableFuture<V> { |
29 |
|
V result; |
30 |
|
Thread thread; |
31 |
|
Exception exception; |
32 |
< |
CustomTask(Callable<V> c) { callable = c; } |
33 |
< |
CustomTask(final Runnable r, final V res) { callable = new Callable<V>() { |
32 |
> |
CustomTask(Callable<V> c) { |
33 |
> |
if (c == null) throw new NullPointerException(); |
34 |
> |
callable = c; |
35 |
> |
} |
36 |
> |
CustomTask(final Runnable r, final V res) { |
37 |
> |
if (r == null) throw new NullPointerException(); |
38 |
> |
callable = new Callable<V>() { |
39 |
|
public V call() throws Exception { r.run(); return res; }}; |
40 |
|
} |
41 |
|
public boolean isDone() { |
197 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
198 |
|
try { |
199 |
|
p1.execute(new ShortRunnable()); |
200 |
< |
Thread.sleep(SMALL_DELAY_MS); |
200 |
> |
Thread.sleep(SMALL_DELAY_MS); |
201 |
|
} finally { |
202 |
|
joinPool(p1); |
203 |
|
} |
404 |
|
*/ |
405 |
|
public void testIsShutdown() { |
406 |
|
|
407 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
407 |
> |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
408 |
|
assertFalse(p1.isShutdown()); |
409 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
410 |
< |
assertTrue(p1.isShutdown()); |
410 |
> |
assertTrue(p1.isShutdown()); |
411 |
|
joinPool(p1); |
412 |
|
} |
413 |
|
|
416 |
|
* isTerminated is false before termination, true after |
417 |
|
*/ |
418 |
|
public void testIsTerminated() throws InterruptedException { |
419 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
419 |
> |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
420 |
|
assertFalse(p1.isTerminated()); |
421 |
|
try { |
422 |
|
p1.execute(new MediumRunnable()); |
431 |
|
* isTerminating is not true when running or when terminated |
432 |
|
*/ |
433 |
|
public void testIsTerminating() throws InterruptedException { |
434 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
434 |
> |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
435 |
|
assertFalse(p1.isTerminating()); |
436 |
|
try { |
437 |
|
p1.execute(new SmallRunnable()); |
518 |
|
* shutDownNow returns a list containing tasks that were not run |
519 |
|
*/ |
520 |
|
public void testShutDownNow() { |
521 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
521 |
> |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
522 |
|
List l; |
523 |
|
try { |
524 |
|
for (int i = 0; i < 5; i++) |
530 |
|
} catch (SecurityException ok) { return; } |
531 |
|
|
532 |
|
} |
533 |
< |
assertTrue(p1.isShutdown()); |
534 |
< |
assertTrue(l.size() <= 4); |
533 |
> |
assertTrue(p1.isShutdown()); |
534 |
> |
assertTrue(l.size() <= 4); |
535 |
|
} |
536 |
|
|
537 |
|
// Exception Tests |
921 |
|
ThreadPoolExecutor tpe = |
922 |
|
new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
923 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
924 |
< |
try { |
925 |
< |
tpe.execute(new NoOpRunnable()); |
926 |
< |
shouldThrow(); |
927 |
< |
} catch (RejectedExecutionException success) {} |
924 |
> |
try { |
925 |
> |
tpe.execute(new NoOpRunnable()); |
926 |
> |
shouldThrow(); |
927 |
> |
} catch (RejectedExecutionException success) {} |
928 |
|
|
929 |
< |
joinPool(tpe); |
929 |
> |
joinPool(tpe); |
930 |
|
} |
931 |
|
|
932 |
|
/** |
937 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
938 |
|
|
939 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
940 |
< |
try { |
940 |
> |
try { |
941 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
942 |
< |
p.execute(r); |
942 |
> |
p.execute(r); |
943 |
|
assertFalse(r.done); |
944 |
|
} finally { |
945 |
|
joinPool(p); |
954 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
955 |
|
|
956 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
957 |
< |
try { |
957 |
> |
try { |
958 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
959 |
< |
p.execute(r); |
959 |
> |
p.execute(r); |
960 |
|
assertFalse(r.done); |
961 |
|
} finally { |
962 |
|
joinPool(p); |
972 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
973 |
|
|
974 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
975 |
< |
try { |
975 |
> |
try { |
976 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
977 |
< |
p.execute(r); |
977 |
> |
p.execute(r); |
978 |
|
assertFalse(r.done); |
979 |
|
} finally { |
980 |
|
joinPool(p); |
988 |
|
public void testExecuteNull() { |
989 |
|
ThreadPoolExecutor tpe = null; |
990 |
|
try { |
991 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
992 |
< |
tpe.execute(null); |
991 |
> |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
992 |
> |
tpe.execute(null); |
993 |
|
shouldThrow(); |
994 |
< |
} catch (NullPointerException success) {} |
994 |
> |
} catch (NullPointerException success) {} |
995 |
|
|
996 |
< |
joinPool(tpe); |
996 |
> |
joinPool(tpe); |
997 |
|
} |
998 |
|
|
999 |
|
/** |
1000 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1001 |
|
*/ |
1002 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1003 |
< |
ThreadPoolExecutor tpe = null; |
1004 |
< |
try { |
1005 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1006 |
< |
} catch (Exception e) {} |
1007 |
< |
try { |
1008 |
< |
tpe.setCorePoolSize(-1); |
1004 |
< |
shouldThrow(); |
1005 |
< |
} catch (IllegalArgumentException success) { |
1003 |
> |
ThreadPoolExecutor tpe = |
1004 |
> |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1005 |
> |
try { |
1006 |
> |
tpe.setCorePoolSize(-1); |
1007 |
> |
shouldThrow(); |
1008 |
> |
} catch (IllegalArgumentException success) { |
1009 |
|
} finally { |
1010 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1011 |
|
} |
1056 |
|
* when given a negative value |
1057 |
|
*/ |
1058 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1059 |
< |
ThreadPoolExecutor tpe = null; |
1059 |
> |
ThreadPoolExecutor tpe = null; |
1060 |
|
try { |
1061 |
|
tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1062 |
|
} catch (Exception e) {} |
1063 |
|
|
1064 |
< |
try { |
1064 |
> |
try { |
1065 |
|
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
1066 |
|
shouldThrow(); |
1067 |
|
} catch (IllegalArgumentException success) { |
1178 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1179 |
|
try { |
1180 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1181 |
< |
l.add(new CheckedCallable<String>() { |
1182 |
< |
public String realCall() throws InterruptedException { |
1183 |
< |
latch.await(); |
1181 |
> |
l.add(new Callable<String>() { |
1182 |
> |
public String call() { |
1183 |
> |
try { |
1184 |
> |
latch.await(); |
1185 |
> |
} catch (InterruptedException ok) {} |
1186 |
|
return TEST_STRING; |
1187 |
|
}}); |
1188 |
|
l.add(null); |
1281 |
|
l.add(new NPETask()); |
1282 |
|
List<Future<String>> result = e.invokeAll(l); |
1283 |
|
assertEquals(1, result.size()); |
1284 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1285 |
< |
it.next().get(); |
1284 |
> |
for (Future<String> future : result) |
1285 |
> |
future.get(); |
1286 |
|
shouldThrow(); |
1287 |
|
} catch (ExecutionException success) { |
1288 |
|
} finally { |
1301 |
|
l.add(new StringTask()); |
1302 |
|
List<Future<String>> result = e.invokeAll(l); |
1303 |
|
assertEquals(2, result.size()); |
1304 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1305 |
< |
assertSame(TEST_STRING, it.next().get()); |
1301 |
< |
shouldThrow(); |
1302 |
< |
} catch (ExecutionException success) { |
1304 |
> |
for (Future<String> future : result) |
1305 |
> |
assertSame(TEST_STRING, future.get()); |
1306 |
|
} finally { |
1307 |
|
joinPool(e); |
1308 |
|
} |
1398 |
|
l.add(new StringTask()); |
1399 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1400 |
|
assertSame(TEST_STRING, result); |
1398 |
– |
shouldThrow(); |
1399 |
– |
} catch (ExecutionException success) { |
1401 |
|
} finally { |
1402 |
|
joinPool(e); |
1403 |
|
} |
1473 |
|
l.add(new NPETask()); |
1474 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1475 |
|
assertEquals(1, result.size()); |
1476 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1477 |
< |
it.next().get(); |
1476 |
> |
for (Future<String> future : result) |
1477 |
> |
future.get(); |
1478 |
|
shouldThrow(); |
1479 |
|
} catch (ExecutionException success) { |
1480 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
1481 |
|
} finally { |
1482 |
|
joinPool(e); |
1483 |
|
} |
1494 |
|
l.add(new StringTask()); |
1495 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1496 |
|
assertEquals(2, result.size()); |
1497 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1498 |
< |
assertSame(TEST_STRING, it.next().get()); |
1497 |
< |
shouldThrow(); |
1497 |
> |
for (Future<String> future : result) |
1498 |
> |
assertSame(TEST_STRING, future.get()); |
1499 |
|
} catch (ExecutionException success) { |
1500 |
|
} finally { |
1501 |
|
joinPool(e); |
1535 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1536 |
|
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1537 |
|
try { |
1537 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1538 |
|
for (int k = 0; k < 100; ++k) { |
1539 |
|
e.execute(new NoOpRunnable()); |
1540 |
|
} |