--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2003/12/27 20:42:48 1.15 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2004/12/31 14:52:40 1.21 @@ -36,6 +36,15 @@ public class ThreadPoolExecutorTest exte } } + static class FailingThreadFactory implements ThreadFactory{ + int calls = 0; + public Thread newThread(Runnable r){ + if (++calls > 1) return null; + return new Thread(r); + } + } + + /** * execute successfully executes a runnable */ @@ -117,7 +126,7 @@ public class ThreadPoolExecutorTest exte unexpectedException(); } assertEquals(1, p2.getCompletedTaskCount()); - p2.shutdown(); + try { p2.shutdown(); } catch(SecurityException ok) { return; } joinPool(p2); } @@ -147,7 +156,6 @@ public class ThreadPoolExecutorTest exte ThreadFactory tf = new SimpleThreadFactory(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10), tf, new NoOpREHandler()); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } @@ -159,7 +167,6 @@ public class ThreadPoolExecutorTest exte ThreadFactory tf = new SimpleThreadFactory(); p.setThreadFactory(tf); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } @@ -185,7 +192,6 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new NoOpREHandler(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10), h); assertSame(h, p.getRejectedExecutionHandler()); - p.shutdown(); joinPool(p); } @@ -198,7 +204,6 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new NoOpREHandler(); p.setRejectedExecutionHandler(h); assertSame(h, p.getRejectedExecutionHandler()); - p.shutdown(); joinPool(p); } @@ -281,7 +286,7 @@ public class ThreadPoolExecutorTest exte ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); assertFalse(p1.isShutdown()); - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } assertTrue(p1.isShutdown()); joinPool(p1); } @@ -296,7 +301,7 @@ public class ThreadPoolExecutorTest exte try { p1.execute(new MediumRunnable()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } } try { assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); @@ -316,7 +321,7 @@ public class ThreadPoolExecutorTest exte p1.execute(new SmallRunnable()); assertFalse(p1.isTerminating()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch(SecurityException ok) { return; } } try { assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); @@ -344,7 +349,6 @@ public class ThreadPoolExecutorTest exte assertSame(q, wq); assertFalse(wq.contains(tasks[0])); assertTrue(wq.contains(tasks[4])); - p1.shutdownNow(); } catch(Exception e) { unexpectedException(); } finally { @@ -374,7 +378,6 @@ public class ThreadPoolExecutorTest exte assertTrue(q.contains(tasks[3])); assertTrue(p1.remove(tasks[3])); assertFalse(q.contains(tasks[3])); - p1.shutdownNow(); } catch(Exception e) { unexpectedException(); } finally { @@ -397,7 +400,6 @@ public class ThreadPoolExecutorTest exte p1.purge(); long count = p1.getTaskCount(); assertTrue(count >= 2 && count < 5); - p1.shutdownNow(); joinPool(p1); } @@ -412,7 +414,10 @@ public class ThreadPoolExecutorTest exte p1.execute(new MediumPossiblyInterruptedRunnable()); } finally { - l = p1.shutdownNow(); + try { + l = p1.shutdownNow(); + } catch (SecurityException ok) { return; } + } assertTrue(p1.isShutdown()); assertTrue(l.size() <= 4); @@ -773,7 +778,7 @@ public class ThreadPoolExecutorTest exte for(int i = 1; i < 5; ++i) { assertTrue(tasks[i].done); } - p.shutdownNow(); + try { p.shutdownNow(); } catch(SecurityException ok) { return; } } catch(RejectedExecutionException ex){ unexpectedException(); } finally { @@ -800,7 +805,7 @@ public class ThreadPoolExecutorTest exte for(int i = 0; i < 5; ++i){ assertFalse(tasks[i].done); } - p.shutdownNow(); + try { p.shutdownNow(); } catch(SecurityException ok) { return; } } catch(RejectedExecutionException ex){ unexpectedException(); } finally { @@ -823,7 +828,7 @@ public class ThreadPoolExecutorTest exte p.execute(r3); assertFalse(p.getQueue().contains(r2)); assertTrue(p.getQueue().contains(r3)); - p.shutdownNow(); + try { p.shutdownNow(); } catch(SecurityException ok) { return; } } catch(RejectedExecutionException ex){ unexpectedException(); } finally { @@ -837,7 +842,7 @@ public class ThreadPoolExecutorTest exte public void testRejectedExecutionExceptionOnShutdown() { ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(1)); - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } try { tpe.execute(new NoOpRunnable()); shouldThrow(); @@ -853,7 +858,7 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); - p.shutdown(); + try { p.shutdown(); } catch(SecurityException ok) { return; } try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); @@ -872,7 +877,7 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); - p.shutdown(); + try { p.shutdown(); } catch(SecurityException ok) { return; } try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); @@ -892,7 +897,7 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); - p.shutdown(); + try { p.shutdown(); } catch(SecurityException ok) { return; } try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); @@ -932,7 +937,7 @@ public class ThreadPoolExecutorTest exte shouldThrow(); } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } joinPool(tpe); } @@ -951,7 +956,7 @@ public class ThreadPoolExecutorTest exte shouldThrow(); } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } joinPool(tpe); } @@ -970,7 +975,7 @@ public class ThreadPoolExecutorTest exte shouldThrow(); } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } joinPool(tpe); } @@ -991,7 +996,7 @@ public class ThreadPoolExecutorTest exte shouldThrow(); } catch(IllegalArgumentException success){ } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } joinPool(tpe); } @@ -1001,7 +1006,7 @@ public class ThreadPoolExecutorTest exte */ public void testTerminated() { ExtendedTPE tpe = new ExtendedTPE(); - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } assertTrue(tpe.terminatedCalled); joinPool(tpe); } @@ -1018,7 +1023,7 @@ public class ThreadPoolExecutorTest exte assertTrue(r.done); assertTrue(tpe.beforeCalled); assertTrue(tpe.afterCalled); - tpe.shutdown(); + try { tpe.shutdown(); } catch(SecurityException ok) { return; } } catch(Exception ex) { unexpectedException(); @@ -1483,14 +1488,17 @@ public class ThreadPoolExecutorTest exte ArrayList> l = new ArrayList>(); l.add(new StringTask()); l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); + l.add(new StringTask()); List> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - assertEquals(2, result.size()); + assertEquals(3, result.size()); Iterator> it = result.iterator(); Future f1 = it.next(); Future f2 = it.next(); + Future f3 = it.next(); assertTrue(f1.isDone()); - assertFalse(f1.isCancelled()); assertTrue(f2.isDone()); + assertTrue(f3.isDone()); + assertFalse(f1.isCancelled()); assertTrue(f2.isCancelled()); } catch(Exception ex) { unexpectedException(); @@ -1499,5 +1507,66 @@ public class ThreadPoolExecutorTest exte } } + /** + * Execution continues if there is at least one thread even if + * thread factory fails to create more + */ + public void testFailingThreadFactory() { + ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new FailingThreadFactory()); + try { + ArrayList> l = new ArrayList>(); + for (int k = 0; k < 100; ++k) { + e.execute(new NoOpRunnable()); + } + Thread.sleep(LONG_DELAY_MS); + } catch(Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * allowsCoreThreadTimeOut is by default false. + */ + public void testAllowsCoreThreadTimeOut() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertFalse(tpe.allowsCoreThreadTimeOut()); + joinPool(tpe); + } + + /** + * allowCoreThreadTimeOut(true) causes idle threads to time out + */ + public void testAllowCoreThreadTimeOut_true() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + tpe.allowCoreThreadTimeOut(true); + tpe.execute(new NoOpRunnable()); + try { + Thread.sleep(MEDIUM_DELAY_MS); + assertEquals(0, tpe.getPoolSize()); + } catch(InterruptedException e){ + unexpectedException(); + } finally { + joinPool(tpe); + } + } + + /** + * allowCoreThreadTimeOut(false) causes idle threads not to time out + */ + public void testAllowCoreThreadTimeOut_false() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + tpe.allowCoreThreadTimeOut(false); + tpe.execute(new NoOpRunnable()); + try { + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(tpe.getPoolSize() >= 1); + } catch(InterruptedException e){ + unexpectedException(); + } finally { + joinPool(tpe); + } + } }