8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.*; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
13 |
|
import java.util.*; |
14 |
|
|
15 |
|
public class FutureTaskTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(FutureTaskTest.class); |
21 |
> |
return new TestSuite(FutureTaskTest.class); |
22 |
> |
} |
23 |
> |
|
24 |
> |
void checkNotDone(Future<?> f) { |
25 |
> |
assertFalse(f.isDone()); |
26 |
> |
assertFalse(f.isCancelled()); |
27 |
> |
} |
28 |
> |
|
29 |
> |
<T> void checkCompletedNormally(Future<T> f, T expected) { |
30 |
> |
assertTrue(f.isDone()); |
31 |
> |
assertFalse(f.isCancelled()); |
32 |
> |
|
33 |
> |
try { |
34 |
> |
assertSame(expected, f.get()); |
35 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
36 |
> |
try { |
37 |
> |
assertSame(expected, f.get(5L, SECONDS)); |
38 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
39 |
> |
|
40 |
> |
assertFalse(f.cancel(false)); |
41 |
> |
assertFalse(f.cancel(true)); |
42 |
> |
} |
43 |
> |
|
44 |
> |
void checkCancelled(Future<?> f) { |
45 |
> |
assertTrue(f.isDone()); |
46 |
> |
assertTrue(f.isCancelled()); |
47 |
> |
|
48 |
> |
try { |
49 |
> |
f.get(); |
50 |
> |
shouldThrow(); |
51 |
> |
} catch (CancellationException success) { |
52 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
53 |
> |
|
54 |
> |
try { |
55 |
> |
f.get(5L, SECONDS); |
56 |
> |
shouldThrow(); |
57 |
> |
} catch (CancellationException success) { |
58 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
59 |
> |
|
60 |
> |
assertFalse(f.cancel(false)); |
61 |
> |
assertFalse(f.cancel(true)); |
62 |
> |
} |
63 |
> |
|
64 |
> |
void checkCompletedAbnormally(Future<?> f, Throwable t) { |
65 |
> |
assertTrue(f.isDone()); |
66 |
> |
assertFalse(f.isCancelled()); |
67 |
> |
|
68 |
> |
try { |
69 |
> |
f.get(); |
70 |
> |
shouldThrow(); |
71 |
> |
} catch (ExecutionException success) { |
72 |
> |
assertSame(t, success.getCause()); |
73 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
74 |
> |
|
75 |
> |
try { |
76 |
> |
f.get(5L, SECONDS); |
77 |
> |
shouldThrow(); |
78 |
> |
} catch (ExecutionException success) { |
79 |
> |
assertSame(t, success.getCause()); |
80 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
81 |
> |
|
82 |
> |
assertFalse(f.cancel(false)); |
83 |
> |
assertFalse(f.cancel(true)); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
100 |
|
try { |
101 |
|
FutureTask task = new FutureTask(null); |
102 |
|
shouldThrow(); |
103 |
< |
} |
40 |
< |
catch (NullPointerException success) { |
41 |
< |
} |
103 |
> |
} catch (NullPointerException success) {} |
104 |
|
} |
105 |
|
|
106 |
|
/** |
110 |
|
try { |
111 |
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
112 |
|
shouldThrow(); |
113 |
< |
} |
52 |
< |
catch (NullPointerException success) { |
53 |
< |
} |
113 |
> |
} catch (NullPointerException success) {} |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
|
* isDone is true when a task completes |
118 |
|
*/ |
119 |
|
public void testIsDone() { |
120 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
121 |
< |
task.run(); |
122 |
< |
assertTrue(task.isDone()); |
123 |
< |
assertFalse(task.isCancelled()); |
120 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
121 |
> |
task.run(); |
122 |
> |
assertTrue(task.isDone()); |
123 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
128 |
|
*/ |
129 |
|
public void testRunAndReset() { |
130 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
131 |
< |
assertTrue(task.runAndReset()); |
132 |
< |
assertFalse(task.isDone()); |
131 |
> |
assertTrue(task.runAndReset()); |
132 |
> |
checkNotDone(task); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
138 |
|
public void testResetAfterCancel() { |
139 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
140 |
|
assertTrue(task.cancel(false)); |
141 |
< |
assertFalse(task.runAndReset()); |
142 |
< |
assertTrue(task.isDone()); |
83 |
< |
assertTrue(task.isCancelled()); |
141 |
> |
assertFalse(task.runAndReset()); |
142 |
> |
checkCancelled(task); |
143 |
|
} |
144 |
|
|
145 |
|
|
87 |
– |
|
146 |
|
/** |
147 |
|
* setting value causes get to return it |
148 |
|
*/ |
149 |
< |
public void testSet() { |
149 |
> |
public void testSet() throws Exception { |
150 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
151 |
|
task.set(one); |
152 |
< |
try { |
153 |
< |
assertEquals(task.get(), one); |
96 |
< |
} |
97 |
< |
catch (Exception e) { |
98 |
< |
unexpectedException(); |
99 |
< |
} |
152 |
> |
assertSame(task.get(), one); |
153 |
> |
checkCompletedNormally(task, one); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
|
* setException causes get to throw ExecutionException |
158 |
|
*/ |
159 |
< |
public void testSetException() { |
159 |
> |
public void testSetException() throws Exception { |
160 |
|
Exception nse = new NoSuchElementException(); |
161 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
162 |
|
task.setException(nse); |
163 |
|
try { |
164 |
|
Object x = task.get(); |
165 |
|
shouldThrow(); |
166 |
< |
} |
167 |
< |
catch (ExecutionException ee) { |
168 |
< |
Throwable cause = ee.getCause(); |
115 |
< |
assertEquals(cause, nse); |
116 |
< |
} |
117 |
< |
catch (Exception e) { |
118 |
< |
unexpectedException(); |
166 |
> |
} catch (ExecutionException success) { |
167 |
> |
assertSame(success.getCause(), nse); |
168 |
> |
checkCompletedAbnormally(task, nse); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* Cancelling before running succeeds |
173 |
> |
* Cancelling before running succeeds |
174 |
|
*/ |
175 |
|
public void testCancelBeforeRun() { |
176 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
176 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
177 |
|
assertTrue(task.cancel(false)); |
178 |
< |
task.run(); |
179 |
< |
assertTrue(task.isDone()); |
130 |
< |
assertTrue(task.isCancelled()); |
178 |
> |
task.run(); |
179 |
> |
checkCancelled(task); |
180 |
|
} |
181 |
|
|
182 |
|
/** |
183 |
|
* Cancel(true) before run succeeds |
184 |
|
*/ |
185 |
|
public void testCancelBeforeRun2() { |
186 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
186 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
187 |
|
assertTrue(task.cancel(true)); |
188 |
< |
task.run(); |
189 |
< |
assertTrue(task.isDone()); |
141 |
< |
assertTrue(task.isCancelled()); |
188 |
> |
task.run(); |
189 |
> |
checkCancelled(task); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
|
* cancel of a completed task fails |
194 |
|
*/ |
195 |
|
public void testCancelAfterRun() { |
196 |
< |
FutureTask task = new FutureTask( new NoOpCallable()); |
197 |
< |
task.run(); |
196 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
197 |
> |
task.run(); |
198 |
|
assertFalse(task.cancel(false)); |
199 |
< |
assertTrue(task.isDone()); |
152 |
< |
assertFalse(task.isCancelled()); |
199 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
|
* cancel(true) interrupts a running task |
204 |
|
*/ |
205 |
< |
public void testCancelInterrupt() { |
206 |
< |
FutureTask task = new FutureTask( new Callable() { |
207 |
< |
public Object call() { |
208 |
< |
try { |
209 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
210 |
< |
threadShouldThrow(); |
205 |
> |
public void testCancelInterrupt() throws InterruptedException { |
206 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
207 |
> |
final FutureTask task = |
208 |
> |
new FutureTask(new CheckedCallable<Object>() { |
209 |
> |
public Object realCall() { |
210 |
> |
threadStarted.countDown(); |
211 |
> |
long t0 = System.nanoTime(); |
212 |
> |
for (;;) { |
213 |
> |
if (Thread.interrupted()) |
214 |
> |
return Boolean.TRUE; |
215 |
> |
if (millisElapsedSince(t0) > MEDIUM_DELAY_MS) |
216 |
> |
fail("interrupt not delivered"); |
217 |
> |
Thread.yield(); |
218 |
|
} |
219 |
< |
catch (InterruptedException success) {} |
166 |
< |
return Boolean.TRUE; |
167 |
< |
} }); |
168 |
< |
Thread t = new Thread(task); |
169 |
< |
t.start(); |
219 |
> |
}}); |
220 |
|
|
221 |
< |
try { |
222 |
< |
Thread.sleep(SHORT_DELAY_MS); |
223 |
< |
assertTrue(task.cancel(true)); |
224 |
< |
t.join(); |
225 |
< |
assertTrue(task.isDone()); |
226 |
< |
assertTrue(task.isCancelled()); |
177 |
< |
} catch (InterruptedException e) { |
178 |
< |
unexpectedException(); |
179 |
< |
} |
221 |
> |
Thread t = newStartedThread(task); |
222 |
> |
threadStarted.await(); |
223 |
> |
assertTrue(task.cancel(true)); |
224 |
> |
checkCancelled(task); |
225 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
226 |
> |
checkCancelled(task); |
227 |
|
} |
228 |
|
|
182 |
– |
|
229 |
|
/** |
230 |
|
* cancel(false) does not interrupt a running task |
231 |
|
*/ |
232 |
< |
public void testCancelNoInterrupt() { |
233 |
< |
FutureTask task = new FutureTask( new Callable() { |
234 |
< |
public Object call() { |
235 |
< |
try { |
236 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
237 |
< |
} |
238 |
< |
catch (InterruptedException success) { |
239 |
< |
threadFail("should not interrupt"); |
240 |
< |
} |
232 |
> |
public void testCancelNoInterrupt() throws InterruptedException { |
233 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
234 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
235 |
> |
final FutureTask<Boolean> task = |
236 |
> |
new FutureTask<Boolean>(new CheckedCallable<Boolean>() { |
237 |
> |
public Boolean realCall() throws InterruptedException { |
238 |
> |
threadStarted.countDown(); |
239 |
> |
cancelled.await(MEDIUM_DELAY_MS, MILLISECONDS); |
240 |
> |
assertFalse(Thread.interrupted()); |
241 |
|
return Boolean.TRUE; |
242 |
< |
} }); |
197 |
< |
Thread t = new Thread(task); |
198 |
< |
t.start(); |
242 |
> |
}}); |
243 |
|
|
244 |
< |
try { |
245 |
< |
Thread.sleep(SHORT_DELAY_MS); |
246 |
< |
assertTrue(task.cancel(false)); |
247 |
< |
t.join(); |
248 |
< |
assertTrue(task.isDone()); |
249 |
< |
assertTrue(task.isCancelled()); |
250 |
< |
} catch (InterruptedException e) { |
207 |
< |
unexpectedException(); |
208 |
< |
} |
244 |
> |
Thread t = newStartedThread(task); |
245 |
> |
threadStarted.await(); |
246 |
> |
assertTrue(task.cancel(false)); |
247 |
> |
checkCancelled(task); |
248 |
> |
cancelled.countDown(); |
249 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
250 |
> |
checkCancelled(task); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* set in one thread causes get in another thread to retrieve value |
254 |
> |
* run in one thread causes get in another thread to retrieve value |
255 |
|
*/ |
256 |
< |
public void testGet1() { |
257 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
258 |
< |
public Object call() { |
259 |
< |
try { |
260 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
261 |
< |
} catch (InterruptedException e) { |
220 |
< |
threadUnexpectedException(); |
221 |
< |
} |
256 |
> |
public void testGetRun() throws InterruptedException { |
257 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
258 |
> |
|
259 |
> |
final FutureTask task = |
260 |
> |
new FutureTask(new CheckedCallable<Object>() { |
261 |
> |
public Object realCall() throws InterruptedException { |
262 |
|
return Boolean.TRUE; |
263 |
< |
} |
224 |
< |
}); |
225 |
< |
Thread t = new Thread(new Runnable() { |
226 |
< |
public void run() { |
227 |
< |
try { |
228 |
< |
ft.get(); |
229 |
< |
} catch (Exception e) { |
230 |
< |
threadUnexpectedException(); |
231 |
< |
} |
232 |
< |
} |
233 |
< |
}); |
234 |
< |
try { |
235 |
< |
assertFalse(ft.isDone()); |
236 |
< |
assertFalse(ft.isCancelled()); |
237 |
< |
t.start(); |
238 |
< |
Thread.sleep(SHORT_DELAY_MS); |
239 |
< |
ft.run(); |
240 |
< |
t.join(); |
241 |
< |
assertTrue(ft.isDone()); |
242 |
< |
assertFalse(ft.isCancelled()); |
243 |
< |
} catch (InterruptedException e) { |
244 |
< |
unexpectedException(); |
263 |
> |
}}); |
264 |
|
|
265 |
< |
} |
265 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
266 |
> |
public void realRun() throws Exception { |
267 |
> |
threadStarted.countDown(); |
268 |
> |
assertSame(Boolean.TRUE, task.get()); |
269 |
> |
}}); |
270 |
> |
|
271 |
> |
threadStarted.await(); |
272 |
> |
checkNotDone(task); |
273 |
> |
assertTrue(t.isAlive()); |
274 |
> |
task.run(); |
275 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
276 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
< |
* set in one thread causes timed get in another thread to retrieve value |
280 |
> |
* set in one thread causes get in another thread to retrieve value |
281 |
|
*/ |
282 |
< |
public void testTimedGet1() { |
283 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
284 |
< |
public Object call() { |
285 |
< |
try { |
286 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
287 |
< |
} catch (InterruptedException e) { |
258 |
< |
threadUnexpectedException(); |
259 |
< |
} |
282 |
> |
public void testGetSet() throws InterruptedException { |
283 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
284 |
> |
|
285 |
> |
final PublicFutureTask task = |
286 |
> |
new PublicFutureTask(new CheckedCallable<Object>() { |
287 |
> |
public Object realCall() throws InterruptedException { |
288 |
|
return Boolean.TRUE; |
289 |
< |
} |
262 |
< |
}); |
263 |
< |
Thread t = new Thread(new Runnable() { |
264 |
< |
public void run() { |
265 |
< |
try { |
266 |
< |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
267 |
< |
} catch (TimeoutException success) { |
268 |
< |
} catch (Exception e) { |
269 |
< |
threadUnexpectedException(); |
270 |
< |
} |
271 |
< |
} |
272 |
< |
}); |
273 |
< |
try { |
274 |
< |
assertFalse(ft.isDone()); |
275 |
< |
assertFalse(ft.isCancelled()); |
276 |
< |
t.start(); |
277 |
< |
ft.run(); |
278 |
< |
t.join(); |
279 |
< |
assertTrue(ft.isDone()); |
280 |
< |
assertFalse(ft.isCancelled()); |
281 |
< |
} catch (InterruptedException e) { |
282 |
< |
unexpectedException(); |
289 |
> |
}}); |
290 |
|
|
291 |
< |
} |
291 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
292 |
> |
public void realRun() throws Exception { |
293 |
> |
threadStarted.countDown(); |
294 |
> |
assertSame(Boolean.FALSE, task.get()); |
295 |
> |
}}); |
296 |
> |
|
297 |
> |
threadStarted.await(); |
298 |
> |
checkNotDone(task); |
299 |
> |
assertTrue(t.isAlive()); |
300 |
> |
task.set(Boolean.FALSE); |
301 |
> |
checkCompletedNormally(task, Boolean.FALSE); |
302 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* Cancelling a task causes timed get in another thread to throw CancellationException |
306 |
> |
* run in one thread causes timed get in another thread to retrieve value |
307 |
|
*/ |
308 |
< |
public void testTimedGet_Cancellation() { |
309 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
310 |
< |
public Object call() { |
311 |
< |
try { |
312 |
< |
Thread.sleep(SMALL_DELAY_MS); |
313 |
< |
threadShouldThrow(); |
314 |
< |
} catch (InterruptedException e) { |
315 |
< |
} |
316 |
< |
return Boolean.TRUE; |
317 |
< |
} |
318 |
< |
}); |
319 |
< |
try { |
320 |
< |
Thread t1 = new Thread(new Runnable() { |
321 |
< |
public void run() { |
322 |
< |
try { |
323 |
< |
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
324 |
< |
threadShouldThrow(); |
325 |
< |
} catch (CancellationException success) {} |
326 |
< |
catch (Exception e) { |
327 |
< |
threadUnexpectedException(); |
328 |
< |
} |
329 |
< |
} |
312 |
< |
}); |
313 |
< |
Thread t2 = new Thread(ft); |
314 |
< |
t1.start(); |
315 |
< |
t2.start(); |
316 |
< |
Thread.sleep(SHORT_DELAY_MS); |
317 |
< |
ft.cancel(true); |
318 |
< |
t1.join(); |
319 |
< |
t2.join(); |
320 |
< |
} catch (InterruptedException ie) { |
321 |
< |
unexpectedException(); |
322 |
< |
} |
308 |
> |
public void testTimedGetRun() throws InterruptedException { |
309 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
310 |
> |
|
311 |
> |
final FutureTask task = |
312 |
> |
new FutureTask(new CheckedCallable<Object>() { |
313 |
> |
public Object realCall() throws InterruptedException { |
314 |
> |
return Boolean.TRUE; |
315 |
> |
}}); |
316 |
> |
|
317 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
318 |
> |
public void realRun() throws Exception { |
319 |
> |
threadStarted.countDown(); |
320 |
> |
assertSame(Boolean.TRUE, |
321 |
> |
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
322 |
> |
}}); |
323 |
> |
|
324 |
> |
threadStarted.await(); |
325 |
> |
checkNotDone(task); |
326 |
> |
assertTrue(t.isAlive()); |
327 |
> |
task.run(); |
328 |
> |
checkCompletedNormally(task, Boolean.TRUE); |
329 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
< |
* Cancelling a task causes get in another thread to throw CancellationException |
333 |
> |
* set in one thread causes timed get in another thread to retrieve value |
334 |
|
*/ |
335 |
< |
public void testGet_Cancellation() { |
336 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
337 |
< |
public Object call() { |
338 |
< |
try { |
339 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
340 |
< |
threadShouldThrow(); |
334 |
< |
} catch (InterruptedException e) { |
335 |
< |
} |
335 |
> |
public void testTimedGetSet() throws InterruptedException { |
336 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
337 |
> |
|
338 |
> |
final PublicFutureTask task = |
339 |
> |
new PublicFutureTask(new CheckedCallable<Object>() { |
340 |
> |
public Object realCall() throws InterruptedException { |
341 |
|
return Boolean.TRUE; |
342 |
< |
} |
343 |
< |
}); |
344 |
< |
try { |
345 |
< |
Thread t1 = new Thread(new Runnable() { |
346 |
< |
public void run() { |
347 |
< |
try { |
348 |
< |
ft.get(); |
349 |
< |
threadShouldThrow(); |
350 |
< |
} catch (CancellationException success) { |
351 |
< |
} |
352 |
< |
catch (Exception e) { |
353 |
< |
threadUnexpectedException(); |
354 |
< |
} |
355 |
< |
} |
356 |
< |
}); |
357 |
< |
Thread t2 = new Thread(ft); |
358 |
< |
t1.start(); |
359 |
< |
t2.start(); |
360 |
< |
Thread.sleep(SHORT_DELAY_MS); |
361 |
< |
ft.cancel(true); |
362 |
< |
t1.join(); |
363 |
< |
t2.join(); |
364 |
< |
} catch (InterruptedException success) { |
365 |
< |
unexpectedException(); |
366 |
< |
} |
342 |
> |
}}); |
343 |
> |
|
344 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
345 |
> |
public void realRun() throws Exception { |
346 |
> |
threadStarted.countDown(); |
347 |
> |
assertSame(Boolean.FALSE, |
348 |
> |
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
349 |
> |
}}); |
350 |
> |
|
351 |
> |
threadStarted.await(); |
352 |
> |
checkNotDone(task); |
353 |
> |
assertTrue(t.isAlive()); |
354 |
> |
task.set(Boolean.FALSE); |
355 |
> |
checkCompletedNormally(task, Boolean.FALSE); |
356 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
357 |
> |
} |
358 |
> |
|
359 |
> |
/** |
360 |
> |
* Cancelling a task causes timed get in another thread to throw |
361 |
> |
* CancellationException |
362 |
> |
*/ |
363 |
> |
public void testTimedGet_Cancellation() throws InterruptedException { |
364 |
> |
final CountDownLatch threadStarted = new CountDownLatch(2); |
365 |
> |
final FutureTask task = |
366 |
> |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
367 |
> |
public Object realCall() throws InterruptedException { |
368 |
> |
threadStarted.countDown(); |
369 |
> |
Thread.sleep(LONG_DELAY_MS); |
370 |
> |
return Boolean.TRUE; |
371 |
> |
}}); |
372 |
> |
|
373 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
374 |
> |
public void realRun() throws Exception { |
375 |
> |
threadStarted.countDown(); |
376 |
> |
task.get(MEDIUM_DELAY_MS, MILLISECONDS); |
377 |
> |
}}; |
378 |
> |
Thread t2 = new Thread(task); |
379 |
> |
t1.start(); |
380 |
> |
t2.start(); |
381 |
> |
threadStarted.await(); |
382 |
> |
task.cancel(true); |
383 |
> |
awaitTermination(t1, MEDIUM_DELAY_MS); |
384 |
> |
awaitTermination(t2, MEDIUM_DELAY_MS); |
385 |
> |
checkCancelled(task); |
386 |
> |
} |
387 |
> |
|
388 |
> |
/** |
389 |
> |
* Cancelling a task causes get in another thread to throw |
390 |
> |
* CancellationException |
391 |
> |
*/ |
392 |
> |
public void testGet_Cancellation() throws InterruptedException { |
393 |
> |
final CountDownLatch threadStarted = new CountDownLatch(2); |
394 |
> |
final FutureTask task = |
395 |
> |
new FutureTask(new CheckedInterruptedCallable<Object>() { |
396 |
> |
public Object realCall() throws InterruptedException { |
397 |
> |
threadStarted.countDown(); |
398 |
> |
Thread.sleep(LONG_DELAY_MS); |
399 |
> |
return Boolean.TRUE; |
400 |
> |
}}); |
401 |
> |
|
402 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
403 |
> |
public void realRun() throws Exception { |
404 |
> |
threadStarted.countDown(); |
405 |
> |
task.get(); |
406 |
> |
}}; |
407 |
> |
Thread t2 = new Thread(task); |
408 |
> |
t1.start(); |
409 |
> |
t2.start(); |
410 |
> |
threadStarted.await(); |
411 |
> |
task.cancel(true); |
412 |
> |
awaitTermination(t1, MEDIUM_DELAY_MS); |
413 |
> |
awaitTermination(t2, MEDIUM_DELAY_MS); |
414 |
> |
checkCancelled(task); |
415 |
|
} |
416 |
|
|
417 |
|
|
418 |
|
/** |
419 |
|
* A runtime exception in task causes get to throw ExecutionException |
420 |
|
*/ |
421 |
< |
public void testGet_ExecutionException() { |
422 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
423 |
< |
public Object call() { |
424 |
< |
int i = 5/0; |
425 |
< |
return Boolean.TRUE; |
426 |
< |
} |
427 |
< |
}); |
428 |
< |
try { |
429 |
< |
ft.run(); |
430 |
< |
ft.get(); |
431 |
< |
shouldThrow(); |
432 |
< |
} catch (ExecutionException success) { |
421 |
> |
public void testGet_ExecutionException() throws InterruptedException { |
422 |
> |
final FutureTask task = new FutureTask(new Callable() { |
423 |
> |
public Object call() { |
424 |
> |
return 5/0; |
425 |
> |
}}); |
426 |
> |
|
427 |
> |
task.run(); |
428 |
> |
try { |
429 |
> |
task.get(); |
430 |
> |
shouldThrow(); |
431 |
> |
} catch (ExecutionException success) { |
432 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
433 |
> |
checkCompletedAbnormally(task, success.getCause()); |
434 |
|
} |
381 |
– |
catch (Exception e) { |
382 |
– |
unexpectedException(); |
383 |
– |
} |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
< |
* A runtime exception in task causes timed get to throw ExecutionException |
439 |
< |
*/ |
440 |
< |
public void testTimedGet_ExecutionException2() { |
441 |
< |
final FutureTask ft = new FutureTask(new Callable() { |
442 |
< |
public Object call() { |
443 |
< |
int i = 5/0; |
444 |
< |
return Boolean.TRUE; |
445 |
< |
} |
446 |
< |
}); |
447 |
< |
try { |
448 |
< |
ft.run(); |
449 |
< |
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
450 |
< |
shouldThrow(); |
451 |
< |
} catch (ExecutionException success) { |
452 |
< |
} catch (TimeoutException success) { } // unlikely but OK |
453 |
< |
catch (Exception e) { |
403 |
< |
unexpectedException(); |
404 |
< |
} |
438 |
> |
* A runtime exception in task causes timed get to throw ExecutionException |
439 |
> |
*/ |
440 |
> |
public void testTimedGet_ExecutionException2() throws Exception { |
441 |
> |
final FutureTask task = new FutureTask(new Callable() { |
442 |
> |
public Object call() { |
443 |
> |
return 5/0; |
444 |
> |
}}); |
445 |
> |
|
446 |
> |
task.run(); |
447 |
> |
try { |
448 |
> |
task.get(SHORT_DELAY_MS, MILLISECONDS); |
449 |
> |
shouldThrow(); |
450 |
> |
} catch (ExecutionException success) { |
451 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
452 |
> |
checkCompletedAbnormally(task, success.getCause()); |
453 |
> |
} |
454 |
|
} |
455 |
|
|
456 |
|
|
457 |
|
/** |
458 |
|
* Interrupting a waiting get causes it to throw InterruptedException |
459 |
|
*/ |
460 |
< |
public void testGet_InterruptedException() { |
461 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
462 |
< |
Thread t = new Thread(new Runnable() { |
463 |
< |
public void run() { |
464 |
< |
try { |
465 |
< |
ft.get(); |
466 |
< |
threadShouldThrow(); |
467 |
< |
} catch (InterruptedException success) { |
468 |
< |
} catch (Exception e) { |
469 |
< |
threadUnexpectedException(); |
470 |
< |
} |
471 |
< |
} |
472 |
< |
}); |
424 |
< |
try { |
425 |
< |
t.start(); |
426 |
< |
Thread.sleep(SHORT_DELAY_MS); |
427 |
< |
t.interrupt(); |
428 |
< |
t.join(); |
429 |
< |
} catch (Exception e) { |
430 |
< |
unexpectedException(); |
431 |
< |
} |
460 |
> |
public void testGet_InterruptedException() throws InterruptedException { |
461 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
462 |
> |
final FutureTask task = new FutureTask(new NoOpCallable()); |
463 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
464 |
> |
public void realRun() throws Exception { |
465 |
> |
threadStarted.countDown(); |
466 |
> |
task.get(); |
467 |
> |
}}); |
468 |
> |
|
469 |
> |
threadStarted.await(); |
470 |
> |
t.interrupt(); |
471 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
472 |
> |
checkNotDone(task); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
< |
* Interrupting a waiting timed get causes it to throw InterruptedException |
477 |
< |
*/ |
478 |
< |
public void testTimedGet_InterruptedException2() { |
479 |
< |
final FutureTask ft = new FutureTask(new NoOpCallable()); |
480 |
< |
Thread t = new Thread(new Runnable() { |
481 |
< |
public void run() { |
482 |
< |
try { |
483 |
< |
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
484 |
< |
threadShouldThrow(); |
485 |
< |
} catch (InterruptedException success) {} |
486 |
< |
catch (Exception e) { |
487 |
< |
threadUnexpectedException(); |
488 |
< |
} |
489 |
< |
} |
490 |
< |
}); |
450 |
< |
try { |
451 |
< |
t.start(); |
452 |
< |
Thread.sleep(SHORT_DELAY_MS); |
453 |
< |
t.interrupt(); |
454 |
< |
t.join(); |
455 |
< |
} catch (Exception e) { |
456 |
< |
unexpectedException(); |
457 |
< |
} |
476 |
> |
* Interrupting a waiting timed get causes it to throw InterruptedException |
477 |
> |
*/ |
478 |
> |
public void testTimedGet_InterruptedException2() throws InterruptedException { |
479 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
480 |
> |
final FutureTask task = new FutureTask(new NoOpCallable()); |
481 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
482 |
> |
public void realRun() throws Exception { |
483 |
> |
threadStarted.countDown(); |
484 |
> |
task.get(LONG_DELAY_MS, MILLISECONDS); |
485 |
> |
}}); |
486 |
> |
|
487 |
> |
threadStarted.await(); |
488 |
> |
t.interrupt(); |
489 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
490 |
> |
checkNotDone(task); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
|
* A timed out timed get throws TimeoutException |
495 |
|
*/ |
496 |
< |
public void testGet_TimeoutException() { |
497 |
< |
try { |
498 |
< |
FutureTask ft = new FutureTask(new NoOpCallable()); |
499 |
< |
ft.get(1,TimeUnit.MILLISECONDS); |
500 |
< |
shouldThrow(); |
501 |
< |
} catch (TimeoutException success) {} |
469 |
< |
catch (Exception success) { |
470 |
< |
unexpectedException(); |
471 |
< |
} |
496 |
> |
public void testGet_TimeoutException() throws Exception { |
497 |
> |
try { |
498 |
> |
FutureTask task = new FutureTask(new NoOpCallable()); |
499 |
> |
task.get(1, MILLISECONDS); |
500 |
> |
shouldThrow(); |
501 |
> |
} catch (TimeoutException success) {} |
502 |
|
} |
503 |
|
|
504 |
|
} |