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 ScheduledExecutorTest extends TestCase{ |
13 |
|
|
|
14 |
|
|
boolean flag = false; |
15 |
|
|
|
16 |
|
|
public static void main(String[] args) { |
17 |
|
|
junit.textui.TestRunner.run (suite()); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
|
21 |
|
|
public static Test suite() { |
22 |
|
|
return new TestSuite(ScheduledExecutorTest.class); |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
private static long SHORT_DELAY_MS = 100; |
26 |
|
|
private static long MEDIUM_DELAY_MS = 1000; |
27 |
|
|
private static long LONG_DELAY_MS = 10000; |
28 |
|
|
|
29 |
|
|
static class MyRunnable implements Runnable { |
30 |
|
|
volatile boolean waiting = true; |
31 |
|
|
volatile boolean done = false; |
32 |
|
|
public void run(){ |
33 |
|
|
try{ |
34 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
35 |
|
|
waiting = false; |
36 |
|
|
done = true; |
37 |
|
|
} catch(Exception e){} |
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
static class MyCallable implements Callable { |
42 |
|
|
volatile boolean waiting = true; |
43 |
|
|
volatile boolean done = false; |
44 |
|
|
public Object call(){ |
45 |
|
|
try{ |
46 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
47 |
|
|
waiting = false; |
48 |
|
|
done = true; |
49 |
|
|
}catch(Exception e){} |
50 |
|
|
return Boolean.TRUE; |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Test to verify execute successfully runs the given Runnable |
56 |
|
|
*/ |
57 |
|
|
public void testExecute(){ |
58 |
|
|
try{ |
59 |
|
|
MyRunnable runnable =new MyRunnable(); |
60 |
|
|
ScheduledExecutor one = new ScheduledExecutor(1); |
61 |
|
|
one.execute(runnable); |
62 |
|
|
Thread.sleep(100); |
63 |
|
|
assertTrue(runnable.waiting); |
64 |
|
|
one.shutdown(); |
65 |
|
|
// make sure the Runnable has time to complete |
66 |
|
|
try{Thread.sleep(1010);}catch(InterruptedException e){} |
67 |
|
|
assertFalse(runnable.waiting); |
68 |
|
|
assertTrue(runnable.done); |
69 |
|
|
one.shutdown(); |
70 |
|
|
} |
71 |
|
|
catch(Exception e){ |
72 |
|
|
fail("unexpected exception"); |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* Test to verify schedule successfully runs the given Callable. |
78 |
|
|
* The waiting flag shows that the Callable is not started until |
79 |
|
|
* immediately. |
80 |
|
|
*/ |
81 |
|
|
public void testSchedule1(){ |
82 |
|
|
try{ |
83 |
|
|
MyCallable callable = new MyCallable(); |
84 |
|
|
ScheduledExecutor one = new ScheduledExecutor(1); |
85 |
|
|
Future f = one.schedule(callable, 500, TimeUnit.MILLISECONDS); |
86 |
|
|
// Thread.sleep(505); |
87 |
|
|
assertTrue(callable.waiting); |
88 |
|
|
Thread.sleep(2000); |
89 |
|
|
assertTrue(callable.done); |
90 |
|
|
assertEquals(Boolean.TRUE, f.get()); |
91 |
|
|
one.shutdown(); |
92 |
|
|
}catch(RejectedExecutionException e){} |
93 |
|
|
catch(Exception e){} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* Another version of schedule, only using Runnable instead of Callable |
98 |
|
|
*/ |
99 |
|
|
public void testSchedule3(){ |
100 |
|
|
try{ |
101 |
|
|
MyRunnable runnable = new MyRunnable(); |
102 |
|
|
ScheduledExecutor one = new ScheduledExecutor(1); |
103 |
|
|
one.schedule(runnable, 500, TimeUnit.MILLISECONDS); |
104 |
|
|
Thread.sleep(50); |
105 |
|
|
assertTrue(runnable.waiting); |
106 |
|
|
Thread.sleep(2000); |
107 |
|
|
assertTrue(runnable.done); |
108 |
|
|
one.shutdown(); |
109 |
|
|
} catch(Exception e){ |
110 |
|
|
fail("unexpected exception"); |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* The final version of schedule, using both long, TimeUnit and Runnable |
116 |
|
|
*/ |
117 |
|
|
public void testSchedule4(){ |
118 |
|
|
try{ |
119 |
|
|
MyRunnable runnable = new MyRunnable(); |
120 |
|
|
ScheduledExecutor one = new ScheduledExecutor(1); |
121 |
|
|
one.schedule(runnable, 500, TimeUnit.MILLISECONDS); |
122 |
|
|
// Thread.sleep(505); |
123 |
|
|
assertTrue(runnable.waiting); |
124 |
|
|
Thread.sleep(2000); |
125 |
|
|
assertTrue(runnable.done); |
126 |
|
|
one.shutdown(); |
127 |
|
|
} catch(Exception e){ |
128 |
|
|
fail("unexpected exception"); |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
// exception tests |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* Test to verify schedule(Runnable, long) throws RejectedExecutionException |
137 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
138 |
|
|
*/ |
139 |
|
|
public void testSchedule1_RejectedExecutionException(){ |
140 |
|
|
try{ |
141 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
142 |
|
|
se.shutdown(); |
143 |
|
|
se.schedule(new Runnable(){ |
144 |
|
|
public void run(){} |
145 |
|
|
}, 10000, TimeUnit.MILLISECONDS); |
146 |
|
|
fail("shoud throw"); |
147 |
|
|
}catch(RejectedExecutionException e){} |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
152 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
153 |
|
|
*/ |
154 |
|
|
public void testSchedule2_RejectedExecutionException(){ |
155 |
|
|
try{ |
156 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
157 |
|
|
se.shutdown(); |
158 |
|
|
se.schedule(new Callable(){ |
159 |
|
|
public Object call(){ |
160 |
|
|
return Boolean.TRUE; |
161 |
|
|
} |
162 |
|
|
}, (long)100, TimeUnit.SECONDS); |
163 |
|
|
fail("should throw"); |
164 |
|
|
}catch(RejectedExecutionException e){} |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* Test to verify schedule(Callable, long) throws RejectedExecutionException |
169 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
170 |
|
|
*/ |
171 |
|
|
public void testSchedule3_RejectedExecutionException(){ |
172 |
|
|
try{ |
173 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
174 |
|
|
se.shutdown(); |
175 |
|
|
se.schedule(new Callable(){ |
176 |
|
|
public Object call(){ |
177 |
|
|
return Boolean.TRUE; |
178 |
|
|
} |
179 |
|
|
}, 10000, TimeUnit.MILLISECONDS); |
180 |
|
|
fail("should throw"); |
181 |
|
|
}catch(RejectedExecutionException e){} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
/** |
185 |
|
|
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
186 |
|
|
* RejectedExecutionException. |
187 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
188 |
|
|
*/ |
189 |
|
|
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
190 |
|
|
try{ |
191 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
192 |
|
|
se.shutdown(); |
193 |
|
|
se.scheduleAtFixedRate(new Runnable(){ |
194 |
|
|
public void run(){} |
195 |
|
|
}, 100, 100, TimeUnit.SECONDS); |
196 |
|
|
fail("should throw"); |
197 |
|
|
}catch(RejectedExecutionException e){} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
|
|
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
202 |
|
|
* RejectedExecutionException. |
203 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
204 |
|
|
*/ |
205 |
|
|
public void testScheduleAtFixedRate2_RejectedExecutionException(){ |
206 |
|
|
try{ |
207 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
208 |
|
|
se.shutdown(); |
209 |
|
|
se.scheduleAtFixedRate(new Runnable(){ |
210 |
|
|
public void run(){} |
211 |
|
|
}, 1, 100, TimeUnit.SECONDS); |
212 |
|
|
fail("should throw"); |
213 |
|
|
}catch(RejectedExecutionException e){} |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
/** |
217 |
|
|
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
218 |
|
|
* RejectedExecutionException. |
219 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
220 |
|
|
*/ |
221 |
|
|
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
222 |
|
|
try{ |
223 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
224 |
|
|
se.shutdown(); |
225 |
|
|
se.scheduleWithFixedDelay(new Runnable(){ |
226 |
|
|
public void run(){} |
227 |
|
|
}, 100, 100, TimeUnit.SECONDS); |
228 |
|
|
fail("should throw"); |
229 |
|
|
}catch(RejectedExecutionException e){} |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
/** |
233 |
|
|
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
234 |
|
|
* RejectedExecutionException. |
235 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
236 |
|
|
*/ |
237 |
|
|
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
238 |
|
|
try{ |
239 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
240 |
|
|
se.shutdown(); |
241 |
|
|
se.scheduleWithFixedDelay(new Runnable(){ |
242 |
|
|
public void run(){} |
243 |
|
|
}, 1, 100, TimeUnit.SECONDS); |
244 |
|
|
fail("should throw"); |
245 |
|
|
}catch(RejectedExecutionException e){} |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
/** |
249 |
|
|
* Test to verify execute throws RejectedExecutionException |
250 |
|
|
* This occurs on an attempt to schedule a task on a shutdown executor |
251 |
|
|
*/ |
252 |
|
|
public void testExecute_RejectedExecutionException(){ |
253 |
|
|
try{ |
254 |
|
|
ScheduledExecutor se = new ScheduledExecutor(1); |
255 |
|
|
se.shutdown(); |
256 |
|
|
se.execute(new Runnable(){ |
257 |
|
|
public void run(){} |
258 |
|
|
}); |
259 |
|
|
fail("should throw"); |
260 |
|
|
}catch(RejectedExecutionException e){} |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
} |