ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
Revision: 1.8
Committed: Mon Dec 22 00:48:55 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.7: +0 -10 lines
Log Message:
Add and adjust tests reflecting API changes

File Contents

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