ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
Revision: 1.3
Committed: Fri Sep 12 15:40:25 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +18 -0 lines
Log Message:
test for null runnables

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
11 public class FutureTaskTest extends TestCase {
12
13 public static void main(String[] args) {
14 junit.textui.TestRunner.run (suite());
15 }
16 public static Test suite() {
17 return new TestSuite(FutureTaskTest.class);
18 }
19
20 private static long SHORT_DELAY_MS = 100;
21 private static long MEDIUM_DELAY_MS = 1000;
22 private static long LONG_DELAY_MS = 10000;
23
24 public void testConstructor(){
25 try {
26 FutureTask task = new FutureTask(null);
27 fail("should throw");
28 }
29 catch(NullPointerException success) {
30 }
31 }
32
33 public void testConstructor2(){
34 try {
35 FutureTask task = new FutureTask(null, Boolean.TRUE);
36 fail("should throw");
37 }
38 catch(NullPointerException success) {
39 }
40 }
41
42 public void testIsDone(){
43 FutureTask task = new FutureTask( new Callable() {
44 public Object call() { return Boolean.TRUE; } });
45 task.run();
46 assertTrue(task.isDone());
47 assertFalse(task.isCancelled());
48 }
49
50 public void testCancelBeforeRun() {
51 FutureTask task = new FutureTask( new Callable() {
52 public Object call() { return Boolean.TRUE; } });
53 assertTrue(task.cancel(false));
54 task.run();
55 assertTrue(task.isDone());
56 assertTrue(task.isCancelled());
57 }
58
59 public void testCancelBeforeRun2() {
60 FutureTask task = new FutureTask( new Callable() {
61 public Object call() { return Boolean.TRUE; } });
62 assertTrue(task.cancel(true));
63 task.run();
64 assertTrue(task.isDone());
65 assertTrue(task.isCancelled());
66 }
67
68 public void testCancelAfterRun() {
69 FutureTask task = new FutureTask( new Callable() {
70 public Object call() { return Boolean.TRUE; } });
71 task.run();
72 assertFalse(task.cancel(false));
73 assertTrue(task.isDone());
74 assertFalse(task.isCancelled());
75 }
76
77 public void testCancelInterrupt(){
78 FutureTask task = new FutureTask( new Callable() {
79 public Object call() {
80 try {
81 Thread.sleep(SHORT_DELAY_MS* 2);
82 fail("should throw");
83 }
84 catch (InterruptedException success) {}
85 return Boolean.TRUE;
86 } });
87 Thread t = new Thread(task);
88 t.start();
89
90 try{
91 Thread.sleep(SHORT_DELAY_MS);
92 assertTrue(task.cancel(true));
93 t.join();
94 assertTrue(task.isDone());
95 assertTrue(task.isCancelled());
96 } catch(InterruptedException e){
97 fail("unexpected exception");
98 }
99 }
100
101
102 public void testCancelNoInterrupt(){
103 FutureTask task = new FutureTask( new Callable() {
104 public Object call() {
105 try {
106 Thread.sleep(SHORT_DELAY_MS* 2);
107 }
108 catch (InterruptedException success) {
109 fail("should not interrupt");
110 }
111 return Boolean.TRUE;
112 } });
113 Thread t = new Thread(task);
114 t.start();
115
116 try{
117 Thread.sleep(SHORT_DELAY_MS);
118 assertTrue(task.cancel(false));
119 t.join();
120 assertTrue(task.isDone());
121 assertTrue(task.isCancelled());
122 } catch(InterruptedException e){
123 fail("unexpected exception");
124 }
125 }
126
127 public void testGet1() {
128 final FutureTask ft = new FutureTask(new Callable(){
129 public Object call(){
130 try{
131 Thread.sleep(MEDIUM_DELAY_MS);
132 } catch(InterruptedException e){
133 fail("unexpected exception");
134 }
135 return Boolean.TRUE;
136 }
137 });
138 Thread t = new Thread(new Runnable(){
139 public void run(){
140 try{
141 ft.get();
142 } catch(Exception e){
143 fail("unexpected exception");
144 }
145 }
146 });
147 try{
148 assertFalse(ft.isDone());
149 assertFalse(ft.isCancelled());
150 t.start();
151 Thread.sleep(SHORT_DELAY_MS);
152 ft.run();
153 t.join();
154 assertTrue(ft.isDone());
155 assertFalse(ft.isCancelled());
156 } catch(InterruptedException e){
157 fail("unexpected exception");
158
159 }
160 }
161
162 public void testTimedGet1() {
163 final FutureTask ft = new FutureTask(new Callable(){
164 public Object call(){
165 try{
166 Thread.sleep(MEDIUM_DELAY_MS);
167 } catch(InterruptedException e){
168 fail("unexpected exception");
169 }
170 return Boolean.TRUE;
171 }
172 });
173 Thread t = new Thread(new Runnable(){
174 public void run(){
175 try{
176 ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
177 } catch(TimeoutException success) {
178 } catch(Exception e){
179 fail("unexpected exception");
180 }
181 }
182 });
183 try{
184 assertFalse(ft.isDone());
185 assertFalse(ft.isCancelled());
186 t.start();
187 ft.run();
188 t.join();
189 assertTrue(ft.isDone());
190 assertFalse(ft.isCancelled());
191 } catch(InterruptedException e){
192 fail("unexpected exception");
193
194 }
195 }
196
197
198 public void testGet_Cancellation(){
199 final FutureTask ft = new FutureTask(new Callable(){
200 public Object call(){
201 try{
202 Thread.sleep(MEDIUM_DELAY_MS);
203 } catch(InterruptedException e){
204 fail("unexpected exception");
205 }
206 return Boolean.TRUE;
207 }
208 });
209 try {
210 Thread.sleep(SHORT_DELAY_MS);
211 Thread t = new Thread(new Runnable(){
212 public void run(){
213 try{
214 ft.get();
215 fail("should throw");
216 } catch(CancellationException success){
217 }
218 catch(Exception e){
219 fail("unexpected exception");
220 }
221 }
222 });
223 t.start();
224 ft.cancel(true);
225 t.join();
226 } catch(InterruptedException success){
227 fail("unexpected exception");
228 }
229 }
230
231 public void testGet_Cancellation2(){
232 final FutureTask ft = new FutureTask(new Callable(){
233 public Object call(){
234 try{
235 Thread.sleep(SHORT_DELAY_MS);
236 } catch(InterruptedException e) {
237 fail("unexpected exception");
238 }
239 return Boolean.TRUE;
240 }
241 });
242 try{
243 Thread.sleep(100);
244 Thread t = new Thread(new Runnable(){
245 public void run(){
246 try{
247 ft.get(3 * SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
248 fail("should throw");
249 } catch(CancellationException success) {}
250 catch(Exception e){
251 fail("unexpected exception");
252 }
253 }
254 });
255 t.start();
256 Thread.sleep(SHORT_DELAY_MS);
257 ft.cancel(true);
258 Thread.sleep(SHORT_DELAY_MS);
259 t.join();
260 } catch(InterruptedException ie){
261 fail("unexpected exception");
262 }
263 }
264
265 public void testGet_ExecutionException(){
266 final FutureTask ft = new FutureTask(new Callable(){
267 public Object call(){
268 int i = 5/0;
269 return Boolean.TRUE;
270 }
271 });
272 try{
273 ft.run();
274 ft.get();
275 fail("should throw");
276 } catch(ExecutionException success){
277 }
278 catch(Exception e){
279 fail("unexpected exception");
280 }
281 }
282
283 public void testTimedGet_ExecutionException2(){
284 final FutureTask ft = new FutureTask(new Callable(){
285 public Object call(){
286 int i = 5/0;
287 return Boolean.TRUE;
288 }
289 });
290 try{
291 ft.run();
292 ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
293 fail("should throw");
294 } catch(ExecutionException success) {
295 } catch(TimeoutException success) { } // unlikely but OK
296 catch(Exception e){
297 fail("unexpected exception");
298 }
299 }
300
301
302 public void testGet_InterruptedException(){
303 final FutureTask ft = new FutureTask(new Callable(){
304 public Object call(){
305 return new Object();
306 }
307 });
308 Thread t = new Thread(new Runnable(){
309 public void run(){
310 try{
311 ft.get();
312 fail("should throw");
313 } catch(InterruptedException success){
314 } catch(Exception e){
315 fail("unexpected exception");
316 }
317 }
318 });
319 try {
320 t.start();
321 Thread.sleep(SHORT_DELAY_MS);
322 t.interrupt();
323 t.join();
324 } catch(Exception e){
325 fail("unexpected exception");
326 }
327 }
328
329 public void testTimedGet_InterruptedException2(){
330 final FutureTask ft = new FutureTask(new Callable(){
331 public Object call(){
332 return new Object();
333 }
334 });
335 Thread t = new Thread(new Runnable(){
336 public void run(){
337 try{
338 ft.get(100,TimeUnit.SECONDS);
339 fail("should throw");
340 } catch(InterruptedException success){}
341 catch(Exception e){
342 fail("unexpected exception");
343 }
344 }
345 });
346 try {
347 t.start();
348 Thread.sleep(SHORT_DELAY_MS);
349 t.interrupt();
350 t.join();
351 } catch(Exception e){
352 fail("unexpected exception");
353 }
354 }
355
356 public void testGet_TimeoutException(){
357 FutureTask ft = new FutureTask(new Callable(){
358 public Object call(){
359 return new Object();
360 }
361 });
362 try{
363 ft.get(1,TimeUnit.MILLISECONDS);
364 fail("should throw");
365 } catch(TimeoutException success){}
366 catch(Exception success){
367 fail("unexpected exception");
368 }
369
370
371 }
372
373 }