ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CancellableTaskTest.java
Revision: 1.7
Committed: Thu Dec 4 20:54:46 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +0 -0 lines
State: FILE REMOVED
Log Message:
Collapsed Cancellable and Future

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 dl 1.3 public class CancellableTaskTest extends JSR166TestCase {
13 dl 1.1 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 dl 1.3 /**
21     * Subclass to expose protected methods
22     */
23 dl 1.6 static class PublicCancellableTask extends CancellableTask {
24     public PublicCancellableTask() {}
25     public PublicCancellableTask(Runnable r) { super(r); }
26 dl 1.3 public boolean reset() { return super.reset(); }
27     public Runnable getRunnable() { return super.getRunnable(); }
28     public void setRunnable(Runnable r) { super.setRunnable(r); }
29     public void setCancelled() { super.setCancelled(); }
30     public void setDone() { super.setDone(); }
31     }
32 dl 1.2
33 dl 1.4 /**
34 dl 1.5 * creating task with null runnable throws NPE
35 dl 1.4 */
36 dl 1.2 public void testConstructor(){
37     try {
38     CancellableTask task = new CancellableTask(null);
39 dl 1.4 shouldThrow();
40 dl 1.2 }
41     catch(NullPointerException success) {
42     }
43     }
44 dl 1.1
45 dl 1.4 /**
46 dl 1.5 * isDone is true after task is run
47 dl 1.4 */
48 dl 1.1 public void testIsDone(){
49 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
50 dl 1.1 task.run();
51     assertTrue(task.isDone());
52     assertFalse(task.isCancelled());
53     }
54    
55 dl 1.4 /**
56 dl 1.5 * Cancelling before running succeeds
57 dl 1.4 */
58 dl 1.5 public void testCancelBeforeRun() {
59     CancellableTask task = new CancellableTask(new NoOpRunnable());
60     assertTrue(task.cancel(false));
61 dl 1.3 task.run();
62     assertTrue(task.isDone());
63 dl 1.5 assertTrue(task.isCancelled());
64 dl 1.3 }
65    
66 dl 1.4 /**
67 dl 1.5 * reset of a done task succeeds and changes status to not done
68 dl 1.4 */
69 dl 1.5 public void testReset(){
70 dl 1.6 PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable());
71 dl 1.3 task.run();
72     assertTrue(task.isDone());
73 dl 1.5 assertTrue(task.reset());
74     assertFalse(task.isDone());
75 dl 1.3 }
76    
77 dl 1.4 /**
78 dl 1.5 * Resetting after cancellation fails
79 dl 1.4 */
80 dl 1.3 public void testResetAfterCancel() {
81 dl 1.6 PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable());
82 dl 1.3 assertTrue(task.cancel(false));
83     task.run();
84     assertTrue(task.isDone());
85     assertTrue(task.isCancelled());
86     assertFalse(task.reset());
87     }
88    
89 dl 1.4 /**
90 dl 1.5 * Setting the runnable causes it to be used
91 dl 1.4 */
92 dl 1.3 public void testSetRunnable() {
93 dl 1.6 PublicCancellableTask task = new PublicCancellableTask();
94 dl 1.3 assertNull(task.getRunnable());
95     Runnable r = new NoOpRunnable();
96     task.setRunnable(r);
97     assertEquals(r, task.getRunnable());
98 dl 1.1 assertTrue(task.cancel(false));
99     task.run();
100     assertTrue(task.isDone());
101     assertTrue(task.isCancelled());
102 dl 1.3 assertFalse(task.reset());
103     }
104    
105 dl 1.4 /**
106 dl 1.5 * setDone of new task causes isDone to be true
107 dl 1.4 */
108 dl 1.3 public void testSetDone() {
109 dl 1.6 PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable());
110 dl 1.3 task.setDone();
111     assertTrue(task.isDone());
112     assertFalse(task.isCancelled());
113     }
114    
115 dl 1.4 /**
116 dl 1.5 * setCancelled of a new task causes isCancelled to be true
117 dl 1.4 */
118 dl 1.3 public void testSetCancelled() {
119 dl 1.6 PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable());
120 dl 1.3 assertTrue(task.cancel(false));
121     task.setCancelled();
122     assertTrue(task.isDone());
123     assertTrue(task.isCancelled());
124 dl 1.1 }
125    
126 dl 1.4 /**
127 dl 1.5 * Cancel(true) before run succeeds
128 dl 1.4 */
129 dl 1.1 public void testCancelBeforeRun2() {
130 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
131 dl 1.1 assertTrue(task.cancel(true));
132     task.run();
133     assertTrue(task.isDone());
134     assertTrue(task.isCancelled());
135     }
136    
137 dl 1.4 /**
138 dl 1.5 * cancel of a completed task fails
139 dl 1.4 */
140 dl 1.1 public void testCancelAfterRun() {
141 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
142 dl 1.1 task.run();
143     assertFalse(task.cancel(false));
144     assertTrue(task.isDone());
145     assertFalse(task.isCancelled());
146     }
147    
148 dl 1.4 /**
149 dl 1.5 * cancel(true) interrupts a running task
150 dl 1.4 */
151 dl 1.1 public void testCancelInterrupt(){
152 dl 1.3 CancellableTask task = new CancellableTask(new SmallInterruptedRunnable());
153 dl 1.1 Thread t = new Thread(task);
154    
155     try{
156 dl 1.3 t.start();
157 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
158     assertTrue(task.cancel(true));
159     t.join();
160     assertTrue(task.isDone());
161     assertTrue(task.isCancelled());
162     } catch(InterruptedException e){
163 dl 1.4 unexpectedException();
164 dl 1.1 }
165     }
166    
167    
168 dl 1.4 /**
169 dl 1.5 * cancel(false) does not interrupt a running task
170 dl 1.4 */
171 dl 1.1 public void testCancelNoInterrupt(){
172 dl 1.3 CancellableTask task = new CancellableTask(new SmallRunnable());
173 dl 1.1 Thread t = new Thread(task);
174     try{
175 dl 1.3 t.start();
176 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
177     assertTrue(task.cancel(false));
178     t.join();
179     assertTrue(task.isDone());
180     assertTrue(task.isCancelled());
181     } catch(InterruptedException e){
182 dl 1.4 unexpectedException();
183 dl 1.1 }
184     }
185    
186    
187     }