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

File Contents

# User Rev Content
1 dl 1.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.*;
10     import java.util.concurrent.*;
11    
12     public class CancellableTaskTest extends TestCase{
13     public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(CancellableTaskTest.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    
25     public void testIsDone(){
26     CancellableTask task = new CancellableTask(new Runnable() {
27     public void run() {} });
28     task.run();
29     assertTrue(task.isDone());
30     assertFalse(task.isCancelled());
31     }
32    
33     public void testCancelBeforeRun() {
34     CancellableTask task = new CancellableTask(new Runnable() {
35     public void run() {} });
36     assertTrue(task.cancel(false));
37     task.run();
38     assertTrue(task.isDone());
39     assertTrue(task.isCancelled());
40     }
41    
42     public void testCancelBeforeRun2() {
43     CancellableTask task = new CancellableTask(new Runnable() {
44     public void run() {} });
45     assertTrue(task.cancel(true));
46     task.run();
47     assertTrue(task.isDone());
48     assertTrue(task.isCancelled());
49     }
50    
51     public void testCancelAfterRun() {
52     CancellableTask task = new CancellableTask(new Runnable() {
53     public void run() {} });
54     task.run();
55     assertFalse(task.cancel(false));
56     assertTrue(task.isDone());
57     assertFalse(task.isCancelled());
58     }
59    
60     public void testCancelInterrupt(){
61     CancellableTask task = new CancellableTask(new Runnable() {
62     public void run() {
63     try {
64     Thread.sleep(SHORT_DELAY_MS* 2);
65     fail("should throw");
66     }
67     catch (InterruptedException success) {}
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     CancellableTask task = new CancellableTask(new Runnable() {
86     public void run() {
87     try {
88     Thread.sleep(SHORT_DELAY_MS* 2);
89     }
90     catch (InterruptedException success) {
91     fail("should not interrupt");
92     }
93     } });
94     Thread t = new Thread(task);
95     t.start();
96    
97     try{
98     Thread.sleep(SHORT_DELAY_MS);
99     assertTrue(task.cancel(false));
100     t.join();
101     assertTrue(task.isDone());
102     assertTrue(task.isCancelled());
103     } catch(InterruptedException e){
104     fail("unexpected exception");
105     }
106     }
107    
108    
109     }