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; |
22 |
> |
public void run() { |
23 |
> |
try { |
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; |
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 |
|
|
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 |
– |
|
43 |
|
/** |
44 |
< |
* Test to verify execute successfully runs the given Runnable |
44 |
> |
* execute successfully executes a runnable |
45 |
|
*/ |
46 |
< |
public void testExecute(){ |
47 |
< |
try{ |
46 |
> |
public void testExecute() { |
47 |
> |
try { |
48 |
|
MyRunnable runnable =new MyRunnable(); |
49 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
50 |
< |
one.execute(runnable); |
51 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
52 |
< |
assertTrue(runnable.waiting); |
53 |
< |
one.shutdown(); |
54 |
< |
try{ |
49 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
50 |
> |
p1.execute(runnable); |
51 |
> |
assertFalse(runnable.done); |
52 |
> |
Thread.sleep(SHORT_DELAY_MS); |
53 |
> |
p1.shutdown(); |
54 |
> |
try { |
55 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
56 |
|
} catch(InterruptedException e){ |
57 |
< |
fail("unexpected exception"); |
57 |
> |
unexpectedException(); |
58 |
|
} |
88 |
– |
assertFalse(runnable.waiting); |
59 |
|
assertTrue(runnable.done); |
60 |
< |
one.shutdown(); |
60 |
> |
p1.shutdown(); |
61 |
> |
joinPool(p1); |
62 |
|
} |
63 |
|
catch(Exception e){ |
64 |
< |
fail("unexpected exception"); |
64 |
> |
unexpectedException(); |
65 |
|
} |
66 |
+ |
|
67 |
|
} |
68 |
|
|
69 |
+ |
|
70 |
|
/** |
71 |
< |
* Test to verify schedule successfully runs the given Callable. |
99 |
< |
* The waiting flag shows that the Callable is not started until |
100 |
< |
* immediately. |
71 |
> |
* delayed schedule of callable successfully executes after delay |
72 |
|
*/ |
73 |
< |
public void testSchedule1(){ |
74 |
< |
try{ |
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); |
78 |
< |
assertTrue(callable.waiting); |
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 |
< |
}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, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
98 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
99 |
< |
assertTrue(runnable.waiting); |
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(); |
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{ |
112 |
> |
public void testSchedule4() { |
113 |
> |
try { |
114 |
|
MyRunnable runnable = new MyRunnable(); |
115 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
116 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
117 |
< |
// Thread.sleep(505); |
145 |
< |
assertTrue(runnable.waiting); |
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 |
< |
one.shutdown(); |
120 |
> |
h.cancel(true); |
121 |
> |
p1.shutdown(); |
122 |
> |
joinPool(p1); |
123 |
|
} catch(Exception e){ |
124 |
< |
fail("unexpected exception"); |
124 |
> |
unexpectedException(); |
125 |
|
} |
126 |
|
} |
153 |
– |
|
154 |
– |
|
155 |
– |
// exception tests |
127 |
|
|
128 |
|
/** |
129 |
< |
* Test to verify schedule(Runnable, long) throws RejectedExecutionException |
159 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
129 |
> |
* scheduleWithFixedDelay executes runnable after given initial delay |
130 |
|
*/ |
131 |
< |
public void testSchedule1_RejectedExecutionException(){ |
132 |
< |
try{ |
133 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
134 |
< |
se.shutdown(); |
135 |
< |
se.schedule(new Runnable(){ |
136 |
< |
public void run(){} |
137 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
138 |
< |
fail("shoud throw"); |
139 |
< |
}catch(RejectedExecutionException e){} |
131 |
> |
public void testSchedule5() { |
132 |
> |
try { |
133 |
> |
MyRunnable runnable = new MyRunnable(); |
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 |
> |
h.cancel(true); |
140 |
> |
p1.shutdown(); |
141 |
> |
joinPool(p1); |
142 |
> |
} catch(Exception e){ |
143 |
> |
unexpectedException(); |
144 |
> |
} |
145 |
|
} |
146 |
< |
|
146 |
> |
|
147 |
|
/** |
148 |
< |
* Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
174 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
148 |
> |
* execute (null) throws NPE |
149 |
|
*/ |
150 |
< |
public void testSchedule2_RejectedExecutionException(){ |
151 |
< |
try{ |
152 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
153 |
< |
se.shutdown(); |
154 |
< |
se.schedule(new Callable(){ |
155 |
< |
public Object call(){ |
156 |
< |
return Boolean.TRUE; |
157 |
< |
} |
158 |
< |
}, (long)100, TimeUnit.SECONDS); |
159 |
< |
fail("should throw"); |
160 |
< |
}catch(RejectedExecutionException e){} |
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 |
< |
* Test to verify schedule(Callable, long) throws RejectedExecutionException |
166 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
167 |
< |
*/ |
168 |
< |
public void testSchedule3_RejectedExecutionException(){ |
169 |
< |
try{ |
170 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
171 |
< |
se.shutdown(); |
172 |
< |
se.schedule(new Callable(){ |
173 |
< |
public Object call(){ |
174 |
< |
return Boolean.TRUE; |
175 |
< |
} |
176 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
177 |
< |
fail("should throw"); |
203 |
< |
}catch(RejectedExecutionException e){} |
165 |
> |
* schedule (null) throws NPE |
166 |
> |
*/ |
167 |
> |
public void testScheduleNull() { |
168 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
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 |
< |
|
179 |
> |
|
180 |
|
/** |
181 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
182 |
< |
* RejectedExecutionException. |
183 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
184 |
< |
*/ |
185 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
212 |
< |
try{ |
213 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
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.scheduleAtFixedRate(new Runnable(){ |
188 |
< |
public void run(){} |
189 |
< |
}, 100, 100, TimeUnit.SECONDS); |
190 |
< |
fail("should throw"); |
191 |
< |
}catch(RejectedExecutionException e){} |
187 |
> |
se.schedule(new NoOpRunnable(), |
188 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
189 |
> |
shouldThrow(); |
190 |
> |
} catch(RejectedExecutionException success){ |
191 |
> |
} |
192 |
> |
joinPool(se); |
193 |
> |
|
194 |
|
} |
195 |
< |
|
195 |
> |
|
196 |
|
/** |
197 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
198 |
< |
* RejectedExecutionException. |
199 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
200 |
< |
*/ |
201 |
< |
public void testScheduleAtFixedRate2_RejectedExecutionException(){ |
228 |
< |
try{ |
229 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
197 |
> |
* schedule throws RejectedExecutionException if shutdown |
198 |
> |
*/ |
199 |
> |
public void testSchedule2_RejectedExecutionException() { |
200 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
201 |
> |
try { |
202 |
|
se.shutdown(); |
203 |
< |
se.scheduleAtFixedRate(new Runnable(){ |
204 |
< |
public void run(){} |
205 |
< |
}, 1, 100, TimeUnit.SECONDS); |
206 |
< |
fail("should throw"); |
207 |
< |
}catch(RejectedExecutionException e){} |
203 |
> |
se.schedule(new NoOpCallable(), |
204 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
205 |
> |
shouldThrow(); |
206 |
> |
} catch(RejectedExecutionException success){ |
207 |
> |
} |
208 |
> |
joinPool(se); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
240 |
< |
* RejectedExecutionException. |
241 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
212 |
> |
* schedule callable throws RejectedExecutionException if shutdown |
213 |
|
*/ |
214 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
215 |
< |
try{ |
216 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
214 |
> |
public void testSchedule3_RejectedExecutionException() { |
215 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
216 |
> |
try { |
217 |
|
se.shutdown(); |
218 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
219 |
< |
public void run(){} |
220 |
< |
}, 100, 100, TimeUnit.SECONDS); |
221 |
< |
fail("should throw"); |
222 |
< |
}catch(RejectedExecutionException e){} |
218 |
> |
se.schedule(new NoOpCallable(), |
219 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
220 |
> |
shouldThrow(); |
221 |
> |
} catch(RejectedExecutionException success){ |
222 |
> |
} |
223 |
> |
joinPool(se); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
256 |
< |
* RejectedExecutionException. |
257 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
227 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
228 |
|
*/ |
229 |
< |
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
230 |
< |
try{ |
231 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
229 |
> |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
230 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
231 |
> |
try { |
232 |
|
se.shutdown(); |
233 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
234 |
< |
public void run(){} |
235 |
< |
}, 1, 100, TimeUnit.SECONDS); |
236 |
< |
fail("should throw"); |
237 |
< |
}catch(RejectedExecutionException e){} |
233 |
> |
se.scheduleAtFixedRate(new NoOpRunnable(), |
234 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
235 |
> |
shouldThrow(); |
236 |
> |
} catch(RejectedExecutionException success){ |
237 |
> |
} |
238 |
> |
joinPool(se); |
239 |
|
} |
240 |
< |
|
240 |
> |
|
241 |
|
/** |
242 |
< |
* Test to verify execute throws RejectedExecutionException |
272 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
242 |
> |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
243 |
|
*/ |
244 |
< |
public void testExecute_RejectedExecutionException(){ |
245 |
< |
try{ |
246 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
244 |
> |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
245 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
246 |
> |
try { |
247 |
|
se.shutdown(); |
248 |
< |
se.execute(new Runnable(){ |
249 |
< |
public void run(){} |
250 |
< |
}); |
251 |
< |
fail("should throw"); |
252 |
< |
}catch(RejectedExecutionException e){} |
248 |
> |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
249 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
250 |
> |
shouldThrow(); |
251 |
> |
} catch(RejectedExecutionException success){ |
252 |
> |
} |
253 |
> |
joinPool(se); |
254 |
|
} |
255 |
|
|
285 |
– |
|
286 |
– |
|
256 |
|
/** |
257 |
< |
* Test to verify getActiveCount gives correct values |
258 |
< |
*/ |
259 |
< |
public void testGetActiveCount(){ |
260 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
257 |
> |
* getActiveCount increases but doesn't overestimate, when a |
258 |
> |
* thread becomes active |
259 |
> |
*/ |
260 |
> |
public void testGetActiveCount() { |
261 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
262 |
> |
assertEquals(0, p2.getActiveCount()); |
263 |
> |
p2.execute(new SmallRunnable()); |
264 |
|
try { |
265 |
< |
assertEquals(0, two.getActiveCount()); |
266 |
< |
two.execute(newRunnable()); |
267 |
< |
try{ |
296 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
297 |
< |
} catch(Exception e){ |
298 |
< |
fail("unexpected exception"); |
299 |
< |
} |
300 |
< |
assertEquals(1, two.getActiveCount()); |
301 |
< |
} finally { |
302 |
< |
two.shutdown(); |
265 |
> |
Thread.sleep(SHORT_DELAY_MS); |
266 |
> |
} catch(Exception e){ |
267 |
> |
unexpectedException(); |
268 |
|
} |
269 |
+ |
assertEquals(1, p2.getActiveCount()); |
270 |
+ |
joinPool(p2); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* Test to verify 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); |
277 |
> |
public void testGetCompletedTaskCount() { |
278 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
279 |
> |
assertEquals(0, p2.getCompletedTaskCount()); |
280 |
> |
p2.execute(new SmallRunnable()); |
281 |
|
try { |
282 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
283 |
< |
two.execute(newRunnable()); |
284 |
< |
try{ |
315 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
316 |
< |
} catch(Exception e){ |
317 |
< |
fail("unexpected exception"); |
318 |
< |
} |
319 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
320 |
< |
} finally { |
321 |
< |
two.shutdown(); |
282 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
283 |
> |
} catch(Exception e){ |
284 |
> |
unexpectedException(); |
285 |
|
} |
286 |
+ |
assertEquals(1, p2.getCompletedTaskCount()); |
287 |
+ |
joinPool(p2); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
< |
* Test to verify 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 |
< |
try { |
296 |
< |
assertEquals(1, one.getCorePoolSize()); |
332 |
< |
} finally { |
333 |
< |
one.shutdown(); |
334 |
< |
} |
293 |
> |
public void testGetCorePoolSize() { |
294 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
295 |
> |
assertEquals(1, p1.getCorePoolSize()); |
296 |
> |
joinPool(p1); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* Test to verify 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); |
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 |
< |
assertEquals(0, two.getLargestPoolSize()); |
310 |
< |
two.execute(newRunnable()); |
311 |
< |
two.execute(newRunnable()); |
346 |
< |
try{ |
347 |
< |
Thread.sleep(SHORT_DELAY_MS); |
348 |
< |
} catch(Exception e){ |
349 |
< |
fail("unexpected exception"); |
350 |
< |
} |
351 |
< |
assertEquals(2, two.getLargestPoolSize()); |
352 |
< |
} finally { |
353 |
< |
two.shutdown(); |
309 |
> |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
} catch(Exception e){ |
311 |
> |
unexpectedException(); |
312 |
|
} |
313 |
+ |
assertEquals(2, p2.getLargestPoolSize()); |
314 |
+ |
joinPool(p2); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* Test to verify 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 |
< |
try { |
324 |
< |
assertEquals(0, one.getPoolSize()); |
325 |
< |
one.execute(newRunnable()); |
326 |
< |
assertEquals(1, one.getPoolSize()); |
366 |
< |
} finally { |
367 |
< |
one.shutdown(); |
368 |
< |
} |
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 |
< |
* Test to verify 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); |
333 |
> |
public void testGetTaskCount() { |
334 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
335 |
> |
assertEquals(0, p1.getTaskCount()); |
336 |
> |
for(int i = 0; i < 5; i++) |
337 |
> |
p1.execute(new SmallRunnable()); |
338 |
|
try { |
339 |
< |
assertEquals(0, one.getTaskCount()); |
340 |
< |
for(int i = 0; i < 5; i++) |
341 |
< |
one.execute(newRunnable()); |
380 |
< |
try{ |
381 |
< |
Thread.sleep(SHORT_DELAY_MS); |
382 |
< |
} catch(Exception e){ |
383 |
< |
fail("unexpected exception"); |
384 |
< |
} |
385 |
< |
assertEquals(5, one.getTaskCount()); |
386 |
< |
} finally { |
387 |
< |
one.shutdown(); |
339 |
> |
Thread.sleep(SHORT_DELAY_MS); |
340 |
> |
} catch(Exception e){ |
341 |
> |
unexpectedException(); |
342 |
|
} |
343 |
+ |
assertEquals(5, p1.getTaskCount()); |
344 |
+ |
joinPool(p1); |
345 |
|
} |
346 |
|
|
347 |
|
/** |
348 |
< |
* Test to verify 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 |
< |
* Test to verify isTerminated gives correct values |
409 |
< |
* Makes sure termination does not take an innapropriate |
410 |
< |
* 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(newRunnable()); |
369 |
> |
p1.execute(new SmallRunnable()); |
370 |
|
} finally { |
371 |
< |
one.shutdown(); |
371 |
> |
p1.shutdown(); |
372 |
|
} |
373 |
< |
boolean flag = false; |
374 |
< |
try{ |
375 |
< |
flag = one.awaitTermination(10, TimeUnit.SECONDS); |
373 |
> |
try { |
374 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
375 |
> |
assertTrue(p1.isTerminated()); |
376 |
|
} catch(Exception e){ |
377 |
< |
fail("unexpected exception"); |
377 |
> |
unexpectedException(); |
378 |
|
} |
425 |
– |
assertTrue(one.isTerminated()); |
426 |
– |
if(!flag) |
427 |
– |
fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* Test to verify that purge correctly removes cancelled tasks |
432 |
< |
* 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 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
389 |
< |
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); |
388 |
> |
p1.execute(new SmallRunnable()); |
389 |
> |
assertFalse(p1.isTerminating()); |
390 |
|
} finally { |
391 |
< |
one.shutdown(); |
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] = 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 |
+ |
p1.purge(); |
415 |
+ |
long count = p1.getTaskCount(); |
416 |
+ |
assertTrue(count > 0 && count <= max); |
417 |
+ |
joinPool(p1); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* Test to verify shutDownNow returns a list |
454 |
< |
* 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(newRunnable(), 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(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(newNoopRunnable(), SHORT_DELAY_MS/2, 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()); |
453 |
< |
Thread.sleep(SHORT_DELAY_MS); |
452 |
> |
assertTrue(p1.isShutdown()); |
453 |
> |
Thread.sleep(SMALL_DELAY_MS); |
454 |
|
for (int i = 0; i < 5; ++i) { |
455 |
|
assertTrue(tasks[i].isDone()); |
456 |
|
assertFalse(tasks[i].isCancelled()); |
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(newNoopRunnable(), SHORT_DELAY_MS/2, 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(SHORT_DELAY_MS); |
482 |
< |
assertTrue(one.isTerminated()); |
481 |
> |
Thread.sleep(SMALL_DELAY_MS); |
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(newNoopRunnable(), 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(newNoopRunnable(), 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 |
|
|