60 |
|
pf.run(); |
61 |
|
pf.runAndReset(); |
62 |
|
assertEquals(savedRunCount, pf.runCount()); |
63 |
+ |
Object r2 = null; |
64 |
|
try { |
65 |
< |
assertSame(r, f.get()); |
65 |
> |
r2 = f.get(); |
66 |
|
} catch (CancellationException t) { |
67 |
|
assertSame(exInfo, CancellationException.class); |
68 |
|
} catch (ExecutionException t) { |
70 |
|
} catch (Throwable t) { |
71 |
|
threadUnexpectedException(t); |
72 |
|
} |
73 |
+ |
if (exInfo == null) |
74 |
+ |
assertSame(r, r2); |
75 |
|
assertTrue(f.isDone()); |
76 |
|
} |
77 |
|
} |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
< |
<T> void checkCompletedNormally(Future<T> f, T expected) { |
107 |
> |
<T> void checkCompletedNormally(Future<T> f, T expectedValue) { |
108 |
|
checkIsDone(f); |
109 |
|
assertFalse(f.isCancelled()); |
110 |
|
|
111 |
+ |
T v1 = null, v2 = null; |
112 |
|
try { |
113 |
< |
assertSame(expected, f.get()); |
114 |
< |
assertSame(expected, f.get(randomTimeout(), randomTimeUnit())); |
113 |
> |
v1 = f.get(); |
114 |
> |
v2 = f.get(randomTimeout(), randomTimeUnit()); |
115 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
+ |
assertSame(expectedValue, v1); |
117 |
+ |
assertSame(expectedValue, v2); |
118 |
|
} |
119 |
|
|
120 |
|
void checkCancelled(Future<?> f) { |
464 |
|
try { |
465 |
|
task.cancel(true); |
466 |
|
shouldThrow(); |
467 |
< |
} catch (SecurityException expected) {} |
467 |
> |
} catch (SecurityException success) {} |
468 |
|
|
469 |
|
// We failed to deliver the interrupt, but the world retains |
470 |
|
// its sanity, as if we had done task.cancel(false) |
720 |
|
/** |
721 |
|
* get is interruptible |
722 |
|
*/ |
723 |
< |
public void testGet_interruptible() { |
723 |
> |
public void testGet_Interruptible() { |
724 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
725 |
|
final FutureTask task = new FutureTask(new NoOpCallable()); |
726 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
749 |
|
/** |
750 |
|
* timed get is interruptible |
751 |
|
*/ |
752 |
< |
public void testTimedGet_interruptible() { |
752 |
> |
public void testTimedGet_Interruptible() { |
753 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
754 |
|
final FutureTask task = new FutureTask(new NoOpCallable()); |
755 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
756 |
|
public void realRun() throws Exception { |
757 |
|
Thread.currentThread().interrupt(); |
758 |
|
try { |
759 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
759 |
> |
task.get(randomTimeout(), randomTimeUnit()); |
760 |
|
shouldThrow(); |
761 |
|
} catch (InterruptedException success) {} |
762 |
|
assertFalse(Thread.interrupted()); |
763 |
|
|
764 |
|
pleaseInterrupt.countDown(); |
765 |
|
try { |
766 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
766 |
> |
task.get(LONGER_DELAY_MS, MILLISECONDS); |
767 |
|
shouldThrow(); |
768 |
|
} catch (InterruptedException success) {} |
769 |
|
assertFalse(Thread.interrupted()); |
770 |
|
}}); |
771 |
|
|
772 |
|
await(pleaseInterrupt); |
773 |
+ |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
774 |
|
t.interrupt(); |
775 |
|
awaitTermination(t); |
776 |
|
checkNotDone(task); |
845 |
|
* toString indicates current completion state |
846 |
|
*/ |
847 |
|
public void testToString_incomplete() { |
848 |
< |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
848 |
> |
FutureTask<String> f = new FutureTask<>(() -> ""); |
849 |
|
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
850 |
|
if (testImplementationDetails) |
851 |
|
assertTrue(f.toString().startsWith( |
853 |
|
} |
854 |
|
|
855 |
|
public void testToString_normal() { |
856 |
< |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
856 |
> |
FutureTask<String> f = new FutureTask<>(() -> ""); |
857 |
|
f.run(); |
858 |
|
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
859 |
|
if (testImplementationDetails) |
862 |
|
} |
863 |
|
|
864 |
|
public void testToString_exception() { |
865 |
< |
FutureTask<String> f = new FutureTask<String>( |
865 |
> |
FutureTask<String> f = new FutureTask<>( |
866 |
|
() -> { throw new ArithmeticException(); }); |
867 |
|
f.run(); |
868 |
|
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
873 |
|
|
874 |
|
public void testToString_cancelled() { |
875 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
876 |
< |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
876 |
> |
FutureTask<String> f = new FutureTask<>(() -> ""); |
877 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
878 |
|
assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); |
879 |
|
if (testImplementationDetails) |