--- jsr166/src/test/tck/FutureTaskTest.java 2012/12/20 04:58:53 1.33 +++ jsr166/src/test/tck/FutureTaskTest.java 2013/06/19 05:54:45 1.38 @@ -7,7 +7,6 @@ */ import junit.framework.*; -import java.security.Permission; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; @@ -38,16 +37,31 @@ public class FutureTaskTest extends JSR1 assertEquals(1, pf.doneCount()); assertFalse(pf.runAndReset()); assertEquals(1, pf.doneCount()); + Object r = null; Object exInfo = null; + try { + r = f.get(); + } catch (CancellationException t) { + exInfo = CancellationException.class; + } catch (ExecutionException t) { + exInfo = t.getCause(); + } catch (Throwable t) { + threadUnexpectedException(t); + } // Check that run and runAndReset have no effect. int savedRunCount = pf.runCount(); - int savedSetCount = pf.setCount(); - int savedSetExceptionCount = pf.setExceptionCount(); pf.run(); pf.runAndReset(); assertEquals(savedRunCount, pf.runCount()); - assertEquals(savedSetCount, pf.setCount()); - assertEquals(savedSetExceptionCount, pf.setExceptionCount()); + try { + assertSame(r, f.get()); + } catch (CancellationException t) { + assertSame(exInfo, CancellationException.class); + } catch (ExecutionException t) { + assertSame(exInfo, t.getCause()); + } catch (Throwable t) { + threadUnexpectedException(t); + } assertTrue(f.isDone()); } } @@ -69,8 +83,13 @@ public class FutureTaskTest extends JSR1 FutureTask ft = (FutureTask) f; // Check that run methods do nothing ft.run(); - if (f instanceof PublicFutureTask) - assertFalse(((PublicFutureTask) f).runAndReset()); + if (f instanceof PublicFutureTask) { + PublicFutureTask pf = (PublicFutureTask) f; + int savedRunCount = pf.runCount(); + pf.run(); + assertFalse(pf.runAndReset()); + assertEquals(savedRunCount, pf.runCount()); + } checkNotDone(f); } } @@ -462,7 +481,10 @@ public class FutureTaskTest extends JSR1 try { pleaseCancel.countDown(); delay(LONG_DELAY_MS); - } finally { throw new RuntimeException(); } + threadShouldThrow(); + } catch (InterruptedException success) { + } catch (Throwable t) { threadUnexpectedException(t); } + throw new RuntimeException(); }}); Thread t = newStartedThread(task); @@ -587,51 +609,55 @@ public class FutureTaskTest extends JSR1 * CancellationException */ public void testTimedGet_Cancellation() { - for (final boolean mayInterruptIfRunning : - new boolean[] { true, false }) { - final CountDownLatch pleaseCancel = new CountDownLatch(3); - final CountDownLatch cancelled = new CountDownLatch(1); - final PublicFutureTask task = - new PublicFutureTask(new CheckedCallable() { - public Object realCall() throws InterruptedException { - pleaseCancel.countDown(); - if (mayInterruptIfRunning) { - try { - delay(2*LONG_DELAY_MS); - } catch (InterruptedException success) {} - } else { - await(cancelled); - } - return two; - }}); + testTimedGet_Cancellation(false); + } + public void testTimedGet_Cancellation_interrupt() { + testTimedGet_Cancellation(true); + } + public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { + final CountDownLatch pleaseCancel = new CountDownLatch(3); + final CountDownLatch cancelled = new CountDownLatch(1); + final Callable callable = + new CheckedCallable() { + public Object realCall() throws InterruptedException { + pleaseCancel.countDown(); + if (mayInterruptIfRunning) { + try { + delay(2*LONG_DELAY_MS); + } catch (InterruptedException success) {} + } else { + await(cancelled); + } + return two; + }}; + final PublicFutureTask task = new PublicFutureTask(callable); - Thread t1 = new ThreadShouldThrow(CancellationException.class) { + Thread t1 = new ThreadShouldThrow(CancellationException.class) { public void realRun() throws Exception { pleaseCancel.countDown(); task.get(); }}; - Thread t2 = new ThreadShouldThrow(CancellationException.class) { + Thread t2 = new ThreadShouldThrow(CancellationException.class) { public void realRun() throws Exception { pleaseCancel.countDown(); task.get(2*LONG_DELAY_MS, MILLISECONDS); }}; - t1.start(); - t2.start(); - Thread t3 = newStartedThread(task); - await(pleaseCancel); - checkIsRunning(task); - task.cancel(mayInterruptIfRunning); - checkCancelled(task); - awaitTermination(t1); - awaitTermination(t2); - cancelled.countDown(); - awaitTermination(t3); - assertEquals(1, task.runCount()); - assertEquals(1, task.setCount()); - assertEquals(0, task.setExceptionCount()); - tryToConfuseDoneTask(task); - checkCancelled(task); - } + t1.start(); + t2.start(); + Thread t3 = newStartedThread(task); + await(pleaseCancel); + checkIsRunning(task); + task.cancel(mayInterruptIfRunning); + checkCancelled(task); + awaitTermination(t1); + awaitTermination(t2); + cancelled.countDown(); + awaitTermination(t3); + assertEquals(1, task.runCount()); + assertEquals(1, task.setCount()); + assertEquals(0, task.setExceptionCount()); + tryToConfuseDoneTask(task); + checkCancelled(task); } /**