[cvs] / jsr166 / src / test / tck / FutureTaskTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/FutureTaskTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (view) (download)

1 : dl 1.1 /*
2 : dl 1.10 * Written by Doug Lea with assistance from members of JCP JSR-166
3 :     * Expert Group and released to the public domain, as explained at
4 :     * http://creativecommons.org/licenses/publicdomain
5 : jsr166 1.12 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : dl 1.1 */
8 :    
9 :     import junit.framework.*;
10 :     import java.util.concurrent.*;
11 : jsr166 1.15 import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 : dl 1.4 import java.util.*;
13 : dl 1.1
14 : dl 1.4 public class FutureTaskTest extends JSR166TestCase {
15 : dl 1.1
16 :     public static void main(String[] args) {
17 : jsr166 1.19 junit.textui.TestRunner.run(suite());
18 : dl 1.1 }
19 :     public static Test suite() {
20 : jsr166 1.16 return new TestSuite(FutureTaskTest.class);
21 : dl 1.1 }
22 :    
23 : dl 1.4 /**
24 :     * Subclass to expose protected methods
25 :     */
26 : dl 1.7 static class PublicFutureTask extends FutureTask {
27 :     public PublicFutureTask(Callable r) { super(r); }
28 : dl 1.9 public boolean runAndReset() { return super.runAndReset(); }
29 : dl 1.4 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 : dl 1.6 * Creating a future with a null callable throws NPE
35 : dl 1.5 */
36 :     public void testConstructor() {
37 : dl 1.3 try {
38 :     FutureTask task = new FutureTask(null);
39 : jsr166 1.17 shouldThrow();
40 : jsr166 1.15 } catch (NullPointerException success) {}
41 : dl 1.3 }
42 :    
43 : dl 1.5 /**
44 : dl 1.6 * creating a future with null runnable fails
45 : dl 1.5 */
46 :     public void testConstructor2() {
47 : dl 1.3 try {
48 :     FutureTask task = new FutureTask(null, Boolean.TRUE);
49 : jsr166 1.17 shouldThrow();
50 : jsr166 1.15 } catch (NullPointerException success) {}
51 : dl 1.3 }
52 :    
53 : dl 1.5 /**
54 : dl 1.6 * isDone is true when a task completes
55 : dl 1.5 */
56 :     public void testIsDone() {
57 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
58 : jsr166 1.16 task.run();
59 :     assertTrue(task.isDone());
60 :     assertFalse(task.isCancelled());
61 : dl 1.4 }
62 :    
63 : dl 1.5 /**
64 : dl 1.9 * runAndReset of a non-cancelled task succeeds
65 : dl 1.5 */
66 : dl 1.9 public void testRunAndReset() {
67 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
68 : jsr166 1.16 assertTrue(task.runAndReset());
69 : dl 1.6 assertFalse(task.isDone());
70 : dl 1.4 }
71 :    
72 : dl 1.5 /**
73 : dl 1.9 * runAndReset after cancellation fails
74 : dl 1.5 */
75 : dl 1.4 public void testResetAfterCancel() {
76 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
77 : dl 1.4 assertTrue(task.cancel(false));
78 : jsr166 1.16 assertFalse(task.runAndReset());
79 :     assertTrue(task.isDone());
80 :     assertTrue(task.isCancelled());
81 : dl 1.4 }
82 :    
83 : dl 1.1
84 : dl 1.4
85 : dl 1.5 /**
86 : dl 1.11 * setting value causes get to return it
87 : dl 1.5 */
88 : jsr166 1.15 public void testSet() throws Exception {
89 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
90 : dl 1.4 task.set(one);
91 : jsr166 1.18 assertSame(task.get(), one);
92 : dl 1.4 }
93 :    
94 : dl 1.5 /**
95 : dl 1.6 * setException causes get to throw ExecutionException
96 : dl 1.5 */
97 : jsr166 1.15 public void testSetException() throws Exception {
98 : dl 1.4 Exception nse = new NoSuchElementException();
99 : dl 1.7 PublicFutureTask task = new PublicFutureTask(new NoOpCallable());
100 : dl 1.4 task.setException(nse);
101 :     try {
102 :     Object x = task.get();
103 : jsr166 1.17 shouldThrow();
104 : jsr166 1.15 } catch (ExecutionException success) {
105 :     assertSame(success.getCause(), nse);
106 : dl 1.4 }
107 :     }
108 :    
109 : dl 1.5 /**
110 : dl 1.6 * Cancelling before running succeeds
111 : dl 1.5 */
112 : dl 1.1 public void testCancelBeforeRun() {
113 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
114 : dl 1.1 assertTrue(task.cancel(false));
115 : jsr166 1.16 task.run();
116 :     assertTrue(task.isDone());
117 :     assertTrue(task.isCancelled());
118 : dl 1.1 }
119 :    
120 : dl 1.5 /**
121 : dl 1.6 * Cancel(true) before run succeeds
122 : dl 1.5 */
123 : dl 1.1 public void testCancelBeforeRun2() {
124 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
125 : dl 1.1 assertTrue(task.cancel(true));
126 : jsr166 1.16 task.run();
127 :     assertTrue(task.isDone());
128 :     assertTrue(task.isCancelled());
129 : dl 1.1 }
130 :    
131 : dl 1.5 /**
132 : dl 1.6 * cancel of a completed task fails
133 : dl 1.5 */
134 : dl 1.1 public void testCancelAfterRun() {
135 : jsr166 1.15 FutureTask task = new FutureTask(new NoOpCallable());
136 : jsr166 1.16 task.run();
137 : dl 1.1 assertFalse(task.cancel(false));
138 : jsr166 1.16 assertTrue(task.isDone());
139 :     assertFalse(task.isCancelled());
140 : dl 1.1 }
141 :    
142 : dl 1.5 /**
143 : dl 1.6 * cancel(true) interrupts a running task
144 : dl 1.5 */
145 : jsr166 1.15 public void testCancelInterrupt() throws InterruptedException {
146 : jsr166 1.16 final FutureTask task =
147 : jsr166 1.15 new FutureTask(new CheckedInterruptedCallable<Object>() {
148 : jsr166 1.16 public Object realCall() throws InterruptedException {
149 : jsr166 1.15 Thread.sleep(SMALL_DELAY_MS);
150 : jsr166 1.16 return Boolean.TRUE;
151 : jsr166 1.15 }});
152 :    
153 :     Thread t = new Thread(task);
154 : dl 1.1 t.start();
155 : jsr166 1.15 Thread.sleep(SHORT_DELAY_MS);
156 :     assertTrue(task.cancel(true));
157 :     t.join();
158 :     assertTrue(task.isDone());
159 :     assertTrue(task.isCancelled());
160 : dl 1.1 }
161 :    
162 :    
163 : dl 1.5 /**
164 : dl 1.6 * cancel(false) does not interrupt a running task
165 : dl 1.5 */
166 : jsr166 1.15 public void testCancelNoInterrupt() throws InterruptedException {
167 : jsr166 1.16 final FutureTask task =
168 : jsr166 1.15 new FutureTask(new CheckedCallable<Object>() {
169 : jsr166 1.16 public Object realCall() throws InterruptedException {
170 : jsr166 1.15 Thread.sleep(MEDIUM_DELAY_MS);
171 : dl 1.1 return Boolean.TRUE;
172 : jsr166 1.15 }});
173 :    
174 :     Thread t = new Thread(task);
175 : dl 1.1 t.start();
176 : jsr166 1.15 Thread.sleep(SHORT_DELAY_MS);
177 :     assertTrue(task.cancel(false));
178 :     t.join();
179 :     assertTrue(task.isDone());
180 :     assertTrue(task.isCancelled());
181 : dl 1.1 }
182 :    
183 : dl 1.5 /**
184 : dl 1.6 * set in one thread causes get in another thread to retrieve value
185 : dl 1.5 */
186 : jsr166 1.15 public void testGet1() throws InterruptedException {
187 : jsr166 1.16 final FutureTask ft =
188 : jsr166 1.15 new FutureTask(new CheckedCallable<Object>() {
189 : jsr166 1.16 public Object realCall() throws InterruptedException {
190 : dl 1.1 return Boolean.TRUE;
191 : jsr166 1.15 }});
192 : jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
193 : jsr166 1.15 public void realRun() throws Exception {
194 :     assertSame(Boolean.TRUE, ft.get());
195 :     }});
196 : dl 1.1
197 : jsr166 1.15 assertFalse(ft.isDone());
198 :     assertFalse(ft.isCancelled());
199 :     t.start();
200 :     Thread.sleep(SHORT_DELAY_MS);
201 :     ft.run();
202 :     t.join();
203 :     assertTrue(ft.isDone());
204 :     assertFalse(ft.isCancelled());
205 : dl 1.1 }
206 :    
207 : dl 1.5 /**
208 : dl 1.6 * set in one thread causes timed get in another thread to retrieve value
209 : dl 1.5 */
210 : jsr166 1.15 public void testTimedGet1() throws InterruptedException {
211 : jsr166 1.16 final FutureTask ft =
212 : jsr166 1.15 new FutureTask(new CheckedCallable<Object>() {
213 : jsr166 1.16 public Object realCall() throws InterruptedException {
214 : dl 1.1 return Boolean.TRUE;
215 : jsr166 1.15 }});
216 : jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
217 : jsr166 1.15 public void realRun() throws Exception {
218 :     assertSame(Boolean.TRUE, ft.get(SMALL_DELAY_MS, MILLISECONDS));
219 :     }});
220 : jsr166 1.12
221 : jsr166 1.15 assertFalse(ft.isDone());
222 :     assertFalse(ft.isCancelled());
223 :     t.start();
224 :     Thread.sleep(SHORT_DELAY_MS);
225 :     ft.run();
226 :     t.join();
227 :     assertTrue(ft.isDone());
228 :     assertFalse(ft.isCancelled());
229 : dl 1.1 }
230 :    
231 : dl 1.5 /**
232 : dl 1.6 * Cancelling a task causes timed get in another thread to throw CancellationException
233 : dl 1.5 */
234 : jsr166 1.15 public void testTimedGet_Cancellation() throws InterruptedException {
235 : jsr166 1.16 final FutureTask ft =
236 : jsr166 1.15 new FutureTask(new CheckedInterruptedCallable<Object>() {
237 : jsr166 1.16 public Object realCall() throws InterruptedException {
238 : jsr166 1.15 Thread.sleep(SMALL_DELAY_MS);
239 : jsr166 1.16 return Boolean.TRUE;
240 : jsr166 1.15 }});
241 :    
242 :     Thread t1 = new ThreadShouldThrow(CancellationException.class) {
243 :     public void realRun() throws Exception {
244 :     ft.get(MEDIUM_DELAY_MS, MILLISECONDS);
245 :     }};
246 :     Thread t2 = new Thread(ft);
247 :     t1.start();
248 :     t2.start();
249 :     Thread.sleep(SHORT_DELAY_MS);
250 :     ft.cancel(true);
251 :     t1.join();
252 :     t2.join();
253 : dl 1.1 }
254 : dl 1.6
255 : dl 1.5 /**
256 : dl 1.6 * Cancelling a task causes get in another thread to throw CancellationException
257 : dl 1.5 */
258 : jsr166 1.15 public void testGet_Cancellation() throws InterruptedException {
259 : jsr166 1.16 final FutureTask ft =
260 : jsr166 1.15 new FutureTask(new CheckedInterruptedCallable<Object>() {
261 : jsr166 1.16 public Object realCall() throws InterruptedException {
262 : jsr166 1.15 Thread.sleep(SMALL_DELAY_MS);
263 : jsr166 1.16 return Boolean.TRUE;
264 : jsr166 1.15 }});
265 :     Thread t1 = new ThreadShouldThrow(CancellationException.class) {
266 :     public void realRun() throws Exception {
267 :     ft.get();
268 :     }};
269 :    
270 :     Thread t2 = new Thread(ft);
271 :     t1.start();
272 :     t2.start();
273 :     Thread.sleep(SHORT_DELAY_MS);
274 :     ft.cancel(true);
275 :     t1.join();
276 :     t2.join();
277 : dl 1.1 }
278 : jsr166 1.12
279 : dl 1.1
280 : dl 1.5 /**
281 : dl 1.6 * A runtime exception in task causes get to throw ExecutionException
282 : dl 1.5 */
283 : jsr166 1.15 public void testGet_ExecutionException() throws InterruptedException {
284 : jsr166 1.16 final FutureTask ft = new FutureTask(new Callable() {
285 :     public Object call() {
286 : jsr166 1.15 return 5/0;
287 :     }});
288 :    
289 :     ft.run();
290 :     try {
291 : jsr166 1.16 ft.get();
292 : jsr166 1.17 shouldThrow();
293 : jsr166 1.16 } catch (ExecutionException success) {
294 : jsr166 1.15 assertTrue(success.getCause() instanceof ArithmeticException);
295 : dl 1.1 }
296 :     }
297 : jsr166 1.12
298 : dl 1.5 /**
299 : dl 1.6 * A runtime exception in task causes timed get to throw ExecutionException
300 : dl 1.5 */
301 : jsr166 1.15 public void testTimedGet_ExecutionException2() throws Exception {
302 : jsr166 1.16 final FutureTask ft = new FutureTask(new Callable() {
303 :     public Object call() {
304 : jsr166 1.15 return 5/0;
305 :     }});
306 :    
307 :     ft.run();
308 : jsr166 1.16 try {
309 :     ft.get(SHORT_DELAY_MS, MILLISECONDS);
310 : jsr166 1.17 shouldThrow();
311 : jsr166 1.16 } catch (ExecutionException success) {
312 : jsr166 1.15 assertTrue(success.getCause() instanceof ArithmeticException);
313 :     }
314 : dl 1.1 }
315 : jsr166 1.12
316 : dl 1.1
317 : dl 1.5 /**
318 : dl 1.6 * Interrupting a waiting get causes it to throw InterruptedException
319 : dl 1.5 */
320 : jsr166 1.15 public void testGet_InterruptedException() throws InterruptedException {
321 : jsr166 1.16 final FutureTask ft = new FutureTask(new NoOpCallable());
322 :     Thread t = new Thread(new CheckedInterruptedRunnable() {
323 : jsr166 1.15 public void realRun() throws Exception {
324 :     ft.get();
325 :     }});
326 :    
327 :     t.start();
328 :     Thread.sleep(SHORT_DELAY_MS);
329 :     t.interrupt();
330 :     t.join();
331 : dl 1.1 }
332 :    
333 : dl 1.5 /**
334 : dl 1.6 * Interrupting a waiting timed get causes it to throw InterruptedException
335 : dl 1.5 */
336 : jsr166 1.15 public void testTimedGet_InterruptedException2() throws InterruptedException {
337 : jsr166 1.16 final FutureTask ft = new FutureTask(new NoOpCallable());
338 :     Thread t = new Thread(new CheckedInterruptedRunnable() {
339 : jsr166 1.15 public void realRun() throws Exception {
340 :     ft.get(LONG_DELAY_MS,MILLISECONDS);
341 :     }});
342 :    
343 :     t.start();
344 :     Thread.sleep(SHORT_DELAY_MS);
345 :     t.interrupt();
346 :     t.join();
347 : dl 1.1 }
348 : jsr166 1.12
349 : dl 1.5 /**
350 : dl 1.6 * A timed out timed get throws TimeoutException
351 : dl 1.5 */
352 : jsr166 1.15 public void testGet_TimeoutException() throws Exception {
353 : jsr166 1.16 try {
354 : dl 1.4 FutureTask ft = new FutureTask(new NoOpCallable());
355 : jsr166 1.16 ft.get(1,MILLISECONDS);
356 : jsr166 1.17 shouldThrow();
357 : jsr166 1.16 } catch (TimeoutException success) {}
358 : dl 1.1 }
359 : jsr166 1.12
360 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8