ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/FutureTaskTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:55 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

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