4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
|
|
9 |
|
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
69 |
|
|
70 |
|
Thread.currentThread().interrupt(); |
71 |
|
try { |
72 |
< |
a.get(5L, SECONDS); |
72 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
73 |
|
shouldThrow(); |
74 |
|
} catch (InterruptedException success) { |
75 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
|
} |
77 |
|
|
78 |
|
try { |
79 |
< |
a.get(0L, SECONDS); |
79 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
80 |
|
shouldThrow(); |
81 |
|
} catch (TimeoutException success) { |
82 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
|
} |
84 |
|
|
85 |
< |
<T> void checkCompletedNormally(RecursiveTask<T> a, T expected) { |
85 |
> |
<T> void checkCompletedNormally(RecursiveTask<T> a, T expectedValue) { |
86 |
|
assertTrue(a.isDone()); |
87 |
|
assertFalse(a.isCancelled()); |
88 |
|
assertTrue(a.isCompletedNormally()); |
89 |
|
assertFalse(a.isCompletedAbnormally()); |
90 |
|
assertNull(a.getException()); |
91 |
< |
assertSame(expected, a.getRawResult()); |
92 |
< |
assertSame(expected, a.join()); |
91 |
> |
assertSame(expectedValue, a.getRawResult()); |
92 |
> |
assertSame(expectedValue, a.join()); |
93 |
|
assertFalse(a.cancel(false)); |
94 |
|
assertFalse(a.cancel(true)); |
95 |
+ |
|
96 |
+ |
T v1 = null, v2 = null; |
97 |
|
try { |
98 |
< |
assertSame(expected, a.get()); |
99 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
98 |
< |
try { |
99 |
< |
assertSame(expected, a.get(5L, SECONDS)); |
98 |
> |
v1 = a.get(); |
99 |
> |
v2 = a.get(randomTimeout(), randomTimeUnit()); |
100 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
101 |
+ |
assertSame(expectedValue, v1); |
102 |
+ |
assertSame(expectedValue, v2); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
|
* Waits for the task to complete, and checks that when it does, |
107 |
|
* it will have an Integer result equals to the given int. |
108 |
|
*/ |
109 |
< |
void checkCompletesNormally(RecursiveTask<Integer> a, int expected) { |
109 |
> |
void checkCompletesNormally(RecursiveTask<Integer> a, int expectedValue) { |
110 |
|
Integer r = a.join(); |
111 |
< |
assertEquals(expected, (int) r); |
111 |
> |
assertEquals(expectedValue, (int) r); |
112 |
|
checkCompletedNormally(a, r); |
113 |
|
} |
114 |
|
|
116 |
|
* Like checkCompletesNormally, but verifies that the task has |
117 |
|
* already completed. |
118 |
|
*/ |
119 |
< |
void checkCompletedNormally(RecursiveTask<Integer> a, int expected) { |
119 |
> |
void checkCompletedNormally(RecursiveTask<Integer> a, int expectedValue) { |
120 |
|
Integer r = a.getRawResult(); |
121 |
< |
assertEquals(expected, (int) r); |
121 |
> |
assertEquals(expectedValue, (int) r); |
122 |
|
checkCompletedNormally(a, r); |
123 |
|
} |
124 |
|
|
143 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
144 |
|
|
145 |
|
try { |
146 |
< |
a.get(5L, SECONDS); |
146 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
147 |
|
shouldThrow(); |
148 |
|
} catch (CancellationException success) { |
149 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
174 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
|
|
176 |
|
try { |
177 |
< |
a.get(5L, SECONDS); |
177 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
178 |
|
shouldThrow(); |
179 |
|
} catch (ExecutionException success) { |
180 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
295 |
|
public Integer realCompute() throws Exception { |
296 |
|
FibTask f = new FibTask(8); |
297 |
|
assertSame(f, f.fork()); |
298 |
< |
Integer r = f.get(5L, SECONDS); |
298 |
> |
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); |
299 |
|
assertEquals(21, (int) r); |
300 |
|
checkCompletedNormally(f, r); |
301 |
|
return r; |
421 |
|
FailingFibTask f = new FailingFibTask(8); |
422 |
|
assertSame(f, f.fork()); |
423 |
|
try { |
424 |
< |
Integer r = f.get(5L, SECONDS); |
424 |
> |
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); |
425 |
|
shouldThrow(); |
426 |
|
} catch (ExecutionException success) { |
427 |
|
Throwable cause = success.getCause(); |
518 |
|
assertTrue(f.cancel(true)); |
519 |
|
assertSame(f, f.fork()); |
520 |
|
try { |
521 |
< |
Integer r = f.get(5L, SECONDS); |
521 |
> |
Integer r = f.get(LONG_DELAY_MS, MILLISECONDS); |
522 |
|
shouldThrow(); |
523 |
|
} catch (CancellationException success) { |
524 |
|
checkCancelled(f); |