17 |
|
return new TestSuite(ScheduledExecutorTest.class); |
18 |
|
} |
19 |
|
|
20 |
– |
static class MyRunnable implements Runnable { |
21 |
– |
volatile boolean done = false; |
22 |
– |
public void run(){ |
23 |
– |
try{ |
24 |
– |
Thread.sleep(SMALL_DELAY_MS); |
25 |
– |
done = true; |
26 |
– |
} catch(Exception e){ |
27 |
– |
} |
28 |
– |
} |
29 |
– |
} |
30 |
– |
|
31 |
– |
static class MyCallable implements Callable { |
32 |
– |
volatile boolean done = false; |
33 |
– |
public Object call(){ |
34 |
– |
try{ |
35 |
– |
Thread.sleep(SMALL_DELAY_MS); |
36 |
– |
done = true; |
37 |
– |
}catch(Exception e){} |
38 |
– |
return Boolean.TRUE; |
39 |
– |
} |
40 |
– |
} |
20 |
|
|
21 |
< |
public void testExecute(){ |
22 |
< |
try{ |
23 |
< |
MyRunnable runnable =new MyRunnable(); |
24 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
25 |
< |
one.execute(runnable); |
21 |
> |
/** |
22 |
> |
* execute successfully executes a runnable |
23 |
> |
*/ |
24 |
> |
public void testExecute() { |
25 |
> |
try { |
26 |
> |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
27 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
28 |
> |
p1.execute(runnable); |
29 |
|
assertFalse(runnable.done); |
30 |
|
Thread.sleep(SHORT_DELAY_MS); |
31 |
< |
one.shutdown(); |
32 |
< |
try{ |
31 |
> |
p1.shutdown(); |
32 |
> |
try { |
33 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
34 |
|
} catch(InterruptedException e){ |
35 |
< |
fail("unexpected exception"); |
35 |
> |
unexpectedException(); |
36 |
|
} |
37 |
|
assertTrue(runnable.done); |
38 |
< |
one.shutdown(); |
39 |
< |
joinPool(one); |
38 |
> |
p1.shutdown(); |
39 |
> |
joinPool(p1); |
40 |
|
} |
41 |
|
catch(Exception e){ |
42 |
< |
fail("unexpected exception"); |
42 |
> |
unexpectedException(); |
43 |
|
} |
44 |
|
|
45 |
|
} |
46 |
|
|
47 |
< |
public void testSchedule1(){ |
48 |
< |
try{ |
49 |
< |
MyCallable callable = new MyCallable(); |
50 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
51 |
< |
Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
47 |
> |
|
48 |
> |
/** |
49 |
> |
* delayed schedule of callable successfully executes after delay |
50 |
> |
*/ |
51 |
> |
public void testSchedule1() { |
52 |
> |
try { |
53 |
> |
TrackedCallable callable = new TrackedCallable(); |
54 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
55 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
56 |
|
assertFalse(callable.done); |
57 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
58 |
|
assertTrue(callable.done); |
59 |
|
assertEquals(Boolean.TRUE, f.get()); |
60 |
< |
one.shutdown(); |
61 |
< |
joinPool(one); |
62 |
< |
}catch(RejectedExecutionException e){} |
60 |
> |
p1.shutdown(); |
61 |
> |
joinPool(p1); |
62 |
> |
} catch(RejectedExecutionException e){} |
63 |
|
catch(Exception e){ |
64 |
< |
fail("unexpected exception"); |
64 |
> |
e.printStackTrace(); |
65 |
> |
unexpectedException(); |
66 |
|
} |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* Another version of schedule, only using Runnable instead of Callable |
70 |
> |
* delayed schedule of runnable successfully executes after delay |
71 |
|
*/ |
72 |
< |
public void testSchedule3(){ |
73 |
< |
try{ |
74 |
< |
MyRunnable runnable = new MyRunnable(); |
75 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
76 |
< |
one.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
72 |
> |
public void testSchedule3() { |
73 |
> |
try { |
74 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
75 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
76 |
> |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
77 |
|
Thread.sleep(SHORT_DELAY_MS); |
78 |
|
assertFalse(runnable.done); |
79 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
80 |
|
assertTrue(runnable.done); |
81 |
< |
one.shutdown(); |
82 |
< |
joinPool(one); |
81 |
> |
p1.shutdown(); |
82 |
> |
joinPool(p1); |
83 |
|
} catch(Exception e){ |
84 |
< |
fail("unexpected exception"); |
84 |
> |
unexpectedException(); |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* The final version of schedule, using both long, TimeUnit and Runnable |
89 |
> |
* scheduleAtFixedRate executes runnable after given initial delay |
90 |
> |
*/ |
91 |
> |
public void testSchedule4() { |
92 |
> |
try { |
93 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
94 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
95 |
> |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
96 |
> |
assertFalse(runnable.done); |
97 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
98 |
> |
assertTrue(runnable.done); |
99 |
> |
h.cancel(true); |
100 |
> |
p1.shutdown(); |
101 |
> |
joinPool(p1); |
102 |
> |
} catch(Exception e){ |
103 |
> |
unexpectedException(); |
104 |
> |
} |
105 |
> |
} |
106 |
> |
|
107 |
> |
/** |
108 |
> |
* scheduleWithFixedDelay executes runnable after given initial delay |
109 |
|
*/ |
110 |
< |
public void testSchedule4(){ |
111 |
< |
try{ |
112 |
< |
MyRunnable runnable = new MyRunnable(); |
113 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
114 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
110 |
> |
public void testSchedule5() { |
111 |
> |
try { |
112 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
113 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
114 |
> |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
115 |
|
assertFalse(runnable.done); |
116 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
117 |
|
assertTrue(runnable.done); |
118 |
< |
one.shutdown(); |
119 |
< |
joinPool(one); |
118 |
> |
h.cancel(true); |
119 |
> |
p1.shutdown(); |
120 |
> |
joinPool(p1); |
121 |
|
} catch(Exception e){ |
122 |
< |
fail("unexpected exception"); |
122 |
> |
unexpectedException(); |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
< |
|
127 |
< |
// exception tests |
126 |
> |
/** |
127 |
> |
* execute (null) throws NPE |
128 |
> |
*/ |
129 |
> |
public void testExecuteNull() { |
130 |
> |
ScheduledThreadPoolExecutor se = null; |
131 |
> |
try { |
132 |
> |
se = new ScheduledThreadPoolExecutor(1); |
133 |
> |
se.execute(null); |
134 |
> |
shouldThrow(); |
135 |
> |
} catch(NullPointerException success){} |
136 |
> |
catch(Exception e){ |
137 |
> |
unexpectedException(); |
138 |
> |
} |
139 |
> |
|
140 |
> |
joinPool(se); |
141 |
> |
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* schedule(Runnable, long) throws RejectedExecutionException |
124 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
144 |
> |
* schedule (null) throws NPE |
145 |
|
*/ |
146 |
< |
public void testSchedule1_RejectedExecutionException(){ |
147 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
148 |
< |
try{ |
146 |
> |
public void testScheduleNull() { |
147 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
148 |
> |
try { |
149 |
> |
TrackedCallable callable = null; |
150 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
151 |
> |
shouldThrow(); |
152 |
> |
} catch(NullPointerException success){} |
153 |
> |
catch(Exception e){ |
154 |
> |
unexpectedException(); |
155 |
> |
} |
156 |
> |
joinPool(se); |
157 |
> |
} |
158 |
> |
|
159 |
> |
/** |
160 |
> |
* execute throws RejectedExecutionException if shutdown |
161 |
> |
*/ |
162 |
> |
public void testSchedule1_RejectedExecutionException() { |
163 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
164 |
> |
try { |
165 |
|
se.shutdown(); |
166 |
|
se.schedule(new NoOpRunnable(), |
167 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
168 |
< |
fail("shoud throw"); |
168 |
> |
shouldThrow(); |
169 |
|
} catch(RejectedExecutionException success){ |
170 |
|
} |
171 |
|
joinPool(se); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
141 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
176 |
> |
* schedule throws RejectedExecutionException if shutdown |
177 |
|
*/ |
178 |
< |
public void testSchedule2_RejectedExecutionException(){ |
179 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
180 |
< |
try{ |
178 |
> |
public void testSchedule2_RejectedExecutionException() { |
179 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
180 |
> |
try { |
181 |
|
se.shutdown(); |
182 |
|
se.schedule(new NoOpCallable(), |
183 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
184 |
< |
fail("should throw"); |
184 |
> |
shouldThrow(); |
185 |
|
} catch(RejectedExecutionException success){ |
186 |
|
} |
187 |
|
joinPool(se); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* schedule(Callable, long) throws RejectedExecutionException |
157 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
191 |
> |
* schedule callable throws RejectedExecutionException if shutdown |
192 |
|
*/ |
193 |
< |
public void testSchedule3_RejectedExecutionException(){ |
194 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
195 |
< |
try{ |
193 |
> |
public void testSchedule3_RejectedExecutionException() { |
194 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
195 |
> |
try { |
196 |
|
se.shutdown(); |
197 |
|
se.schedule(new NoOpCallable(), |
198 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
199 |
< |
fail("should throw"); |
199 |
> |
shouldThrow(); |
200 |
|
} catch(RejectedExecutionException success){ |
201 |
|
} |
202 |
|
joinPool(se); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
207 |
< |
* RejectedExecutionException. |
208 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
209 |
< |
*/ |
210 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
177 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
178 |
< |
try{ |
206 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
207 |
> |
*/ |
208 |
> |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
209 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
210 |
> |
try { |
211 |
|
se.shutdown(); |
212 |
|
se.scheduleAtFixedRate(new NoOpRunnable(), |
213 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
214 |
< |
fail("should throw"); |
214 |
> |
shouldThrow(); |
215 |
|
} catch(RejectedExecutionException success){ |
216 |
|
} |
217 |
|
joinPool(se); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
222 |
< |
* RejectedExecutionException. |
223 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
224 |
< |
*/ |
225 |
< |
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 |
209 |
< |
*/ |
210 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
211 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
212 |
< |
try{ |
221 |
> |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
222 |
> |
*/ |
223 |
> |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
224 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
225 |
> |
try { |
226 |
|
se.shutdown(); |
227 |
|
se.scheduleWithFixedDelay(new NoOpRunnable(), |
228 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
229 |
< |
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"); |
229 |
> |
shouldThrow(); |
230 |
|
} catch(RejectedExecutionException success){ |
231 |
|
} |
232 |
|
joinPool(se); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* execute throws RejectedExecutionException |
237 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
236 |
> |
* getActiveCount increases but doesn't overestimate, when a |
237 |
> |
* thread becomes active |
238 |
|
*/ |
239 |
< |
public void testExecute_RejectedExecutionException(){ |
240 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
241 |
< |
try{ |
242 |
< |
se.shutdown(); |
243 |
< |
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{ |
239 |
> |
public void testGetActiveCount() { |
240 |
> |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
241 |
> |
assertEquals(0, p2.getActiveCount()); |
242 |
> |
p2.execute(new SmallRunnable()); |
243 |
> |
try { |
244 |
|
Thread.sleep(SHORT_DELAY_MS); |
245 |
|
} catch(Exception e){ |
246 |
< |
fail("unexpected exception"); |
246 |
> |
unexpectedException(); |
247 |
|
} |
248 |
< |
assertEquals(1, two.getActiveCount()); |
249 |
< |
joinPool(two); |
248 |
> |
assertEquals(1, p2.getActiveCount()); |
249 |
> |
joinPool(p2); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* getCompleteTaskCount gives correct values |
253 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
254 |
> |
* when tasks complete |
255 |
|
*/ |
256 |
< |
public void testGetCompletedTaskCount(){ |
257 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
258 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
259 |
< |
two.execute(new SmallRunnable()); |
260 |
< |
try{ |
256 |
> |
public void testGetCompletedTaskCount() { |
257 |
> |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
258 |
> |
assertEquals(0, p2.getCompletedTaskCount()); |
259 |
> |
p2.execute(new SmallRunnable()); |
260 |
> |
try { |
261 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
262 |
|
} catch(Exception e){ |
263 |
< |
fail("unexpected exception"); |
263 |
> |
unexpectedException(); |
264 |
|
} |
265 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
266 |
< |
joinPool(two); |
265 |
> |
assertEquals(1, p2.getCompletedTaskCount()); |
266 |
> |
joinPool(p2); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
270 |
< |
* getCorePoolSize gives correct values |
270 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
271 |
|
*/ |
272 |
< |
public void testGetCorePoolSize(){ |
273 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
274 |
< |
assertEquals(1, one.getCorePoolSize()); |
275 |
< |
joinPool(one); |
272 |
> |
public void testGetCorePoolSize() { |
273 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
274 |
> |
assertEquals(1, p1.getCorePoolSize()); |
275 |
> |
joinPool(p1); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
< |
* getLargestPoolSize gives correct values |
279 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
280 |
> |
* multiple threads active |
281 |
|
*/ |
282 |
< |
public void testGetLargestPoolSize(){ |
283 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
284 |
< |
assertEquals(0, two.getLargestPoolSize()); |
285 |
< |
two.execute(new SmallRunnable()); |
286 |
< |
two.execute(new SmallRunnable()); |
287 |
< |
try{ |
282 |
> |
public void testGetLargestPoolSize() { |
283 |
> |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
284 |
> |
assertEquals(0, p2.getLargestPoolSize()); |
285 |
> |
p2.execute(new SmallRunnable()); |
286 |
> |
p2.execute(new SmallRunnable()); |
287 |
> |
try { |
288 |
|
Thread.sleep(SHORT_DELAY_MS); |
289 |
|
} catch(Exception e){ |
290 |
< |
fail("unexpected exception"); |
290 |
> |
unexpectedException(); |
291 |
|
} |
292 |
< |
assertEquals(2, two.getLargestPoolSize()); |
293 |
< |
joinPool(two); |
292 |
> |
assertEquals(2, p2.getLargestPoolSize()); |
293 |
> |
joinPool(p2); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
< |
* getPoolSize gives correct values |
297 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
298 |
> |
* become active |
299 |
|
*/ |
300 |
< |
public void testGetPoolSize(){ |
301 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
302 |
< |
assertEquals(0, one.getPoolSize()); |
303 |
< |
one.execute(new SmallRunnable()); |
304 |
< |
assertEquals(1, one.getPoolSize()); |
305 |
< |
joinPool(one); |
300 |
> |
public void testGetPoolSize() { |
301 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
302 |
> |
assertEquals(0, p1.getPoolSize()); |
303 |
> |
p1.execute(new SmallRunnable()); |
304 |
> |
assertEquals(1, p1.getPoolSize()); |
305 |
> |
joinPool(p1); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
< |
* getTaskCount gives correct values |
309 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
310 |
> |
* submitted |
311 |
|
*/ |
312 |
< |
public void testGetTaskCount(){ |
313 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
314 |
< |
assertEquals(0, one.getTaskCount()); |
312 |
> |
public void testGetTaskCount() { |
313 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
314 |
> |
assertEquals(0, p1.getTaskCount()); |
315 |
|
for(int i = 0; i < 5; i++) |
316 |
< |
one.execute(new SmallRunnable()); |
317 |
< |
try{ |
316 |
> |
p1.execute(new SmallRunnable()); |
317 |
> |
try { |
318 |
|
Thread.sleep(SHORT_DELAY_MS); |
319 |
|
} catch(Exception e){ |
320 |
< |
fail("unexpected exception"); |
320 |
> |
unexpectedException(); |
321 |
> |
} |
322 |
> |
assertEquals(5, p1.getTaskCount()); |
323 |
> |
joinPool(p1); |
324 |
> |
} |
325 |
> |
|
326 |
> |
/** |
327 |
> |
* getThreadFactory returns factory in constructor if not set |
328 |
> |
*/ |
329 |
> |
public void testGetThreadFactory() { |
330 |
> |
ThreadFactory tf = new SimpleThreadFactory(); |
331 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
332 |
> |
assertSame(tf, p.getThreadFactory()); |
333 |
> |
p.shutdown(); |
334 |
> |
joinPool(p); |
335 |
> |
} |
336 |
> |
|
337 |
> |
/** |
338 |
> |
* setThreadFactory sets the thread factory returned by getThreadFactory |
339 |
> |
*/ |
340 |
> |
public void testSetThreadFactory() { |
341 |
> |
ThreadFactory tf = new SimpleThreadFactory(); |
342 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
343 |
> |
p.setThreadFactory(tf); |
344 |
> |
assertSame(tf, p.getThreadFactory()); |
345 |
> |
p.shutdown(); |
346 |
> |
joinPool(p); |
347 |
> |
} |
348 |
> |
|
349 |
> |
/** |
350 |
> |
* setThreadFactory(null) throws NPE |
351 |
> |
*/ |
352 |
> |
public void testSetThreadFactoryNull() { |
353 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
354 |
> |
try { |
355 |
> |
p.setThreadFactory(null); |
356 |
> |
shouldThrow(); |
357 |
> |
} catch (NullPointerException success) { |
358 |
> |
} finally { |
359 |
> |
joinPool(p); |
360 |
|
} |
336 |
– |
assertEquals(5, one.getTaskCount()); |
337 |
– |
joinPool(one); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* isShutDown gives correct values |
364 |
> |
* is isShutDown is false before shutdown, true after |
365 |
|
*/ |
366 |
< |
public void testIsShutdown(){ |
366 |
> |
public void testIsShutdown() { |
367 |
|
|
368 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
368 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
369 |
|
try { |
370 |
< |
assertFalse(one.isShutdown()); |
370 |
> |
assertFalse(p1.isShutdown()); |
371 |
|
} |
372 |
|
finally { |
373 |
< |
one.shutdown(); |
373 |
> |
p1.shutdown(); |
374 |
|
} |
375 |
< |
assertTrue(one.isShutdown()); |
375 |
> |
assertTrue(p1.isShutdown()); |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
/** |
380 |
< |
* isTerminated gives correct values |
381 |
< |
* Makes sure termination does not take an innapropriate |
382 |
< |
* amount of time |
380 |
> |
* isTerminated is false before termination, true after |
381 |
> |
*/ |
382 |
> |
public void testIsTerminated() { |
383 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
384 |
> |
try { |
385 |
> |
p1.execute(new SmallRunnable()); |
386 |
> |
} finally { |
387 |
> |
p1.shutdown(); |
388 |
> |
} |
389 |
> |
try { |
390 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
391 |
> |
assertTrue(p1.isTerminated()); |
392 |
> |
} catch(Exception e){ |
393 |
> |
unexpectedException(); |
394 |
> |
} |
395 |
> |
} |
396 |
> |
|
397 |
> |
/** |
398 |
> |
* isTerminating is not true when running or when terminated |
399 |
|
*/ |
400 |
< |
public void testIsTerminated(){ |
401 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
400 |
> |
public void testIsTerminating() { |
401 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
402 |
> |
assertFalse(p1.isTerminating()); |
403 |
|
try { |
404 |
< |
one.execute(new SmallRunnable()); |
404 |
> |
p1.execute(new SmallRunnable()); |
405 |
> |
assertFalse(p1.isTerminating()); |
406 |
|
} finally { |
407 |
< |
one.shutdown(); |
407 |
> |
p1.shutdown(); |
408 |
|
} |
409 |
|
try { |
410 |
< |
assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
411 |
< |
assertTrue(one.isTerminated()); |
410 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
411 |
> |
assertTrue(p1.isTerminated()); |
412 |
> |
assertFalse(p1.isTerminating()); |
413 |
|
} catch(Exception e){ |
414 |
< |
fail("unexpected exception"); |
414 |
> |
unexpectedException(); |
415 |
|
} |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
< |
* that purge correctly removes cancelled tasks |
378 |
< |
* from the queue |
419 |
> |
* getQueue returns the work queue, which contains queued tasks |
420 |
|
*/ |
421 |
< |
public void testPurge(){ |
422 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
423 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
421 |
> |
public void testGetQueue() { |
422 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
423 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
424 |
|
for(int i = 0; i < 5; i++){ |
425 |
< |
tasks[i] = one.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS); |
425 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
426 |
> |
} |
427 |
> |
try { |
428 |
> |
Thread.sleep(SHORT_DELAY_MS); |
429 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
430 |
> |
assertTrue(q.contains(tasks[4])); |
431 |
> |
assertFalse(q.contains(tasks[0])); |
432 |
> |
p1.shutdownNow(); |
433 |
> |
} catch(Exception e) { |
434 |
> |
unexpectedException(); |
435 |
> |
} finally { |
436 |
> |
joinPool(p1); |
437 |
> |
} |
438 |
> |
} |
439 |
> |
|
440 |
> |
/** |
441 |
> |
* remove(task) removes queued task, and fails to remove active task |
442 |
> |
*/ |
443 |
> |
public void testRemove() { |
444 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
445 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
446 |
> |
for(int i = 0; i < 5; i++){ |
447 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
448 |
> |
} |
449 |
> |
try { |
450 |
> |
Thread.sleep(SHORT_DELAY_MS); |
451 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
452 |
> |
assertFalse(p1.remove((Runnable)tasks[0])); |
453 |
> |
assertTrue(q.contains((Runnable)tasks[4])); |
454 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
455 |
> |
assertTrue(p1.remove((Runnable)tasks[4])); |
456 |
> |
assertFalse(p1.remove((Runnable)tasks[4])); |
457 |
> |
assertFalse(q.contains((Runnable)tasks[4])); |
458 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
459 |
> |
assertTrue(p1.remove((Runnable)tasks[3])); |
460 |
> |
assertFalse(q.contains((Runnable)tasks[3])); |
461 |
> |
p1.shutdownNow(); |
462 |
> |
} catch(Exception e) { |
463 |
> |
unexpectedException(); |
464 |
> |
} finally { |
465 |
> |
joinPool(p1); |
466 |
> |
} |
467 |
> |
} |
468 |
> |
|
469 |
> |
/** |
470 |
> |
* purge removes cancelled tasks from the queue |
471 |
> |
*/ |
472 |
> |
public void testPurge() { |
473 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
474 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
475 |
> |
for(int i = 0; i < 5; i++){ |
476 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
477 |
|
} |
478 |
|
int max = 5; |
479 |
|
if (tasks[4].cancel(true)) --max; |
480 |
|
if (tasks[3].cancel(true)) --max; |
481 |
< |
one.purge(); |
482 |
< |
long count = one.getTaskCount(); |
481 |
> |
p1.purge(); |
482 |
> |
long count = p1.getTaskCount(); |
483 |
|
assertTrue(count > 0 && count <= max); |
484 |
< |
joinPool(one); |
484 |
> |
joinPool(p1); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
< |
* shutDownNow returns a list |
397 |
< |
* containing the correct number of elements |
488 |
> |
* shutDownNow returns a list containing tasks that were not run |
489 |
|
*/ |
490 |
< |
public void testShutDownNow(){ |
491 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
490 |
> |
public void testShutDownNow() { |
491 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
492 |
|
for(int i = 0; i < 5; i++) |
493 |
< |
one.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
494 |
< |
List l = one.shutdownNow(); |
495 |
< |
assertTrue(one.isShutdown()); |
493 |
> |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
494 |
> |
List l = p1.shutdownNow(); |
495 |
> |
assertTrue(p1.isShutdown()); |
496 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
497 |
< |
joinPool(one); |
497 |
> |
joinPool(p1); |
498 |
|
} |
499 |
|
|
500 |
< |
public void testShutDown1(){ |
500 |
> |
/** |
501 |
> |
* In default setting, shutdown cancels periodic but not delayed |
502 |
> |
* tasks at shutdown |
503 |
> |
*/ |
504 |
> |
public void testShutDown1() { |
505 |
|
try { |
506 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
507 |
< |
assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
508 |
< |
assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
506 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
507 |
> |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
508 |
> |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
509 |
|
|
510 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
510 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
511 |
|
for(int i = 0; i < 5; i++) |
512 |
< |
tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
513 |
< |
one.shutdown(); |
514 |
< |
BlockingQueue q = one.getQueue(); |
512 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
513 |
> |
p1.shutdown(); |
514 |
> |
BlockingQueue q = p1.getQueue(); |
515 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
516 |
< |
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
516 |
> |
ScheduledFuture t = (ScheduledFuture)it.next(); |
517 |
|
assertFalse(t.isCancelled()); |
518 |
|
} |
519 |
< |
assertTrue(one.isShutdown()); |
519 |
> |
assertTrue(p1.isShutdown()); |
520 |
|
Thread.sleep(SMALL_DELAY_MS); |
521 |
|
for (int i = 0; i < 5; ++i) { |
522 |
|
assertTrue(tasks[i].isDone()); |
525 |
|
|
526 |
|
} |
527 |
|
catch(Exception ex) { |
528 |
< |
fail("unexpected exception"); |
528 |
> |
unexpectedException(); |
529 |
|
} |
530 |
|
} |
531 |
|
|
532 |
|
|
533 |
< |
public void testShutDown2(){ |
533 |
> |
/** |
534 |
> |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
535 |
> |
* delayed tasks are cancelled at shutdown |
536 |
> |
*/ |
537 |
> |
public void testShutDown2() { |
538 |
|
try { |
539 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
540 |
< |
one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
541 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
539 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
540 |
> |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
541 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
542 |
|
for(int i = 0; i < 5; i++) |
543 |
< |
tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
544 |
< |
one.shutdown(); |
545 |
< |
assertTrue(one.isShutdown()); |
546 |
< |
BlockingQueue q = one.getQueue(); |
543 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
544 |
> |
p1.shutdown(); |
545 |
> |
assertTrue(p1.isShutdown()); |
546 |
> |
BlockingQueue q = p1.getQueue(); |
547 |
|
assertTrue(q.isEmpty()); |
548 |
|
Thread.sleep(SMALL_DELAY_MS); |
549 |
< |
assertTrue(one.isTerminated()); |
549 |
> |
assertTrue(p1.isTerminated()); |
550 |
|
} |
551 |
|
catch(Exception ex) { |
552 |
< |
fail("unexpected exception"); |
552 |
> |
unexpectedException(); |
553 |
|
} |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
< |
public void testShutDown3(){ |
557 |
> |
/** |
558 |
> |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
559 |
> |
* periodic tasks are not cancelled at shutdown |
560 |
> |
*/ |
561 |
> |
public void testShutDown3() { |
562 |
|
try { |
563 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
564 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
565 |
< |
ScheduledCancellable task = |
566 |
< |
one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
567 |
< |
one.shutdown(); |
568 |
< |
assertTrue(one.isShutdown()); |
569 |
< |
BlockingQueue q = one.getQueue(); |
563 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
564 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
565 |
> |
ScheduledFuture task = |
566 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
567 |
> |
p1.shutdown(); |
568 |
> |
assertTrue(p1.isShutdown()); |
569 |
> |
BlockingQueue q = p1.getQueue(); |
570 |
|
assertTrue(q.isEmpty()); |
571 |
|
Thread.sleep(SHORT_DELAY_MS); |
572 |
< |
assertTrue(one.isTerminated()); |
572 |
> |
assertTrue(p1.isTerminated()); |
573 |
|
} |
574 |
|
catch(Exception ex) { |
575 |
< |
fail("unexpected exception"); |
575 |
> |
unexpectedException(); |
576 |
|
} |
577 |
|
} |
578 |
|
|
579 |
< |
public void testShutDown4(){ |
580 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
579 |
> |
/** |
580 |
> |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
581 |
> |
* periodic tasks are cancelled at shutdown |
582 |
> |
*/ |
583 |
> |
public void testShutDown4() { |
584 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
585 |
|
try { |
586 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
587 |
< |
ScheduledCancellable task = |
588 |
< |
one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
586 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
587 |
> |
ScheduledFuture task = |
588 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
589 |
|
assertFalse(task.isCancelled()); |
590 |
< |
one.shutdown(); |
590 |
> |
p1.shutdown(); |
591 |
|
assertFalse(task.isCancelled()); |
592 |
< |
assertFalse(one.isTerminated()); |
593 |
< |
assertTrue(one.isShutdown()); |
592 |
> |
assertFalse(p1.isTerminated()); |
593 |
> |
assertTrue(p1.isShutdown()); |
594 |
|
Thread.sleep(SHORT_DELAY_MS); |
595 |
|
assertFalse(task.isCancelled()); |
596 |
|
task.cancel(true); |
597 |
|
assertTrue(task.isCancelled()); |
598 |
|
Thread.sleep(SHORT_DELAY_MS); |
599 |
< |
assertTrue(one.isTerminated()); |
599 |
> |
assertTrue(p1.isTerminated()); |
600 |
|
} |
601 |
|
catch(Exception ex) { |
602 |
< |
fail("unexpected exception"); |
602 |
> |
unexpectedException(); |
603 |
|
} |
604 |
|
finally { |
605 |
< |
one.shutdownNow(); |
605 |
> |
p1.shutdownNow(); |
606 |
> |
} |
607 |
> |
} |
608 |
> |
|
609 |
> |
/** |
610 |
> |
* completed submit of callable returns result |
611 |
> |
*/ |
612 |
> |
public void testSubmitCallable() { |
613 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
614 |
> |
try { |
615 |
> |
Future<String> future = e.submit(new StringTask()); |
616 |
> |
String result = future.get(); |
617 |
> |
assertSame(TEST_STRING, result); |
618 |
> |
} |
619 |
> |
catch (ExecutionException ex) { |
620 |
> |
unexpectedException(); |
621 |
> |
} |
622 |
> |
catch (InterruptedException ex) { |
623 |
> |
unexpectedException(); |
624 |
> |
} finally { |
625 |
> |
joinPool(e); |
626 |
> |
} |
627 |
> |
} |
628 |
> |
|
629 |
> |
/** |
630 |
> |
* completed submit of runnable returns successfully |
631 |
> |
*/ |
632 |
> |
public void testSubmitRunnable() { |
633 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
634 |
> |
try { |
635 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
636 |
> |
future.get(); |
637 |
> |
assertTrue(future.isDone()); |
638 |
> |
} |
639 |
> |
catch (ExecutionException ex) { |
640 |
> |
unexpectedException(); |
641 |
> |
} |
642 |
> |
catch (InterruptedException ex) { |
643 |
> |
unexpectedException(); |
644 |
> |
} finally { |
645 |
> |
joinPool(e); |
646 |
> |
} |
647 |
> |
} |
648 |
> |
|
649 |
> |
/** |
650 |
> |
* completed submit of (runnable, result) returns result |
651 |
> |
*/ |
652 |
> |
public void testSubmitRunnable2() { |
653 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
654 |
> |
try { |
655 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
656 |
> |
String result = future.get(); |
657 |
> |
assertSame(TEST_STRING, result); |
658 |
> |
} |
659 |
> |
catch (ExecutionException ex) { |
660 |
> |
unexpectedException(); |
661 |
> |
} |
662 |
> |
catch (InterruptedException ex) { |
663 |
> |
unexpectedException(); |
664 |
> |
} finally { |
665 |
> |
joinPool(e); |
666 |
> |
} |
667 |
> |
} |
668 |
> |
|
669 |
> |
|
670 |
> |
|
671 |
> |
|
672 |
> |
|
673 |
> |
/** |
674 |
> |
* invokeAny(null) throws NPE |
675 |
> |
*/ |
676 |
> |
public void testInvokeAny1() { |
677 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
678 |
> |
try { |
679 |
> |
e.invokeAny(null); |
680 |
> |
} catch (NullPointerException success) { |
681 |
> |
} catch(Exception ex) { |
682 |
> |
unexpectedException(); |
683 |
> |
} finally { |
684 |
> |
joinPool(e); |
685 |
|
} |
686 |
|
} |
687 |
|
|
688 |
+ |
/** |
689 |
+ |
* invokeAny(empty collection) throws IAE |
690 |
+ |
*/ |
691 |
+ |
public void testInvokeAny2() { |
692 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
693 |
+ |
try { |
694 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
695 |
+ |
} catch (IllegalArgumentException success) { |
696 |
+ |
} catch(Exception ex) { |
697 |
+ |
unexpectedException(); |
698 |
+ |
} finally { |
699 |
+ |
joinPool(e); |
700 |
+ |
} |
701 |
+ |
} |
702 |
+ |
|
703 |
+ |
/** |
704 |
+ |
* invokeAny(c) throws NPE if c has null elements |
705 |
+ |
*/ |
706 |
+ |
public void testInvokeAny3() { |
707 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
708 |
+ |
try { |
709 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
710 |
+ |
l.add(new StringTask()); |
711 |
+ |
l.add(null); |
712 |
+ |
e.invokeAny(l); |
713 |
+ |
} catch (NullPointerException success) { |
714 |
+ |
} catch(Exception ex) { |
715 |
+ |
unexpectedException(); |
716 |
+ |
} finally { |
717 |
+ |
joinPool(e); |
718 |
+ |
} |
719 |
+ |
} |
720 |
+ |
|
721 |
+ |
/** |
722 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
723 |
+ |
*/ |
724 |
+ |
public void testInvokeAny4() { |
725 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
726 |
+ |
try { |
727 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
728 |
+ |
l.add(new NPETask()); |
729 |
+ |
e.invokeAny(l); |
730 |
+ |
} catch (ExecutionException success) { |
731 |
+ |
} catch(Exception ex) { |
732 |
+ |
unexpectedException(); |
733 |
+ |
} finally { |
734 |
+ |
joinPool(e); |
735 |
+ |
} |
736 |
+ |
} |
737 |
+ |
|
738 |
+ |
/** |
739 |
+ |
* invokeAny(c) returns result of some task |
740 |
+ |
*/ |
741 |
+ |
public void testInvokeAny5() { |
742 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
743 |
+ |
try { |
744 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
745 |
+ |
l.add(new StringTask()); |
746 |
+ |
l.add(new StringTask()); |
747 |
+ |
String result = e.invokeAny(l); |
748 |
+ |
assertSame(TEST_STRING, result); |
749 |
+ |
} catch (ExecutionException success) { |
750 |
+ |
} catch(Exception ex) { |
751 |
+ |
unexpectedException(); |
752 |
+ |
} finally { |
753 |
+ |
joinPool(e); |
754 |
+ |
} |
755 |
+ |
} |
756 |
+ |
|
757 |
+ |
/** |
758 |
+ |
* invokeAll(null) throws NPE |
759 |
+ |
*/ |
760 |
+ |
public void testInvokeAll1() { |
761 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
762 |
+ |
try { |
763 |
+ |
e.invokeAll(null); |
764 |
+ |
} catch (NullPointerException success) { |
765 |
+ |
} catch(Exception ex) { |
766 |
+ |
unexpectedException(); |
767 |
+ |
} finally { |
768 |
+ |
joinPool(e); |
769 |
+ |
} |
770 |
+ |
} |
771 |
+ |
|
772 |
+ |
/** |
773 |
+ |
* invokeAll(empty collection) returns empty collection |
774 |
+ |
*/ |
775 |
+ |
public void testInvokeAll2() { |
776 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
777 |
+ |
try { |
778 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
779 |
+ |
assertTrue(r.isEmpty()); |
780 |
+ |
} catch(Exception ex) { |
781 |
+ |
unexpectedException(); |
782 |
+ |
} finally { |
783 |
+ |
joinPool(e); |
784 |
+ |
} |
785 |
+ |
} |
786 |
+ |
|
787 |
+ |
/** |
788 |
+ |
* invokeAll(c) throws NPE if c has null elements |
789 |
+ |
*/ |
790 |
+ |
public void testInvokeAll3() { |
791 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
792 |
+ |
try { |
793 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
794 |
+ |
l.add(new StringTask()); |
795 |
+ |
l.add(null); |
796 |
+ |
e.invokeAll(l); |
797 |
+ |
} catch (NullPointerException success) { |
798 |
+ |
} catch(Exception ex) { |
799 |
+ |
unexpectedException(); |
800 |
+ |
} finally { |
801 |
+ |
joinPool(e); |
802 |
+ |
} |
803 |
+ |
} |
804 |
+ |
|
805 |
+ |
/** |
806 |
+ |
* get of invokeAll(c) throws exception on failed task |
807 |
+ |
*/ |
808 |
+ |
public void testInvokeAll4() { |
809 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
810 |
+ |
try { |
811 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
812 |
+ |
l.add(new NPETask()); |
813 |
+ |
List<Future<String>> result = e.invokeAll(l); |
814 |
+ |
assertEquals(1, result.size()); |
815 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
816 |
+ |
it.next().get(); |
817 |
+ |
} catch(ExecutionException success) { |
818 |
+ |
} catch(Exception ex) { |
819 |
+ |
unexpectedException(); |
820 |
+ |
} finally { |
821 |
+ |
joinPool(e); |
822 |
+ |
} |
823 |
+ |
} |
824 |
+ |
|
825 |
+ |
/** |
826 |
+ |
* invokeAll(c) returns results of all completed tasks |
827 |
+ |
*/ |
828 |
+ |
public void testInvokeAll5() { |
829 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
830 |
+ |
try { |
831 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
832 |
+ |
l.add(new StringTask()); |
833 |
+ |
l.add(new StringTask()); |
834 |
+ |
List<Future<String>> result = e.invokeAll(l); |
835 |
+ |
assertEquals(2, result.size()); |
836 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
837 |
+ |
assertSame(TEST_STRING, it.next().get()); |
838 |
+ |
} catch (ExecutionException success) { |
839 |
+ |
} catch(Exception ex) { |
840 |
+ |
unexpectedException(); |
841 |
+ |
} finally { |
842 |
+ |
joinPool(e); |
843 |
+ |
} |
844 |
+ |
} |
845 |
+ |
|
846 |
+ |
/** |
847 |
+ |
* timed invokeAny(null) throws NPE |
848 |
+ |
*/ |
849 |
+ |
public void testTimedInvokeAny1() { |
850 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
851 |
+ |
try { |
852 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
853 |
+ |
} catch (NullPointerException success) { |
854 |
+ |
} catch(Exception ex) { |
855 |
+ |
unexpectedException(); |
856 |
+ |
} finally { |
857 |
+ |
joinPool(e); |
858 |
+ |
} |
859 |
+ |
} |
860 |
+ |
|
861 |
+ |
/** |
862 |
+ |
* timed invokeAny(,,null) throws NPE |
863 |
+ |
*/ |
864 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
865 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
866 |
+ |
try { |
867 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
+ |
l.add(new StringTask()); |
869 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
870 |
+ |
} catch (NullPointerException success) { |
871 |
+ |
} catch(Exception ex) { |
872 |
+ |
unexpectedException(); |
873 |
+ |
} finally { |
874 |
+ |
joinPool(e); |
875 |
+ |
} |
876 |
+ |
} |
877 |
+ |
|
878 |
+ |
/** |
879 |
+ |
* timed invokeAny(empty collection) throws IAE |
880 |
+ |
*/ |
881 |
+ |
public void testTimedInvokeAny2() { |
882 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
883 |
+ |
try { |
884 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
885 |
+ |
} catch (IllegalArgumentException success) { |
886 |
+ |
} catch(Exception ex) { |
887 |
+ |
unexpectedException(); |
888 |
+ |
} finally { |
889 |
+ |
joinPool(e); |
890 |
+ |
} |
891 |
+ |
} |
892 |
+ |
|
893 |
+ |
/** |
894 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
895 |
+ |
*/ |
896 |
+ |
public void testTimedInvokeAny3() { |
897 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
898 |
+ |
try { |
899 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
900 |
+ |
l.add(new StringTask()); |
901 |
+ |
l.add(null); |
902 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
903 |
+ |
} catch (NullPointerException success) { |
904 |
+ |
} catch(Exception ex) { |
905 |
+ |
ex.printStackTrace(); |
906 |
+ |
unexpectedException(); |
907 |
+ |
} finally { |
908 |
+ |
joinPool(e); |
909 |
+ |
} |
910 |
+ |
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
914 |
+ |
*/ |
915 |
+ |
public void testTimedInvokeAny4() { |
916 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
917 |
+ |
try { |
918 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
919 |
+ |
l.add(new NPETask()); |
920 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
921 |
+ |
} catch(ExecutionException success) { |
922 |
+ |
} catch(Exception ex) { |
923 |
+ |
unexpectedException(); |
924 |
+ |
} finally { |
925 |
+ |
joinPool(e); |
926 |
+ |
} |
927 |
+ |
} |
928 |
+ |
|
929 |
+ |
/** |
930 |
+ |
* timed invokeAny(c) returns result of some task |
931 |
+ |
*/ |
932 |
+ |
public void testTimedInvokeAny5() { |
933 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
934 |
+ |
try { |
935 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
936 |
+ |
l.add(new StringTask()); |
937 |
+ |
l.add(new StringTask()); |
938 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
939 |
+ |
assertSame(TEST_STRING, result); |
940 |
+ |
} catch (ExecutionException success) { |
941 |
+ |
} catch(Exception ex) { |
942 |
+ |
unexpectedException(); |
943 |
+ |
} finally { |
944 |
+ |
joinPool(e); |
945 |
+ |
} |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
/** |
949 |
+ |
* timed invokeAll(null) throws NPE |
950 |
+ |
*/ |
951 |
+ |
public void testTimedInvokeAll1() { |
952 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
953 |
+ |
try { |
954 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
955 |
+ |
} catch (NullPointerException success) { |
956 |
+ |
} catch(Exception ex) { |
957 |
+ |
unexpectedException(); |
958 |
+ |
} finally { |
959 |
+ |
joinPool(e); |
960 |
+ |
} |
961 |
+ |
} |
962 |
+ |
|
963 |
+ |
/** |
964 |
+ |
* timed invokeAll(,,null) throws NPE |
965 |
+ |
*/ |
966 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
967 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
968 |
+ |
try { |
969 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
970 |
+ |
l.add(new StringTask()); |
971 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
972 |
+ |
} catch (NullPointerException success) { |
973 |
+ |
} catch(Exception ex) { |
974 |
+ |
unexpectedException(); |
975 |
+ |
} finally { |
976 |
+ |
joinPool(e); |
977 |
+ |
} |
978 |
+ |
} |
979 |
+ |
|
980 |
+ |
/** |
981 |
+ |
* timed invokeAll(empty collection) returns empty collection |
982 |
+ |
*/ |
983 |
+ |
public void testTimedInvokeAll2() { |
984 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
985 |
+ |
try { |
986 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
987 |
+ |
assertTrue(r.isEmpty()); |
988 |
+ |
} catch(Exception ex) { |
989 |
+ |
unexpectedException(); |
990 |
+ |
} finally { |
991 |
+ |
joinPool(e); |
992 |
+ |
} |
993 |
+ |
} |
994 |
+ |
|
995 |
+ |
/** |
996 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
997 |
+ |
*/ |
998 |
+ |
public void testTimedInvokeAll3() { |
999 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1000 |
+ |
try { |
1001 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1002 |
+ |
l.add(new StringTask()); |
1003 |
+ |
l.add(null); |
1004 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1005 |
+ |
} catch (NullPointerException success) { |
1006 |
+ |
} catch(Exception ex) { |
1007 |
+ |
unexpectedException(); |
1008 |
+ |
} finally { |
1009 |
+ |
joinPool(e); |
1010 |
+ |
} |
1011 |
+ |
} |
1012 |
+ |
|
1013 |
+ |
/** |
1014 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1015 |
+ |
*/ |
1016 |
+ |
public void testTimedInvokeAll4() { |
1017 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1018 |
+ |
try { |
1019 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1020 |
+ |
l.add(new NPETask()); |
1021 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1022 |
+ |
assertEquals(1, result.size()); |
1023 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1024 |
+ |
it.next().get(); |
1025 |
+ |
} catch(ExecutionException success) { |
1026 |
+ |
} catch(Exception ex) { |
1027 |
+ |
unexpectedException(); |
1028 |
+ |
} finally { |
1029 |
+ |
joinPool(e); |
1030 |
+ |
} |
1031 |
+ |
} |
1032 |
+ |
|
1033 |
+ |
/** |
1034 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1035 |
+ |
*/ |
1036 |
+ |
public void testTimedInvokeAll5() { |
1037 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1038 |
+ |
try { |
1039 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1040 |
+ |
l.add(new StringTask()); |
1041 |
+ |
l.add(new StringTask()); |
1042 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1043 |
+ |
assertEquals(2, result.size()); |
1044 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1045 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1046 |
+ |
} catch (ExecutionException success) { |
1047 |
+ |
} catch(Exception ex) { |
1048 |
+ |
unexpectedException(); |
1049 |
+ |
} finally { |
1050 |
+ |
joinPool(e); |
1051 |
+ |
} |
1052 |
+ |
} |
1053 |
+ |
|
1054 |
+ |
/** |
1055 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1056 |
+ |
*/ |
1057 |
+ |
public void testTimedInvokeAll6() { |
1058 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1059 |
+ |
try { |
1060 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1061 |
+ |
l.add(new StringTask()); |
1062 |
+ |
l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING)); |
1063 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1064 |
+ |
assertEquals(2, result.size()); |
1065 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1066 |
+ |
Future<String> f1 = it.next(); |
1067 |
+ |
Future<String> f2 = it.next(); |
1068 |
+ |
assertTrue(f1.isDone()); |
1069 |
+ |
assertFalse(f1.isCancelled()); |
1070 |
+ |
assertTrue(f2.isDone()); |
1071 |
+ |
assertTrue(f2.isCancelled()); |
1072 |
+ |
} catch(Exception ex) { |
1073 |
+ |
unexpectedException(); |
1074 |
+ |
} finally { |
1075 |
+ |
joinPool(e); |
1076 |
+ |
} |
1077 |
+ |
} |
1078 |
+ |
|
1079 |
+ |
|
1080 |
|
} |