ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
Revision: 1.4
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.3: +100 -50 lines
Log Message:
New base class JSR166TestCase

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