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, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +158 -89 lines
Log Message:
Documentation scaffolding

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