9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
< |
public class ScheduledExecutorTest extends TestCase{ |
13 |
< |
|
14 |
< |
boolean flag = false; |
15 |
< |
|
12 |
> |
public class ScheduledExecutorTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
19 |
– |
|
20 |
– |
|
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(ScheduledExecutorTest.class); |
18 |
|
} |
19 |
|
|
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 |
– |
|
20 |
|
static class MyRunnable implements Runnable { |
30 |
– |
volatile boolean waiting = true; |
21 |
|
volatile boolean done = false; |
22 |
|
public void run(){ |
23 |
|
try{ |
24 |
< |
Thread.sleep(SHORT_DELAY_MS); |
35 |
< |
waiting = false; |
24 |
> |
Thread.sleep(SMALL_DELAY_MS); |
25 |
|
done = true; |
26 |
|
} catch(Exception e){ |
27 |
|
} |
29 |
|
} |
30 |
|
|
31 |
|
static class MyCallable implements Callable { |
43 |
– |
volatile boolean waiting = true; |
32 |
|
volatile boolean done = false; |
33 |
|
public Object call(){ |
34 |
|
try{ |
35 |
< |
Thread.sleep(SHORT_DELAY_MS); |
48 |
< |
waiting = false; |
35 |
> |
Thread.sleep(SMALL_DELAY_MS); |
36 |
|
done = true; |
37 |
|
}catch(Exception e){} |
38 |
|
return Boolean.TRUE; |
39 |
|
} |
40 |
|
} |
41 |
|
|
55 |
– |
public Runnable newRunnable(){ |
56 |
– |
return new Runnable(){ |
57 |
– |
public void run(){ |
58 |
– |
try{Thread.sleep(SHORT_DELAY_MS); |
59 |
– |
} catch(Exception e){ |
60 |
– |
} |
61 |
– |
} |
62 |
– |
}; |
63 |
– |
} |
64 |
– |
|
65 |
– |
public Runnable newNoopRunnable() { |
66 |
– |
return new Runnable(){ |
67 |
– |
public void run(){ |
68 |
– |
} |
69 |
– |
}; |
70 |
– |
} |
71 |
– |
|
72 |
– |
/** |
73 |
– |
* Test to verify execute successfully runs the given Runnable |
74 |
– |
*/ |
42 |
|
public void testExecute(){ |
43 |
|
try{ |
44 |
|
MyRunnable runnable =new MyRunnable(); |
45 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
46 |
|
one.execute(runnable); |
47 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
48 |
< |
assertTrue(runnable.waiting); |
47 |
> |
assertFalse(runnable.done); |
48 |
> |
Thread.sleep(SHORT_DELAY_MS); |
49 |
|
one.shutdown(); |
50 |
|
try{ |
51 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
52 |
|
} catch(InterruptedException e){ |
53 |
|
fail("unexpected exception"); |
54 |
|
} |
88 |
– |
assertFalse(runnable.waiting); |
55 |
|
assertTrue(runnable.done); |
56 |
|
one.shutdown(); |
57 |
+ |
joinPool(one); |
58 |
|
} |
59 |
|
catch(Exception e){ |
60 |
|
fail("unexpected exception"); |
61 |
|
} |
62 |
+ |
|
63 |
|
} |
64 |
|
|
97 |
– |
/** |
98 |
– |
* Test to verify schedule successfully runs the given Callable. |
99 |
– |
* The waiting flag shows that the Callable is not started until |
100 |
– |
* immediately. |
101 |
– |
*/ |
65 |
|
public void testSchedule1(){ |
66 |
|
try{ |
67 |
|
MyCallable callable = new MyCallable(); |
68 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
69 |
|
Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
70 |
< |
assertTrue(callable.waiting); |
70 |
> |
assertFalse(callable.done); |
71 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
72 |
|
assertTrue(callable.done); |
73 |
|
assertEquals(Boolean.TRUE, f.get()); |
74 |
|
one.shutdown(); |
75 |
+ |
joinPool(one); |
76 |
|
}catch(RejectedExecutionException e){} |
77 |
|
catch(Exception e){ |
78 |
|
fail("unexpected exception"); |
86 |
|
try{ |
87 |
|
MyRunnable runnable = new MyRunnable(); |
88 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
89 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
90 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
91 |
< |
assertTrue(runnable.waiting); |
89 |
> |
one.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
90 |
> |
Thread.sleep(SHORT_DELAY_MS); |
91 |
> |
assertFalse(runnable.done); |
92 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
93 |
|
assertTrue(runnable.done); |
94 |
|
one.shutdown(); |
95 |
+ |
joinPool(one); |
96 |
|
} catch(Exception e){ |
97 |
|
fail("unexpected exception"); |
98 |
|
} |
106 |
|
MyRunnable runnable = new MyRunnable(); |
107 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
108 |
|
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
109 |
< |
// Thread.sleep(505); |
145 |
< |
assertTrue(runnable.waiting); |
109 |
> |
assertFalse(runnable.done); |
110 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
111 |
|
assertTrue(runnable.done); |
112 |
|
one.shutdown(); |
113 |
+ |
joinPool(one); |
114 |
|
} catch(Exception e){ |
115 |
|
fail("unexpected exception"); |
116 |
|
} |
120 |
|
// exception tests |
121 |
|
|
122 |
|
/** |
123 |
< |
* Test to verify schedule(Runnable, long) throws RejectedExecutionException |
123 |
> |
* schedule(Runnable, long) throws RejectedExecutionException |
124 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
125 |
|
*/ |
126 |
|
public void testSchedule1_RejectedExecutionException(){ |
127 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
128 |
|
try{ |
163 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
129 |
|
se.shutdown(); |
130 |
< |
se.schedule(new Runnable(){ |
131 |
< |
public void run(){} |
167 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
130 |
> |
se.schedule(new NoOpRunnable(), |
131 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
132 |
|
fail("shoud throw"); |
133 |
< |
}catch(RejectedExecutionException e){} |
133 |
> |
} catch(RejectedExecutionException success){ |
134 |
> |
} |
135 |
> |
joinPool(se); |
136 |
> |
|
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
140 |
> |
* schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
141 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
142 |
|
*/ |
143 |
|
public void testSchedule2_RejectedExecutionException(){ |
144 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
145 |
|
try{ |
178 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
146 |
|
se.shutdown(); |
147 |
< |
se.schedule(new Callable(){ |
148 |
< |
public Object call(){ |
182 |
< |
return Boolean.TRUE; |
183 |
< |
} |
184 |
< |
}, (long)100, TimeUnit.SECONDS); |
147 |
> |
se.schedule(new NoOpCallable(), |
148 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
149 |
|
fail("should throw"); |
150 |
< |
}catch(RejectedExecutionException e){} |
150 |
> |
} catch(RejectedExecutionException success){ |
151 |
> |
} |
152 |
> |
joinPool(se); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* Test to verify schedule(Callable, long) throws RejectedExecutionException |
156 |
> |
* schedule(Callable, long) throws RejectedExecutionException |
157 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
158 |
|
*/ |
159 |
|
public void testSchedule3_RejectedExecutionException(){ |
160 |
< |
try{ |
161 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
160 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
161 |
> |
try{ |
162 |
|
se.shutdown(); |
163 |
< |
se.schedule(new Callable(){ |
164 |
< |
public Object call(){ |
199 |
< |
return Boolean.TRUE; |
200 |
< |
} |
201 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
163 |
> |
se.schedule(new NoOpCallable(), |
164 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
165 |
|
fail("should throw"); |
166 |
< |
}catch(RejectedExecutionException e){} |
166 |
> |
} catch(RejectedExecutionException success){ |
167 |
> |
} |
168 |
> |
joinPool(se); |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
172 |
> |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
173 |
|
* RejectedExecutionException. |
174 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
175 |
|
*/ |
176 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
177 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
178 |
|
try{ |
213 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
179 |
|
se.shutdown(); |
180 |
< |
se.scheduleAtFixedRate(new Runnable(){ |
181 |
< |
public void run(){} |
217 |
< |
}, 100, 100, TimeUnit.SECONDS); |
180 |
> |
se.scheduleAtFixedRate(new NoOpRunnable(), |
181 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
182 |
|
fail("should throw"); |
183 |
< |
}catch(RejectedExecutionException e){} |
183 |
> |
} catch(RejectedExecutionException success){ |
184 |
> |
} |
185 |
> |
joinPool(se); |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
189 |
> |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
190 |
|
* RejectedExecutionException. |
191 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
192 |
|
*/ |
193 |
|
public void testScheduleAtFixedRate2_RejectedExecutionException(){ |
194 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
195 |
|
try{ |
229 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
196 |
|
se.shutdown(); |
197 |
< |
se.scheduleAtFixedRate(new Runnable(){ |
198 |
< |
public void run(){} |
233 |
< |
}, 1, 100, TimeUnit.SECONDS); |
197 |
> |
se.scheduleAtFixedRate(new NoOpRunnable(), |
198 |
> |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
199 |
|
fail("should throw"); |
200 |
< |
}catch(RejectedExecutionException e){} |
200 |
> |
} catch(RejectedExecutionException success){ |
201 |
> |
} |
202 |
> |
joinPool(se); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
206 |
> |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
207 |
|
* RejectedExecutionException. |
208 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
209 |
|
*/ |
210 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
211 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
212 |
|
try{ |
245 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
213 |
|
se.shutdown(); |
214 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
215 |
< |
public void run(){} |
249 |
< |
}, 100, 100, TimeUnit.SECONDS); |
214 |
> |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
215 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
216 |
|
fail("should throw"); |
217 |
< |
}catch(RejectedExecutionException e){} |
217 |
> |
} catch(RejectedExecutionException success){ |
218 |
> |
} |
219 |
> |
joinPool(se); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
223 |
> |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
224 |
|
* RejectedExecutionException. |
225 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
226 |
|
*/ |
227 |
|
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
228 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
229 |
|
try{ |
261 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
230 |
|
se.shutdown(); |
231 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
232 |
< |
public void run(){} |
265 |
< |
}, 1, 100, TimeUnit.SECONDS); |
231 |
> |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
232 |
> |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
233 |
|
fail("should throw"); |
234 |
< |
}catch(RejectedExecutionException e){} |
234 |
> |
} catch(RejectedExecutionException success){ |
235 |
> |
} |
236 |
> |
joinPool(se); |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* Test to verify execute throws RejectedExecutionException |
240 |
> |
* execute throws RejectedExecutionException |
241 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
242 |
|
*/ |
243 |
|
public void testExecute_RejectedExecutionException(){ |
244 |
+ |
ScheduledExecutor se = new ScheduledExecutor(1); |
245 |
|
try{ |
276 |
– |
ScheduledExecutor se = new ScheduledExecutor(1); |
246 |
|
se.shutdown(); |
247 |
< |
se.execute(new Runnable(){ |
279 |
< |
public void run(){} |
280 |
< |
}); |
247 |
> |
se.execute(new NoOpRunnable()); |
248 |
|
fail("should throw"); |
249 |
< |
}catch(RejectedExecutionException e){} |
249 |
> |
} catch(RejectedExecutionException success){ |
250 |
> |
} |
251 |
> |
joinPool(se); |
252 |
|
} |
253 |
|
|
285 |
– |
|
286 |
– |
|
254 |
|
/** |
255 |
< |
* Test to verify getActiveCount gives correct values |
255 |
> |
* getActiveCount gives correct values |
256 |
|
*/ |
257 |
|
public void testGetActiveCount(){ |
258 |
|
ScheduledExecutor two = new ScheduledExecutor(2); |
259 |
< |
try { |
260 |
< |
assertEquals(0, two.getActiveCount()); |
261 |
< |
two.execute(newRunnable()); |
262 |
< |
try{ |
263 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
264 |
< |
} catch(Exception e){ |
298 |
< |
fail("unexpected exception"); |
299 |
< |
} |
300 |
< |
assertEquals(1, two.getActiveCount()); |
301 |
< |
} finally { |
302 |
< |
two.shutdown(); |
259 |
> |
assertEquals(0, two.getActiveCount()); |
260 |
> |
two.execute(new SmallRunnable()); |
261 |
> |
try{ |
262 |
> |
Thread.sleep(SHORT_DELAY_MS); |
263 |
> |
} catch(Exception e){ |
264 |
> |
fail("unexpected exception"); |
265 |
|
} |
266 |
+ |
assertEquals(1, two.getActiveCount()); |
267 |
+ |
joinPool(two); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
< |
* Test to verify getCompleteTaskCount gives correct values |
271 |
> |
* getCompleteTaskCount gives correct values |
272 |
|
*/ |
273 |
|
public void testGetCompletedTaskCount(){ |
274 |
|
ScheduledExecutor two = new ScheduledExecutor(2); |
275 |
< |
try { |
276 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
277 |
< |
two.execute(newRunnable()); |
278 |
< |
try{ |
279 |
< |
Thread.sleep(MEDIUM_DELAY_MS/2); |
280 |
< |
} catch(Exception e){ |
317 |
< |
fail("unexpected exception"); |
318 |
< |
} |
319 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
320 |
< |
} finally { |
321 |
< |
two.shutdown(); |
275 |
> |
assertEquals(0, two.getCompletedTaskCount()); |
276 |
> |
two.execute(new SmallRunnable()); |
277 |
> |
try{ |
278 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
279 |
> |
} catch(Exception e){ |
280 |
> |
fail("unexpected exception"); |
281 |
|
} |
282 |
+ |
assertEquals(1, two.getCompletedTaskCount()); |
283 |
+ |
joinPool(two); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* Test to verify getCorePoolSize gives correct values |
287 |
> |
* getCorePoolSize gives correct values |
288 |
|
*/ |
289 |
|
public void testGetCorePoolSize(){ |
290 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
291 |
< |
try { |
292 |
< |
assertEquals(1, one.getCorePoolSize()); |
332 |
< |
} finally { |
333 |
< |
one.shutdown(); |
334 |
< |
} |
291 |
> |
assertEquals(1, one.getCorePoolSize()); |
292 |
> |
joinPool(one); |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* Test to verify getLargestPoolSize gives correct values |
296 |
> |
* getLargestPoolSize gives correct values |
297 |
|
*/ |
298 |
|
public void testGetLargestPoolSize(){ |
299 |
|
ScheduledExecutor two = new ScheduledExecutor(2); |
300 |
< |
try { |
301 |
< |
assertEquals(0, two.getLargestPoolSize()); |
302 |
< |
two.execute(newRunnable()); |
303 |
< |
two.execute(newRunnable()); |
304 |
< |
try{ |
305 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
306 |
< |
} catch(Exception e){ |
349 |
< |
fail("unexpected exception"); |
350 |
< |
} |
351 |
< |
assertEquals(2, two.getLargestPoolSize()); |
352 |
< |
} finally { |
353 |
< |
two.shutdown(); |
300 |
> |
assertEquals(0, two.getLargestPoolSize()); |
301 |
> |
two.execute(new SmallRunnable()); |
302 |
> |
two.execute(new SmallRunnable()); |
303 |
> |
try{ |
304 |
> |
Thread.sleep(SHORT_DELAY_MS); |
305 |
> |
} catch(Exception e){ |
306 |
> |
fail("unexpected exception"); |
307 |
|
} |
308 |
+ |
assertEquals(2, two.getLargestPoolSize()); |
309 |
+ |
joinPool(two); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* Test to verify getPoolSize gives correct values |
313 |
> |
* getPoolSize gives correct values |
314 |
|
*/ |
315 |
|
public void testGetPoolSize(){ |
316 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
317 |
< |
try { |
318 |
< |
assertEquals(0, one.getPoolSize()); |
319 |
< |
one.execute(newRunnable()); |
320 |
< |
assertEquals(1, one.getPoolSize()); |
366 |
< |
} finally { |
367 |
< |
one.shutdown(); |
368 |
< |
} |
317 |
> |
assertEquals(0, one.getPoolSize()); |
318 |
> |
one.execute(new SmallRunnable()); |
319 |
> |
assertEquals(1, one.getPoolSize()); |
320 |
> |
joinPool(one); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* Test to verify getTaskCount gives correct values |
324 |
> |
* getTaskCount gives correct values |
325 |
|
*/ |
326 |
|
public void testGetTaskCount(){ |
327 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
328 |
< |
try { |
329 |
< |
assertEquals(0, one.getTaskCount()); |
330 |
< |
for(int i = 0; i < 5; i++) |
331 |
< |
one.execute(newRunnable()); |
332 |
< |
try{ |
333 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
334 |
< |
} catch(Exception e){ |
383 |
< |
fail("unexpected exception"); |
384 |
< |
} |
385 |
< |
assertEquals(5, one.getTaskCount()); |
386 |
< |
} finally { |
387 |
< |
one.shutdown(); |
328 |
> |
assertEquals(0, one.getTaskCount()); |
329 |
> |
for(int i = 0; i < 5; i++) |
330 |
> |
one.execute(new SmallRunnable()); |
331 |
> |
try{ |
332 |
> |
Thread.sleep(SHORT_DELAY_MS); |
333 |
> |
} catch(Exception e){ |
334 |
> |
fail("unexpected exception"); |
335 |
|
} |
336 |
+ |
assertEquals(5, one.getTaskCount()); |
337 |
+ |
joinPool(one); |
338 |
|
} |
339 |
|
|
340 |
|
/** |
341 |
< |
* Test to verify isShutDown gives correct values |
341 |
> |
* isShutDown gives correct values |
342 |
|
*/ |
343 |
|
public void testIsShutdown(){ |
344 |
|
|
354 |
|
|
355 |
|
|
356 |
|
/** |
357 |
< |
* Test to verify isTerminated gives correct values |
357 |
> |
* isTerminated gives correct values |
358 |
|
* Makes sure termination does not take an innapropriate |
359 |
|
* amount of time |
360 |
|
*/ |
361 |
|
public void testIsTerminated(){ |
362 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
363 |
|
try { |
364 |
< |
one.execute(newRunnable()); |
364 |
> |
one.execute(new SmallRunnable()); |
365 |
|
} finally { |
366 |
|
one.shutdown(); |
367 |
|
} |
368 |
< |
boolean flag = false; |
369 |
< |
try{ |
370 |
< |
flag = one.awaitTermination(10, TimeUnit.SECONDS); |
368 |
> |
try { |
369 |
> |
assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
370 |
> |
assertTrue(one.isTerminated()); |
371 |
|
} catch(Exception e){ |
372 |
|
fail("unexpected exception"); |
373 |
|
} |
425 |
– |
assertTrue(one.isTerminated()); |
426 |
– |
if(!flag) |
427 |
– |
fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
< |
* Test to verify that purge correctly removes cancelled tasks |
377 |
> |
* that purge correctly removes cancelled tasks |
378 |
|
* from the queue |
379 |
|
*/ |
380 |
|
public void testPurge(){ |
381 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
382 |
< |
try { |
383 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
384 |
< |
for(int i = 0; i < 5; i++){ |
439 |
< |
tasks[i] = one.schedule(newRunnable(), 1, TimeUnit.MILLISECONDS); |
440 |
< |
} |
441 |
< |
int max = 5; |
442 |
< |
if (tasks[4].cancel(true)) --max; |
443 |
< |
if (tasks[3].cancel(true)) --max; |
444 |
< |
one.purge(); |
445 |
< |
long count = one.getTaskCount(); |
446 |
< |
assertTrue(count > 0 && count <= max); |
447 |
< |
} finally { |
448 |
< |
one.shutdown(); |
382 |
> |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
383 |
> |
for(int i = 0; i < 5; i++){ |
384 |
> |
tasks[i] = one.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
385 |
|
} |
386 |
+ |
int max = 5; |
387 |
+ |
if (tasks[4].cancel(true)) --max; |
388 |
+ |
if (tasks[3].cancel(true)) --max; |
389 |
+ |
one.purge(); |
390 |
+ |
long count = one.getTaskCount(); |
391 |
+ |
assertTrue(count > 0 && count <= max); |
392 |
+ |
joinPool(one); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
< |
* Test to verify shutDownNow returns a list |
396 |
> |
* shutDownNow returns a list |
397 |
|
* containing the correct number of elements |
398 |
|
*/ |
399 |
|
public void testShutDownNow(){ |
400 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
401 |
|
for(int i = 0; i < 5; i++) |
402 |
< |
one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
402 |
> |
one.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
403 |
|
List l = one.shutdownNow(); |
404 |
|
assertTrue(one.isShutdown()); |
405 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
406 |
+ |
joinPool(one); |
407 |
|
} |
408 |
|
|
409 |
|
public void testShutDown1(){ |
414 |
|
|
415 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
416 |
|
for(int i = 0; i < 5; i++) |
417 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
417 |
> |
tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
418 |
|
one.shutdown(); |
419 |
|
BlockingQueue q = one.getQueue(); |
420 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
422 |
|
assertFalse(t.isCancelled()); |
423 |
|
} |
424 |
|
assertTrue(one.isShutdown()); |
425 |
< |
Thread.sleep(SHORT_DELAY_MS); |
425 |
> |
Thread.sleep(SMALL_DELAY_MS); |
426 |
|
for (int i = 0; i < 5; ++i) { |
427 |
|
assertTrue(tasks[i].isDone()); |
428 |
|
assertFalse(tasks[i].isCancelled()); |
441 |
|
one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
442 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
443 |
|
for(int i = 0; i < 5; i++) |
444 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
444 |
> |
tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
445 |
|
one.shutdown(); |
446 |
|
assertTrue(one.isShutdown()); |
447 |
|
BlockingQueue q = one.getQueue(); |
448 |
|
assertTrue(q.isEmpty()); |
449 |
< |
Thread.sleep(SHORT_DELAY_MS); |
449 |
> |
Thread.sleep(SMALL_DELAY_MS); |
450 |
|
assertTrue(one.isTerminated()); |
451 |
|
} |
452 |
|
catch(Exception ex) { |
460 |
|
ScheduledExecutor one = new ScheduledExecutor(1); |
461 |
|
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
462 |
|
ScheduledCancellable task = |
463 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
463 |
> |
one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
464 |
|
one.shutdown(); |
465 |
|
assertTrue(one.isShutdown()); |
466 |
|
BlockingQueue q = one.getQueue(); |
478 |
|
try { |
479 |
|
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
480 |
|
ScheduledCancellable task = |
481 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
481 |
> |
one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
482 |
|
assertFalse(task.isCancelled()); |
483 |
|
one.shutdown(); |
484 |
|
assertFalse(task.isCancelled()); |