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) |
754 |
|
final FutureTask task = new FutureTask(new NoOpCallable()); |
755 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
756 |
|
public void realRun() throws Exception { |
757 |
+ |
long startTime = System.nanoTime(); |
758 |
+ |
|
759 |
|
Thread.currentThread().interrupt(); |
760 |
|
try { |
761 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
761 |
> |
task.get(randomTimeout(), randomTimeUnit()); |
762 |
|
shouldThrow(); |
763 |
|
} catch (InterruptedException success) {} |
764 |
|
assertFalse(Thread.interrupted()); |
765 |
|
|
766 |
|
pleaseInterrupt.countDown(); |
767 |
|
try { |
768 |
< |
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
768 |
> |
task.get(LONG_DELAY_MS, MILLISECONDS); |
769 |
|
shouldThrow(); |
770 |
|
} catch (InterruptedException success) {} |
771 |
|
assertFalse(Thread.interrupted()); |
772 |
+ |
|
773 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
774 |
|
}}); |
775 |
|
|
776 |
|
await(pleaseInterrupt); |
848 |
|
* toString indicates current completion state |
849 |
|
*/ |
850 |
|
public void testToString_incomplete() { |
851 |
< |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
851 |
> |
FutureTask<String> f = new FutureTask<>(() -> ""); |
852 |
|
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
853 |
|
if (testImplementationDetails) |
854 |
|
assertTrue(f.toString().startsWith( |
856 |
|
} |
857 |
|
|
858 |
|
public void testToString_normal() { |
859 |
< |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
859 |
> |
FutureTask<String> f = new FutureTask<>(() -> ""); |
860 |
|
f.run(); |
861 |
|
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
862 |
|
if (testImplementationDetails) |
865 |
|
} |
866 |
|
|
867 |
|
public void testToString_exception() { |
868 |
< |
FutureTask<String> f = new FutureTask<String>( |
868 |
> |
FutureTask<String> f = new FutureTask<>( |
869 |
|
() -> { throw new ArithmeticException(); }); |
870 |
|
f.run(); |
871 |
|
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
876 |
|
|
877 |
|
public void testToString_cancelled() { |
878 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
879 |
< |
FutureTask<String> f = new FutureTask<String>(() -> ""); |
879 |
> |
FutureTask<String> f = new FutureTask<>(() -> ""); |
880 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
881 |
|
assertTrue(f.toString().matches(".*\\[.*Cancelled.*\\]")); |
882 |
|
if (testImplementationDetails) |