19 |
|
|
20 |
|
static class MyRunnable implements Runnable { |
21 |
|
volatile boolean done = false; |
22 |
< |
public void run(){ |
23 |
< |
try{ |
22 |
> |
public void run() { |
23 |
> |
try { |
24 |
|
Thread.sleep(SMALL_DELAY_MS); |
25 |
|
done = true; |
26 |
|
} catch(Exception e){ |
30 |
|
|
31 |
|
static class MyCallable implements Callable { |
32 |
|
volatile boolean done = false; |
33 |
< |
public Object call(){ |
34 |
< |
try{ |
33 |
> |
public Object call() { |
34 |
> |
try { |
35 |
|
Thread.sleep(SMALL_DELAY_MS); |
36 |
|
done = true; |
37 |
< |
}catch(Exception e){} |
37 |
> |
} catch(Exception e){ |
38 |
> |
} |
39 |
|
return Boolean.TRUE; |
40 |
|
} |
41 |
|
} |
42 |
|
|
43 |
< |
public void testExecute(){ |
44 |
< |
try{ |
43 |
> |
/** |
44 |
> |
* execute successfully executes a runnable |
45 |
> |
*/ |
46 |
> |
public void testExecute() { |
47 |
> |
try { |
48 |
|
MyRunnable runnable =new MyRunnable(); |
49 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
50 |
< |
one.execute(runnable); |
49 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
50 |
> |
p1.execute(runnable); |
51 |
|
assertFalse(runnable.done); |
52 |
|
Thread.sleep(SHORT_DELAY_MS); |
53 |
< |
one.shutdown(); |
54 |
< |
try{ |
53 |
> |
p1.shutdown(); |
54 |
> |
try { |
55 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
56 |
|
} catch(InterruptedException e){ |
57 |
< |
fail("unexpected exception"); |
57 |
> |
unexpectedException(); |
58 |
|
} |
59 |
|
assertTrue(runnable.done); |
60 |
< |
one.shutdown(); |
61 |
< |
joinPool(one); |
60 |
> |
p1.shutdown(); |
61 |
> |
joinPool(p1); |
62 |
|
} |
63 |
|
catch(Exception e){ |
64 |
< |
fail("unexpected exception"); |
64 |
> |
unexpectedException(); |
65 |
|
} |
66 |
|
|
67 |
|
} |
68 |
|
|
69 |
< |
public void testSchedule1(){ |
70 |
< |
try{ |
69 |
> |
|
70 |
> |
/** |
71 |
> |
* delayed schedule of callable successfully executes after delay |
72 |
> |
*/ |
73 |
> |
public void testSchedule1() { |
74 |
> |
try { |
75 |
|
MyCallable callable = new MyCallable(); |
76 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
77 |
< |
Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
76 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
77 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
78 |
|
assertFalse(callable.done); |
79 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
80 |
|
assertTrue(callable.done); |
81 |
|
assertEquals(Boolean.TRUE, f.get()); |
82 |
< |
one.shutdown(); |
83 |
< |
joinPool(one); |
84 |
< |
}catch(RejectedExecutionException e){} |
82 |
> |
p1.shutdown(); |
83 |
> |
joinPool(p1); |
84 |
> |
} catch(RejectedExecutionException e){} |
85 |
|
catch(Exception e){ |
86 |
< |
fail("unexpected exception"); |
86 |
> |
unexpectedException(); |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* Another version of schedule, only using Runnable instead of Callable |
91 |
> |
* delayed schedule of runnable successfully executes after delay |
92 |
|
*/ |
93 |
< |
public void testSchedule3(){ |
94 |
< |
try{ |
93 |
> |
public void testSchedule3() { |
94 |
> |
try { |
95 |
|
MyRunnable runnable = new MyRunnable(); |
96 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
97 |
< |
one.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
96 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
97 |
> |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
98 |
|
Thread.sleep(SHORT_DELAY_MS); |
99 |
|
assertFalse(runnable.done); |
100 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
101 |
|
assertTrue(runnable.done); |
102 |
< |
one.shutdown(); |
103 |
< |
joinPool(one); |
102 |
> |
p1.shutdown(); |
103 |
> |
joinPool(p1); |
104 |
|
} catch(Exception e){ |
105 |
< |
fail("unexpected exception"); |
105 |
> |
unexpectedException(); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* The final version of schedule, using both long, TimeUnit and Runnable |
110 |
> |
* scheduleAtFixedRate executes runnable after given initial delay |
111 |
> |
*/ |
112 |
> |
public void testSchedule4() { |
113 |
> |
try { |
114 |
> |
MyRunnable runnable = new MyRunnable(); |
115 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
116 |
> |
ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
117 |
> |
assertFalse(runnable.done); |
118 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
119 |
> |
assertTrue(runnable.done); |
120 |
> |
h.cancel(true); |
121 |
> |
p1.shutdown(); |
122 |
> |
joinPool(p1); |
123 |
> |
} catch(Exception e){ |
124 |
> |
unexpectedException(); |
125 |
> |
} |
126 |
> |
} |
127 |
> |
|
128 |
> |
/** |
129 |
> |
* scheduleWithFixedDelay executes runnable after given initial delay |
130 |
|
*/ |
131 |
< |
public void testSchedule4(){ |
132 |
< |
try{ |
131 |
> |
public void testSchedule5() { |
132 |
> |
try { |
133 |
|
MyRunnable runnable = new MyRunnable(); |
134 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
135 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
134 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
135 |
> |
ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
136 |
|
assertFalse(runnable.done); |
137 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
138 |
|
assertTrue(runnable.done); |
139 |
< |
one.shutdown(); |
140 |
< |
joinPool(one); |
139 |
> |
h.cancel(true); |
140 |
> |
p1.shutdown(); |
141 |
> |
joinPool(p1); |
142 |
|
} catch(Exception e){ |
143 |
< |
fail("unexpected exception"); |
143 |
> |
unexpectedException(); |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
< |
|
148 |
< |
// exception tests |
147 |
> |
/** |
148 |
> |
* execute (null) throws NPE |
149 |
> |
*/ |
150 |
> |
public void testExecuteNull() { |
151 |
> |
ScheduledExecutor se = null; |
152 |
> |
try { |
153 |
> |
se = new ScheduledExecutor(1); |
154 |
> |
se.execute(null); |
155 |
> |
shouldThrow(); |
156 |
> |
} catch(NullPointerException success){} |
157 |
> |
catch(Exception e){ |
158 |
> |
unexpectedException(); |
159 |
> |
} |
160 |
> |
|
161 |
> |
joinPool(se); |
162 |
> |
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* schedule(Runnable, long) throws RejectedExecutionException |
124 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
165 |
> |
* schedule (null) throws NPE |
166 |
|
*/ |
167 |
< |
public void testSchedule1_RejectedExecutionException(){ |
167 |
> |
public void testScheduleNull() { |
168 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
169 |
< |
try{ |
169 |
> |
try { |
170 |
> |
MyCallable callable = null; |
171 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
172 |
> |
shouldThrow(); |
173 |
> |
} catch(NullPointerException success){} |
174 |
> |
catch(Exception e){ |
175 |
> |
unexpectedException(); |
176 |
> |
} |
177 |
> |
joinPool(se); |
178 |
> |
} |
179 |
> |
|
180 |
> |
/** |
181 |
> |
* execute throws RejectedExecutionException if shutdown |
182 |
> |
*/ |
183 |
> |
public void testSchedule1_RejectedExecutionException() { |
184 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
185 |
> |
try { |
186 |
|
se.shutdown(); |
187 |
|
se.schedule(new NoOpRunnable(), |
188 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
189 |
< |
fail("shoud throw"); |
189 |
> |
shouldThrow(); |
190 |
|
} catch(RejectedExecutionException success){ |
191 |
|
} |
192 |
|
joinPool(se); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
< |
* schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
141 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
197 |
> |
* schedule throws RejectedExecutionException if shutdown |
198 |
|
*/ |
199 |
< |
public void testSchedule2_RejectedExecutionException(){ |
199 |
> |
public void testSchedule2_RejectedExecutionException() { |
200 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
201 |
< |
try{ |
201 |
> |
try { |
202 |
|
se.shutdown(); |
203 |
|
se.schedule(new NoOpCallable(), |
204 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
205 |
< |
fail("should throw"); |
205 |
> |
shouldThrow(); |
206 |
|
} catch(RejectedExecutionException success){ |
207 |
|
} |
208 |
|
joinPool(se); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* schedule(Callable, long) throws RejectedExecutionException |
157 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
212 |
> |
* schedule callable throws RejectedExecutionException if shutdown |
213 |
|
*/ |
214 |
< |
public void testSchedule3_RejectedExecutionException(){ |
214 |
> |
public void testSchedule3_RejectedExecutionException() { |
215 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
216 |
< |
try{ |
216 |
> |
try { |
217 |
|
se.shutdown(); |
218 |
|
se.schedule(new NoOpCallable(), |
219 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
220 |
< |
fail("should throw"); |
220 |
> |
shouldThrow(); |
221 |
|
} catch(RejectedExecutionException success){ |
222 |
|
} |
223 |
|
joinPool(se); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
173 |
< |
* RejectedExecutionException. |
174 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
227 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
228 |
|
*/ |
229 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
229 |
> |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
230 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
231 |
< |
try{ |
231 |
> |
try { |
232 |
|
se.shutdown(); |
233 |
|
se.scheduleAtFixedRate(new NoOpRunnable(), |
234 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
235 |
< |
fail("should throw"); |
235 |
> |
shouldThrow(); |
236 |
|
} catch(RejectedExecutionException success){ |
237 |
|
} |
238 |
|
joinPool(se); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
< |
* 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{ |
196 |
< |
se.shutdown(); |
197 |
< |
se.scheduleAtFixedRate(new NoOpRunnable(), |
198 |
< |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
199 |
< |
fail("should throw"); |
200 |
< |
} catch(RejectedExecutionException success){ |
201 |
< |
} |
202 |
< |
joinPool(se); |
203 |
< |
} |
204 |
< |
|
205 |
< |
/** |
206 |
< |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
207 |
< |
* RejectedExecutionException. |
208 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
242 |
> |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
243 |
|
*/ |
244 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
244 |
> |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
245 |
|
ScheduledExecutor se = new ScheduledExecutor(1); |
246 |
< |
try{ |
246 |
> |
try { |
247 |
|
se.shutdown(); |
248 |
|
se.scheduleWithFixedDelay(new NoOpRunnable(), |
249 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
250 |
< |
fail("should throw"); |
217 |
< |
} catch(RejectedExecutionException success){ |
218 |
< |
} |
219 |
< |
joinPool(se); |
220 |
< |
} |
221 |
< |
|
222 |
< |
/** |
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{ |
230 |
< |
se.shutdown(); |
231 |
< |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
232 |
< |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
233 |
< |
fail("should throw"); |
250 |
> |
shouldThrow(); |
251 |
|
} catch(RejectedExecutionException success){ |
252 |
|
} |
253 |
|
joinPool(se); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* execute throws RejectedExecutionException |
258 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
257 |
> |
* getActiveCount increases but doesn't overestimate, when a |
258 |
> |
* thread becomes active |
259 |
|
*/ |
260 |
< |
public void testExecute_RejectedExecutionException(){ |
261 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
262 |
< |
try{ |
263 |
< |
se.shutdown(); |
264 |
< |
se.execute(new NoOpRunnable()); |
248 |
< |
fail("should throw"); |
249 |
< |
} catch(RejectedExecutionException success){ |
250 |
< |
} |
251 |
< |
joinPool(se); |
252 |
< |
} |
253 |
< |
|
254 |
< |
/** |
255 |
< |
* getActiveCount gives correct values |
256 |
< |
*/ |
257 |
< |
public void testGetActiveCount(){ |
258 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
259 |
< |
assertEquals(0, two.getActiveCount()); |
260 |
< |
two.execute(new SmallRunnable()); |
261 |
< |
try{ |
260 |
> |
public void testGetActiveCount() { |
261 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
262 |
> |
assertEquals(0, p2.getActiveCount()); |
263 |
> |
p2.execute(new SmallRunnable()); |
264 |
> |
try { |
265 |
|
Thread.sleep(SHORT_DELAY_MS); |
266 |
|
} catch(Exception e){ |
267 |
< |
fail("unexpected exception"); |
267 |
> |
unexpectedException(); |
268 |
|
} |
269 |
< |
assertEquals(1, two.getActiveCount()); |
270 |
< |
joinPool(two); |
269 |
> |
assertEquals(1, p2.getActiveCount()); |
270 |
> |
joinPool(p2); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* getCompleteTaskCount gives correct values |
274 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
275 |
> |
* when tasks complete |
276 |
|
*/ |
277 |
< |
public void testGetCompletedTaskCount(){ |
278 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
279 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
280 |
< |
two.execute(new SmallRunnable()); |
281 |
< |
try{ |
277 |
> |
public void testGetCompletedTaskCount() { |
278 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
279 |
> |
assertEquals(0, p2.getCompletedTaskCount()); |
280 |
> |
p2.execute(new SmallRunnable()); |
281 |
> |
try { |
282 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
283 |
|
} catch(Exception e){ |
284 |
< |
fail("unexpected exception"); |
284 |
> |
unexpectedException(); |
285 |
|
} |
286 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
287 |
< |
joinPool(two); |
286 |
> |
assertEquals(1, p2.getCompletedTaskCount()); |
287 |
> |
joinPool(p2); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
< |
* getCorePoolSize gives correct values |
291 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
292 |
|
*/ |
293 |
< |
public void testGetCorePoolSize(){ |
294 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
295 |
< |
assertEquals(1, one.getCorePoolSize()); |
296 |
< |
joinPool(one); |
293 |
> |
public void testGetCorePoolSize() { |
294 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
295 |
> |
assertEquals(1, p1.getCorePoolSize()); |
296 |
> |
joinPool(p1); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* getLargestPoolSize gives correct values |
300 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
301 |
> |
* multiple threads active |
302 |
|
*/ |
303 |
< |
public void testGetLargestPoolSize(){ |
304 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
305 |
< |
assertEquals(0, two.getLargestPoolSize()); |
306 |
< |
two.execute(new SmallRunnable()); |
307 |
< |
two.execute(new SmallRunnable()); |
308 |
< |
try{ |
303 |
> |
public void testGetLargestPoolSize() { |
304 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
305 |
> |
assertEquals(0, p2.getLargestPoolSize()); |
306 |
> |
p2.execute(new SmallRunnable()); |
307 |
> |
p2.execute(new SmallRunnable()); |
308 |
> |
try { |
309 |
|
Thread.sleep(SHORT_DELAY_MS); |
310 |
|
} catch(Exception e){ |
311 |
< |
fail("unexpected exception"); |
311 |
> |
unexpectedException(); |
312 |
|
} |
313 |
< |
assertEquals(2, two.getLargestPoolSize()); |
314 |
< |
joinPool(two); |
313 |
> |
assertEquals(2, p2.getLargestPoolSize()); |
314 |
> |
joinPool(p2); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* getPoolSize gives correct values |
318 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
319 |
> |
* become active |
320 |
|
*/ |
321 |
< |
public void testGetPoolSize(){ |
322 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
323 |
< |
assertEquals(0, one.getPoolSize()); |
324 |
< |
one.execute(new SmallRunnable()); |
325 |
< |
assertEquals(1, one.getPoolSize()); |
326 |
< |
joinPool(one); |
321 |
> |
public void testGetPoolSize() { |
322 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
323 |
> |
assertEquals(0, p1.getPoolSize()); |
324 |
> |
p1.execute(new SmallRunnable()); |
325 |
> |
assertEquals(1, p1.getPoolSize()); |
326 |
> |
joinPool(p1); |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* getTaskCount gives correct values |
330 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
331 |
> |
* submitted |
332 |
|
*/ |
333 |
< |
public void testGetTaskCount(){ |
334 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
335 |
< |
assertEquals(0, one.getTaskCount()); |
333 |
> |
public void testGetTaskCount() { |
334 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
335 |
> |
assertEquals(0, p1.getTaskCount()); |
336 |
|
for(int i = 0; i < 5; i++) |
337 |
< |
one.execute(new SmallRunnable()); |
338 |
< |
try{ |
337 |
> |
p1.execute(new SmallRunnable()); |
338 |
> |
try { |
339 |
|
Thread.sleep(SHORT_DELAY_MS); |
340 |
|
} catch(Exception e){ |
341 |
< |
fail("unexpected exception"); |
341 |
> |
unexpectedException(); |
342 |
|
} |
343 |
< |
assertEquals(5, one.getTaskCount()); |
344 |
< |
joinPool(one); |
343 |
> |
assertEquals(5, p1.getTaskCount()); |
344 |
> |
joinPool(p1); |
345 |
|
} |
346 |
|
|
347 |
|
/** |
348 |
< |
* isShutDown gives correct values |
348 |
> |
* is isShutDown is false before shutdown, true after |
349 |
|
*/ |
350 |
< |
public void testIsShutdown(){ |
350 |
> |
public void testIsShutdown() { |
351 |
|
|
352 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
352 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
353 |
|
try { |
354 |
< |
assertFalse(one.isShutdown()); |
354 |
> |
assertFalse(p1.isShutdown()); |
355 |
|
} |
356 |
|
finally { |
357 |
< |
one.shutdown(); |
357 |
> |
p1.shutdown(); |
358 |
|
} |
359 |
< |
assertTrue(one.isShutdown()); |
359 |
> |
assertTrue(p1.isShutdown()); |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
|
/** |
364 |
< |
* isTerminated gives correct values |
358 |
< |
* Makes sure termination does not take an innapropriate |
359 |
< |
* amount of time |
364 |
> |
* isTerminated is false before termination, true after |
365 |
|
*/ |
366 |
< |
public void testIsTerminated(){ |
367 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
366 |
> |
public void testIsTerminated() { |
367 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
368 |
|
try { |
369 |
< |
one.execute(new SmallRunnable()); |
369 |
> |
p1.execute(new SmallRunnable()); |
370 |
|
} finally { |
371 |
< |
one.shutdown(); |
371 |
> |
p1.shutdown(); |
372 |
|
} |
373 |
|
try { |
374 |
< |
assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
375 |
< |
assertTrue(one.isTerminated()); |
374 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
375 |
> |
assertTrue(p1.isTerminated()); |
376 |
|
} catch(Exception e){ |
377 |
< |
fail("unexpected exception"); |
377 |
> |
unexpectedException(); |
378 |
|
} |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* that purge correctly removes cancelled tasks |
378 |
< |
* from the queue |
382 |
> |
* isTerminating is not true when running or when terminated |
383 |
|
*/ |
384 |
< |
public void testPurge(){ |
385 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
384 |
> |
public void testIsTerminating() { |
385 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
386 |
> |
assertFalse(p1.isTerminating()); |
387 |
> |
try { |
388 |
> |
p1.execute(new SmallRunnable()); |
389 |
> |
assertFalse(p1.isTerminating()); |
390 |
> |
} finally { |
391 |
> |
p1.shutdown(); |
392 |
> |
} |
393 |
> |
try { |
394 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
395 |
> |
assertTrue(p1.isTerminated()); |
396 |
> |
assertFalse(p1.isTerminating()); |
397 |
> |
} catch(Exception e){ |
398 |
> |
unexpectedException(); |
399 |
> |
} |
400 |
> |
} |
401 |
> |
|
402 |
> |
/** |
403 |
> |
* purge removes cancelled tasks from the queue |
404 |
> |
*/ |
405 |
> |
public void testPurge() { |
406 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
407 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
408 |
|
for(int i = 0; i < 5; i++){ |
409 |
< |
tasks[i] = one.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
409 |
> |
tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
410 |
|
} |
411 |
|
int max = 5; |
412 |
|
if (tasks[4].cancel(true)) --max; |
413 |
|
if (tasks[3].cancel(true)) --max; |
414 |
< |
one.purge(); |
415 |
< |
long count = one.getTaskCount(); |
414 |
> |
p1.purge(); |
415 |
> |
long count = p1.getTaskCount(); |
416 |
|
assertTrue(count > 0 && count <= max); |
417 |
< |
joinPool(one); |
417 |
> |
joinPool(p1); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* shutDownNow returns a list |
397 |
< |
* containing the correct number of elements |
421 |
> |
* shutDownNow returns a list containing tasks that were not run |
422 |
|
*/ |
423 |
< |
public void testShutDownNow(){ |
424 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
423 |
> |
public void testShutDownNow() { |
424 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
425 |
|
for(int i = 0; i < 5; i++) |
426 |
< |
one.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
427 |
< |
List l = one.shutdownNow(); |
428 |
< |
assertTrue(one.isShutdown()); |
426 |
> |
p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
427 |
> |
List l = p1.shutdownNow(); |
428 |
> |
assertTrue(p1.isShutdown()); |
429 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
430 |
< |
joinPool(one); |
430 |
> |
joinPool(p1); |
431 |
|
} |
432 |
|
|
433 |
< |
public void testShutDown1(){ |
433 |
> |
/** |
434 |
> |
* In default setting, shutdown cancels periodic but not delayed |
435 |
> |
* tasks at shutdown |
436 |
> |
*/ |
437 |
> |
public void testShutDown1() { |
438 |
|
try { |
439 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
440 |
< |
assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
441 |
< |
assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
439 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
440 |
> |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
441 |
> |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
442 |
|
|
443 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
444 |
|
for(int i = 0; i < 5; i++) |
445 |
< |
tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
446 |
< |
one.shutdown(); |
447 |
< |
BlockingQueue q = one.getQueue(); |
445 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
446 |
> |
p1.shutdown(); |
447 |
> |
BlockingQueue q = p1.getQueue(); |
448 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
449 |
|
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
450 |
|
assertFalse(t.isCancelled()); |
451 |
|
} |
452 |
< |
assertTrue(one.isShutdown()); |
452 |
> |
assertTrue(p1.isShutdown()); |
453 |
|
Thread.sleep(SMALL_DELAY_MS); |
454 |
|
for (int i = 0; i < 5; ++i) { |
455 |
|
assertTrue(tasks[i].isDone()); |
458 |
|
|
459 |
|
} |
460 |
|
catch(Exception ex) { |
461 |
< |
fail("unexpected exception"); |
461 |
> |
unexpectedException(); |
462 |
|
} |
463 |
|
} |
464 |
|
|
465 |
|
|
466 |
< |
public void testShutDown2(){ |
466 |
> |
/** |
467 |
> |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
468 |
> |
* delayed tasks are cancelled at shutdown |
469 |
> |
*/ |
470 |
> |
public void testShutDown2() { |
471 |
|
try { |
472 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
473 |
< |
one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
472 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
473 |
> |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
474 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
475 |
|
for(int i = 0; i < 5; i++) |
476 |
< |
tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
477 |
< |
one.shutdown(); |
478 |
< |
assertTrue(one.isShutdown()); |
479 |
< |
BlockingQueue q = one.getQueue(); |
476 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
477 |
> |
p1.shutdown(); |
478 |
> |
assertTrue(p1.isShutdown()); |
479 |
> |
BlockingQueue q = p1.getQueue(); |
480 |
|
assertTrue(q.isEmpty()); |
481 |
|
Thread.sleep(SMALL_DELAY_MS); |
482 |
< |
assertTrue(one.isTerminated()); |
482 |
> |
assertTrue(p1.isTerminated()); |
483 |
|
} |
484 |
|
catch(Exception ex) { |
485 |
< |
fail("unexpected exception"); |
485 |
> |
unexpectedException(); |
486 |
|
} |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
< |
public void testShutDown3(){ |
490 |
> |
/** |
491 |
> |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
492 |
> |
* periodic tasks are not cancelled at shutdown |
493 |
> |
*/ |
494 |
> |
public void testShutDown3() { |
495 |
|
try { |
496 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
497 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
496 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
497 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
498 |
|
ScheduledCancellable task = |
499 |
< |
one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
500 |
< |
one.shutdown(); |
501 |
< |
assertTrue(one.isShutdown()); |
502 |
< |
BlockingQueue q = one.getQueue(); |
499 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
500 |
> |
p1.shutdown(); |
501 |
> |
assertTrue(p1.isShutdown()); |
502 |
> |
BlockingQueue q = p1.getQueue(); |
503 |
|
assertTrue(q.isEmpty()); |
504 |
|
Thread.sleep(SHORT_DELAY_MS); |
505 |
< |
assertTrue(one.isTerminated()); |
505 |
> |
assertTrue(p1.isTerminated()); |
506 |
|
} |
507 |
|
catch(Exception ex) { |
508 |
< |
fail("unexpected exception"); |
508 |
> |
unexpectedException(); |
509 |
|
} |
510 |
|
} |
511 |
|
|
512 |
< |
public void testShutDown4(){ |
513 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
512 |
> |
/** |
513 |
> |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
514 |
> |
* periodic tasks are cancelled at shutdown |
515 |
> |
*/ |
516 |
> |
public void testShutDown4() { |
517 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
518 |
|
try { |
519 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
519 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
520 |
|
ScheduledCancellable task = |
521 |
< |
one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
521 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
522 |
|
assertFalse(task.isCancelled()); |
523 |
< |
one.shutdown(); |
523 |
> |
p1.shutdown(); |
524 |
|
assertFalse(task.isCancelled()); |
525 |
< |
assertFalse(one.isTerminated()); |
526 |
< |
assertTrue(one.isShutdown()); |
525 |
> |
assertFalse(p1.isTerminated()); |
526 |
> |
assertTrue(p1.isShutdown()); |
527 |
|
Thread.sleep(SHORT_DELAY_MS); |
528 |
|
assertFalse(task.isCancelled()); |
529 |
|
task.cancel(true); |
530 |
|
assertTrue(task.isCancelled()); |
531 |
|
Thread.sleep(SHORT_DELAY_MS); |
532 |
< |
assertTrue(one.isTerminated()); |
532 |
> |
assertTrue(p1.isTerminated()); |
533 |
|
} |
534 |
|
catch(Exception ex) { |
535 |
< |
fail("unexpected exception"); |
535 |
> |
unexpectedException(); |
536 |
|
} |
537 |
|
finally { |
538 |
< |
one.shutdownNow(); |
538 |
> |
p1.shutdownNow(); |
539 |
|
} |
540 |
|
} |
541 |
|
|