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 |
> |
* |
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 |
< |
* Test to verify schedule successfully runs the given Callable. |
99 |
< |
* The waiting flag shows that the Callable is not started until |
100 |
< |
* immediately. |
70 |
> |
* |
71 |
|
*/ |
72 |
< |
public void testSchedule1(){ |
73 |
< |
try{ |
72 |
> |
public void testSchedule1() { |
73 |
> |
try { |
74 |
|
MyCallable callable = new MyCallable(); |
75 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
76 |
< |
Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
77 |
< |
assertTrue(callable.waiting); |
75 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
76 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
77 |
> |
assertFalse(callable.done); |
78 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
79 |
|
assertTrue(callable.done); |
80 |
|
assertEquals(Boolean.TRUE, f.get()); |
81 |
< |
one.shutdown(); |
82 |
< |
}catch(RejectedExecutionException e){} |
81 |
> |
p1.shutdown(); |
82 |
> |
joinPool(p1); |
83 |
> |
} catch(RejectedExecutionException e){} |
84 |
|
catch(Exception e){ |
85 |
< |
fail("unexpected exception"); |
85 |
> |
unexpectedException(); |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
< |
* Another version of schedule, only using Runnable instead of Callable |
90 |
> |
* |
91 |
|
*/ |
92 |
< |
public void testSchedule3(){ |
93 |
< |
try{ |
92 |
> |
public void testSchedule3() { |
93 |
> |
try { |
94 |
|
MyRunnable runnable = new MyRunnable(); |
95 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
96 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
97 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
98 |
< |
assertTrue(runnable.waiting); |
95 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
96 |
> |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
97 |
> |
Thread.sleep(SHORT_DELAY_MS); |
98 |
> |
assertFalse(runnable.done); |
99 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
100 |
|
assertTrue(runnable.done); |
101 |
< |
one.shutdown(); |
101 |
> |
p1.shutdown(); |
102 |
> |
joinPool(p1); |
103 |
|
} catch(Exception e){ |
104 |
< |
fail("unexpected exception"); |
104 |
> |
unexpectedException(); |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* The final version of schedule, using both long, TimeUnit and Runnable |
109 |
> |
* |
110 |
|
*/ |
111 |
< |
public void testSchedule4(){ |
112 |
< |
try{ |
111 |
> |
public void testSchedule4() { |
112 |
> |
try { |
113 |
|
MyRunnable runnable = new MyRunnable(); |
114 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
115 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
116 |
< |
// Thread.sleep(505); |
145 |
< |
assertTrue(runnable.waiting); |
114 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
115 |
> |
p1.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
116 |
> |
assertFalse(runnable.done); |
117 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
118 |
|
assertTrue(runnable.done); |
119 |
< |
one.shutdown(); |
119 |
> |
p1.shutdown(); |
120 |
> |
joinPool(p1); |
121 |
|
} catch(Exception e){ |
122 |
< |
fail("unexpected exception"); |
122 |
> |
unexpectedException(); |
123 |
|
} |
124 |
|
} |
125 |
|
|
127 |
|
// exception tests |
128 |
|
|
129 |
|
/** |
130 |
< |
* Test to verify schedule(Runnable, long) throws RejectedExecutionException |
130 |
> |
* schedule(Runnable, long) throws RejectedExecutionException |
131 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
132 |
|
*/ |
133 |
< |
public void testSchedule1_RejectedExecutionException(){ |
134 |
< |
try{ |
135 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
133 |
> |
public void testSchedule1_RejectedExecutionException() { |
134 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
135 |
> |
try { |
136 |
|
se.shutdown(); |
137 |
< |
se.schedule(new Runnable(){ |
138 |
< |
public void run(){} |
139 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
140 |
< |
fail("shoud throw"); |
141 |
< |
}catch(RejectedExecutionException e){} |
137 |
> |
se.schedule(new NoOpRunnable(), |
138 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
139 |
> |
shouldThrow(); |
140 |
> |
} catch(RejectedExecutionException success){ |
141 |
> |
} |
142 |
> |
joinPool(se); |
143 |
> |
|
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
147 |
> |
* schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
148 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
149 |
|
*/ |
150 |
< |
public void testSchedule2_RejectedExecutionException(){ |
151 |
< |
try{ |
152 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
150 |
> |
public void testSchedule2_RejectedExecutionException() { |
151 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
152 |
> |
try { |
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"); |
186 |
< |
}catch(RejectedExecutionException e){} |
154 |
> |
se.schedule(new NoOpCallable(), |
155 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
156 |
> |
shouldThrow(); |
157 |
> |
} catch(RejectedExecutionException success){ |
158 |
> |
} |
159 |
> |
joinPool(se); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
< |
* Test to verify schedule(Callable, long) throws RejectedExecutionException |
163 |
> |
* schedule(Callable, long) throws RejectedExecutionException |
164 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
165 |
|
*/ |
166 |
< |
public void testSchedule3_RejectedExecutionException(){ |
167 |
< |
try{ |
168 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
166 |
> |
public void testSchedule3_RejectedExecutionException() { |
167 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
168 |
> |
try { |
169 |
|
se.shutdown(); |
170 |
< |
se.schedule(new Callable(){ |
171 |
< |
public Object call(){ |
172 |
< |
return Boolean.TRUE; |
173 |
< |
} |
174 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
175 |
< |
fail("should throw"); |
203 |
< |
}catch(RejectedExecutionException e){} |
170 |
> |
se.schedule(new NoOpCallable(), |
171 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
172 |
> |
shouldThrow(); |
173 |
> |
} catch(RejectedExecutionException success){ |
174 |
> |
} |
175 |
> |
joinPool(se); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
179 |
> |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
180 |
|
* RejectedExecutionException. |
181 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
182 |
|
*/ |
183 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
184 |
< |
try{ |
185 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
183 |
> |
public void testScheduleAtFixedRate1_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.scheduleAtFixedRate(new NoOpRunnable(), |
188 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
189 |
> |
shouldThrow(); |
190 |
> |
} catch(RejectedExecutionException success){ |
191 |
> |
} |
192 |
> |
joinPool(se); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
196 |
> |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
197 |
|
* RejectedExecutionException. |
198 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
199 |
|
*/ |
200 |
< |
public void testScheduleAtFixedRate2_RejectedExecutionException(){ |
201 |
< |
try{ |
202 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
200 |
> |
public void testScheduleAtFixedRate2_RejectedExecutionException() { |
201 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
202 |
> |
try { |
203 |
|
se.shutdown(); |
204 |
< |
se.scheduleAtFixedRate(new Runnable(){ |
205 |
< |
public void run(){} |
206 |
< |
}, 1, 100, TimeUnit.SECONDS); |
207 |
< |
fail("should throw"); |
208 |
< |
}catch(RejectedExecutionException e){} |
204 |
> |
se.scheduleAtFixedRate(new NoOpRunnable(), |
205 |
> |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
206 |
> |
shouldThrow(); |
207 |
> |
} catch(RejectedExecutionException success){ |
208 |
> |
} |
209 |
> |
joinPool(se); |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
213 |
> |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
214 |
|
* RejectedExecutionException. |
215 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
216 |
|
*/ |
217 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
218 |
< |
try{ |
219 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
217 |
> |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
218 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
219 |
> |
try { |
220 |
|
se.shutdown(); |
221 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
222 |
< |
public void run(){} |
223 |
< |
}, 100, 100, TimeUnit.SECONDS); |
224 |
< |
fail("should throw"); |
225 |
< |
}catch(RejectedExecutionException e){} |
221 |
> |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
222 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
223 |
> |
shouldThrow(); |
224 |
> |
} catch(RejectedExecutionException success){ |
225 |
> |
} |
226 |
> |
joinPool(se); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
230 |
> |
* scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
231 |
|
* RejectedExecutionException. |
232 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
233 |
|
*/ |
234 |
< |
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
235 |
< |
try{ |
236 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
234 |
> |
public void testScheduleWithFixedDelay2_RejectedExecutionException() { |
235 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
236 |
> |
try { |
237 |
|
se.shutdown(); |
238 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
239 |
< |
public void run(){} |
240 |
< |
}, 1, 100, TimeUnit.SECONDS); |
241 |
< |
fail("should throw"); |
242 |
< |
}catch(RejectedExecutionException e){} |
238 |
> |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
239 |
> |
1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
240 |
> |
shouldThrow(); |
241 |
> |
} catch(RejectedExecutionException success){ |
242 |
> |
} |
243 |
> |
joinPool(se); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* Test to verify execute throws RejectedExecutionException |
247 |
> |
* execute throws RejectedExecutionException |
248 |
|
* This occurs on an attempt to schedule a task on a shutdown executor |
249 |
|
*/ |
250 |
< |
public void testExecute_RejectedExecutionException(){ |
251 |
< |
try{ |
252 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
250 |
> |
public void testExecute_RejectedExecutionException() { |
251 |
> |
ScheduledExecutor se = new ScheduledExecutor(1); |
252 |
> |
try { |
253 |
|
se.shutdown(); |
254 |
< |
se.execute(new Runnable(){ |
255 |
< |
public void run(){} |
256 |
< |
}); |
257 |
< |
fail("should throw"); |
258 |
< |
}catch(RejectedExecutionException e){} |
254 |
> |
se.execute(new NoOpRunnable()); |
255 |
> |
shouldThrow(); |
256 |
> |
} catch(RejectedExecutionException success){ |
257 |
> |
} |
258 |
> |
joinPool(se); |
259 |
|
} |
260 |
|
|
285 |
– |
|
286 |
– |
|
261 |
|
/** |
262 |
< |
* Test to verify getActiveCount gives correct values |
262 |
> |
* getActiveCount gives correct values |
263 |
|
*/ |
264 |
< |
public void testGetActiveCount(){ |
265 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
264 |
> |
public void testGetActiveCount() { |
265 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
266 |
> |
assertEquals(0, p2.getActiveCount()); |
267 |
> |
p2.execute(new SmallRunnable()); |
268 |
|
try { |
269 |
< |
assertEquals(0, two.getActiveCount()); |
270 |
< |
two.execute(newRunnable()); |
271 |
< |
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(); |
269 |
> |
Thread.sleep(SHORT_DELAY_MS); |
270 |
> |
} catch(Exception e){ |
271 |
> |
unexpectedException(); |
272 |
|
} |
273 |
+ |
assertEquals(1, p2.getActiveCount()); |
274 |
+ |
joinPool(p2); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
< |
* Test to verify getCompleteTaskCount gives correct values |
278 |
> |
* getCompleteTaskCount gives correct values |
279 |
|
*/ |
280 |
< |
public void testGetCompletedTaskCount(){ |
281 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
280 |
> |
public void testGetCompletedTaskCount() { |
281 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
282 |
> |
assertEquals(0, p2.getCompletedTaskCount()); |
283 |
> |
p2.execute(new SmallRunnable()); |
284 |
|
try { |
285 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
286 |
< |
two.execute(newRunnable()); |
287 |
< |
try{ |
315 |
< |
Thread.sleep(MEDIUM_DELAY_MS/2); |
316 |
< |
} catch(Exception e){ |
317 |
< |
fail("unexpected exception"); |
318 |
< |
} |
319 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
320 |
< |
} finally { |
321 |
< |
two.shutdown(); |
285 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
286 |
> |
} catch(Exception e){ |
287 |
> |
unexpectedException(); |
288 |
|
} |
289 |
+ |
assertEquals(1, p2.getCompletedTaskCount()); |
290 |
+ |
joinPool(p2); |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* Test to verify getCorePoolSize gives correct values |
294 |
> |
* getCorePoolSize gives correct values |
295 |
|
*/ |
296 |
< |
public void testGetCorePoolSize(){ |
297 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
298 |
< |
try { |
299 |
< |
assertEquals(1, one.getCorePoolSize()); |
332 |
< |
} finally { |
333 |
< |
one.shutdown(); |
334 |
< |
} |
296 |
> |
public void testGetCorePoolSize() { |
297 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
298 |
> |
assertEquals(1, p1.getCorePoolSize()); |
299 |
> |
joinPool(p1); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* Test to verify getLargestPoolSize gives correct values |
303 |
> |
* getLargestPoolSize gives correct values |
304 |
|
*/ |
305 |
< |
public void testGetLargestPoolSize(){ |
306 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
305 |
> |
public void testGetLargestPoolSize() { |
306 |
> |
ScheduledExecutor p2 = new ScheduledExecutor(2); |
307 |
> |
assertEquals(0, p2.getLargestPoolSize()); |
308 |
> |
p2.execute(new SmallRunnable()); |
309 |
> |
p2.execute(new SmallRunnable()); |
310 |
|
try { |
311 |
< |
assertEquals(0, two.getLargestPoolSize()); |
312 |
< |
two.execute(newRunnable()); |
313 |
< |
two.execute(newRunnable()); |
346 |
< |
try{ |
347 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
348 |
< |
} catch(Exception e){ |
349 |
< |
fail("unexpected exception"); |
350 |
< |
} |
351 |
< |
assertEquals(2, two.getLargestPoolSize()); |
352 |
< |
} finally { |
353 |
< |
two.shutdown(); |
311 |
> |
Thread.sleep(SHORT_DELAY_MS); |
312 |
> |
} catch(Exception e){ |
313 |
> |
unexpectedException(); |
314 |
|
} |
315 |
+ |
assertEquals(2, p2.getLargestPoolSize()); |
316 |
+ |
joinPool(p2); |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* Test to verify getPoolSize gives correct values |
320 |
> |
* getPoolSize gives correct values |
321 |
|
*/ |
322 |
< |
public void testGetPoolSize(){ |
323 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
324 |
< |
try { |
325 |
< |
assertEquals(0, one.getPoolSize()); |
326 |
< |
one.execute(newRunnable()); |
327 |
< |
assertEquals(1, one.getPoolSize()); |
366 |
< |
} finally { |
367 |
< |
one.shutdown(); |
368 |
< |
} |
322 |
> |
public void testGetPoolSize() { |
323 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
324 |
> |
assertEquals(0, p1.getPoolSize()); |
325 |
> |
p1.execute(new SmallRunnable()); |
326 |
> |
assertEquals(1, p1.getPoolSize()); |
327 |
> |
joinPool(p1); |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
< |
* Test to verify getTaskCount gives correct values |
331 |
> |
* getTaskCount gives correct values |
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/2); |
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 |
> |
* isShutDown gives correct values |
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 gives correct values |
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 gives correct values |
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 |
< |
* Test to verify shutDownNow returns a list |
403 |
> |
* that purge correctly removes cancelled tasks |
404 |
> |
* from the queue |
405 |
> |
*/ |
406 |
> |
public void testPurge() { |
407 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
408 |
> |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
409 |
> |
for(int i = 0; i < 5; i++){ |
410 |
> |
tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
411 |
> |
} |
412 |
> |
int max = 5; |
413 |
> |
if (tasks[4].cancel(true)) --max; |
414 |
> |
if (tasks[3].cancel(true)) --max; |
415 |
> |
p1.purge(); |
416 |
> |
long count = p1.getTaskCount(); |
417 |
> |
assertTrue(count > 0 && count <= max); |
418 |
> |
joinPool(p1); |
419 |
> |
} |
420 |
> |
|
421 |
> |
/** |
422 |
> |
* shutDownNow returns a list |
423 |
|
* containing the correct number of elements |
424 |
|
*/ |
425 |
< |
public void testShutDownNow(){ |
426 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
425 |
> |
public void testShutDownNow() { |
426 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
427 |
|
for(int i = 0; i < 5; i++) |
428 |
< |
one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
429 |
< |
List l = one.shutdownNow(); |
430 |
< |
assertTrue(one.isShutdown()); |
428 |
> |
p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
429 |
> |
List l = p1.shutdownNow(); |
430 |
> |
assertTrue(p1.isShutdown()); |
431 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
432 |
+ |
joinPool(p1); |
433 |
|
} |
434 |
|
|
435 |
< |
public void testShutDown1(){ |
435 |
> |
/** |
436 |
> |
* |
437 |
> |
*/ |
438 |
> |
public void testShutDown1() { |
439 |
|
try { |
440 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
441 |
< |
assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
442 |
< |
assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
440 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
441 |
> |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
442 |
> |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
443 |
|
|
444 |
|
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
445 |
|
for(int i = 0; i < 5; i++) |
446 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
447 |
< |
one.shutdown(); |
448 |
< |
BlockingQueue q = one.getQueue(); |
446 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
447 |
> |
p1.shutdown(); |
448 |
> |
BlockingQueue q = p1.getQueue(); |
449 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
450 |
|
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
451 |
|
assertFalse(t.isCancelled()); |
452 |
|
} |
453 |
< |
assertTrue(one.isShutdown()); |
454 |
< |
Thread.sleep(SHORT_DELAY_MS); |
453 |
> |
assertTrue(p1.isShutdown()); |
454 |
> |
Thread.sleep(SMALL_DELAY_MS); |
455 |
|
for (int i = 0; i < 5; ++i) { |
456 |
|
assertTrue(tasks[i].isDone()); |
457 |
|
assertFalse(tasks[i].isCancelled()); |
459 |
|
|
460 |
|
} |
461 |
|
catch(Exception ex) { |
462 |
< |
fail("unexpected exception"); |
462 |
> |
unexpectedException(); |
463 |
|
} |
464 |
|
} |
465 |
|
|
466 |
|
|
467 |
< |
public void testShutDown2(){ |
467 |
> |
/** |
468 |
> |
* |
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 |
> |
* |
492 |
> |
*/ |
493 |
> |
public void testShutDown3() { |
494 |
|
try { |
495 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
496 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
495 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
496 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
497 |
|
ScheduledCancellable task = |
498 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
499 |
< |
one.shutdown(); |
500 |
< |
assertTrue(one.isShutdown()); |
501 |
< |
BlockingQueue q = one.getQueue(); |
498 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
499 |
> |
p1.shutdown(); |
500 |
> |
assertTrue(p1.isShutdown()); |
501 |
> |
BlockingQueue q = p1.getQueue(); |
502 |
|
assertTrue(q.isEmpty()); |
503 |
|
Thread.sleep(SHORT_DELAY_MS); |
504 |
< |
assertTrue(one.isTerminated()); |
504 |
> |
assertTrue(p1.isTerminated()); |
505 |
|
} |
506 |
|
catch(Exception ex) { |
507 |
< |
fail("unexpected exception"); |
507 |
> |
unexpectedException(); |
508 |
|
} |
509 |
|
} |
510 |
|
|
511 |
< |
public void testShutDown4(){ |
512 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
511 |
> |
/** |
512 |
> |
* |
513 |
> |
*/ |
514 |
> |
public void testShutDown4() { |
515 |
> |
ScheduledExecutor p1 = new ScheduledExecutor(1); |
516 |
|
try { |
517 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
517 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
518 |
|
ScheduledCancellable task = |
519 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
519 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
520 |
|
assertFalse(task.isCancelled()); |
521 |
< |
one.shutdown(); |
521 |
> |
p1.shutdown(); |
522 |
|
assertFalse(task.isCancelled()); |
523 |
< |
assertFalse(one.isTerminated()); |
524 |
< |
assertTrue(one.isShutdown()); |
523 |
> |
assertFalse(p1.isTerminated()); |
524 |
> |
assertTrue(p1.isShutdown()); |
525 |
|
Thread.sleep(SHORT_DELAY_MS); |
526 |
|
assertFalse(task.isCancelled()); |
527 |
|
task.cancel(true); |
528 |
|
assertTrue(task.isCancelled()); |
529 |
|
Thread.sleep(SHORT_DELAY_MS); |
530 |
< |
assertTrue(one.isTerminated()); |
530 |
> |
assertTrue(p1.isTerminated()); |
531 |
|
} |
532 |
|
catch(Exception ex) { |
533 |
< |
fail("unexpected exception"); |
533 |
> |
unexpectedException(); |
534 |
|
} |
535 |
|
finally { |
536 |
< |
one.shutdownNow(); |
536 |
> |
p1.shutdownNow(); |
537 |
|
} |
538 |
|
} |
539 |
|
|