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