--- jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2015/05/15 17:07:27 1.36 +++ jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java 2015/09/28 21:15:44 1.44 @@ -14,6 +14,7 @@ import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; +import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; @@ -29,6 +30,7 @@ import java.util.concurrent.ThreadFactor import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -99,11 +101,13 @@ public class ThreadPoolExecutorSubclassT } lock.lock(); try { - result = v; - exception = e; - done = true; - thread = null; - cond.signalAll(); + if (!done) { + result = v; + exception = e; + done = true; + thread = null; + cond.signalAll(); + } } finally { lock.unlock(); } } @@ -112,6 +116,8 @@ public class ThreadPoolExecutorSubclassT try { while (!done) cond.await(); + if (cancelled) + throw new CancellationException(); if (exception != null) throw new ExecutionException(exception); return result; @@ -123,12 +129,13 @@ public class ThreadPoolExecutorSubclassT long nanos = unit.toNanos(timeout); lock.lock(); try { - for (;;) { - if (done) break; + while (!done) { if (nanos < 0) throw new TimeoutException(); nanos = cond.awaitNanos(nanos); } + if (cancelled) + throw new CancellationException(); if (exception != null) throw new ExecutionException(exception); return result; @@ -345,7 +352,7 @@ public class ThreadPoolExecutorSubclassT */ public void testGetKeepAliveTime() { ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue(10)); - assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); + assertEquals(1, p.getKeepAliveTime(SECONDS)); joinPool(p); } @@ -698,22 +705,42 @@ public class ThreadPoolExecutorSubclassT } /** - * shutdownNow returns a list containing tasks that were not run + * shutdownNow returns a list containing tasks that were not run, + * and those tasks are drained from the queue */ - public void testShutdownNow() { - ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); - List l; - try { - for (int i = 0; i < 5; i++) - p.execute(new MediumPossiblyInterruptedRunnable()); - } - finally { + public void testShutdownNow() throws InterruptedException { + final int poolSize = 2; + final int count = 5; + final AtomicInteger ran = new AtomicInteger(0); + ThreadPoolExecutor p = + new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, + new ArrayBlockingQueue(10)); + CountDownLatch threadsStarted = new CountDownLatch(poolSize); + Runnable waiter = new CheckedRunnable() { public void realRun() { + threadsStarted.countDown(); try { - l = p.shutdownNow(); - } catch (SecurityException ok) { return; } + MILLISECONDS.sleep(2 * LONG_DELAY_MS); + } catch (InterruptedException success) {} + ran.getAndIncrement(); + }}; + for (int i = 0; i < count; i++) + p.execute(waiter); + assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(poolSize, p.getActiveCount()); + assertEquals(0, p.getCompletedTaskCount()); + final List queuedTasks; + try { + queuedTasks = p.shutdownNow(); + } catch (SecurityException ok) { + return; // Allowed in case test doesn't have privs } assertTrue(p.isShutdown()); - assertTrue(l.size() <= 4); + assertTrue(p.getQueue().isEmpty()); + assertEquals(count - poolSize, queuedTasks.size()); + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isTerminated()); + assertEquals(poolSize, ran.get()); + assertEquals(poolSize, p.getCompletedTaskCount()); } // Exception Tests @@ -1223,9 +1250,10 @@ public class ThreadPoolExecutorSubclassT * execute(null) throws NPE */ public void testExecuteNull() { - ThreadPoolExecutor p = null; + ThreadPoolExecutor p = + new CustomTPE(1, 2, 1L, SECONDS, + new ArrayBlockingQueue(10)); try { - p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue(10)); p.execute(null); shouldThrow(); } catch (NullPointerException success) {} @@ -1736,17 +1764,29 @@ public class ThreadPoolExecutorSubclassT public void testTimedInvokeAll6() throws Exception { ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue(10)); try { - List> l = new ArrayList>(); - l.add(new StringTask()); - l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); - l.add(new StringTask()); - List> futures = - e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); - assertEquals(l.size(), futures.size()); - for (Future future : futures) - assertTrue(future.isDone()); - assertFalse(futures.get(0).isCancelled()); - assertTrue(futures.get(1).isCancelled()); + for (long timeout = timeoutMillis();;) { + List> tasks = new ArrayList<>(); + tasks.add(new StringTask("0")); + tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); + tasks.add(new StringTask("2")); + long startTime = System.nanoTime(); + List> futures = + e.invokeAll(tasks, timeout, MILLISECONDS); + assertEquals(tasks.size(), futures.size()); + assertTrue(millisElapsedSince(startTime) >= timeout); + for (Future future : futures) + assertTrue(future.isDone()); + assertTrue(futures.get(1).isCancelled()); + try { + assertEquals("0", futures.get(0).get()); + assertEquals("2", futures.get(2).get()); + break; + } catch (CancellationException retryWithLongerTimeout) { + timeout *= 2; + if (timeout >= LONG_DELAY_MS / 2) + fail("expected exactly one task to be cancelled"); + } + } } finally { joinPool(e); } @@ -1789,21 +1829,21 @@ public class ThreadPoolExecutorSubclassT * allowCoreThreadTimeOut(true) causes idle threads to time out */ public void testAllowCoreThreadTimeOut_true() throws Exception { - long coreThreadTimeOut = SHORT_DELAY_MS; + long keepAliveTime = timeoutMillis(); final ThreadPoolExecutor p = new CustomTPE(2, 10, - coreThreadTimeOut, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { p.allowCoreThreadTimeOut(true); p.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { + public void realRun() { threadStarted.countDown(); assertEquals(1, p.getPoolSize()); }}); await(threadStarted); - delay(coreThreadTimeOut); + delay(keepAliveTime); long startTime = System.nanoTime(); while (p.getPoolSize() > 0 && millisElapsedSince(startTime) < LONG_DELAY_MS) @@ -1819,10 +1859,10 @@ public class ThreadPoolExecutorSubclassT * allowCoreThreadTimeOut(false) causes idle threads not to time out */ public void testAllowCoreThreadTimeOut_false() throws Exception { - long coreThreadTimeOut = SHORT_DELAY_MS; + long keepAliveTime = timeoutMillis(); final ThreadPoolExecutor p = new CustomTPE(2, 10, - coreThreadTimeOut, MILLISECONDS, + keepAliveTime, MILLISECONDS, new ArrayBlockingQueue(10)); final CountDownLatch threadStarted = new CountDownLatch(1); try { @@ -1832,11 +1872,52 @@ public class ThreadPoolExecutorSubclassT threadStarted.countDown(); assertTrue(p.getPoolSize() >= 1); }}); - delay(2 * coreThreadTimeOut); + delay(2 * keepAliveTime); assertTrue(p.getPoolSize() >= 1); } finally { joinPool(p); } } + /** + * get(cancelled task) throws CancellationException + * (in part, a test of CustomTPE itself) + */ + public void testGet_cancelled() throws Exception { + final ExecutorService e = + new CustomTPE(1, 1, + LONG_DELAY_MS, MILLISECONDS, + new LinkedBlockingQueue()); + try { + final CountDownLatch blockerStarted = new CountDownLatch(1); + final CountDownLatch done = new CountDownLatch(1); + final List> futures = new ArrayList<>(); + for (int i = 0; i < 2; i++) { + Runnable r = new CheckedRunnable() { public void realRun() + throws Throwable { + blockerStarted.countDown(); + assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); + }}; + futures.add(e.submit(r)); + } + assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); + for (Future future : futures) future.cancel(false); + for (Future future : futures) { + try { + future.get(); + shouldThrow(); + } catch (CancellationException success) {} + try { + future.get(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (CancellationException success) {} + assertTrue(future.isCancelled()); + assertTrue(future.isDone()); + } + done.countDown(); + } finally { + joinPool(e); + } + } + }