ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.4: +158 -89 lines
Log Message:
Documentation scaffolding

File Contents

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