7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
import java.util.concurrent.atomic.*; |
12 |
|
|
13 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(ScheduledExecutorTest.class); |
18 |
> |
return new TestSuite(ScheduledExecutorSubclassTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
static class CustomTask<V> implements RunnableScheduledFuture<V> { |
74 |
|
} |
75 |
|
|
76 |
|
|
76 |
– |
|
77 |
|
/** |
78 |
|
* execute successfully executes a runnable |
79 |
|
*/ |
80 |
< |
public void testExecute() { |
81 |
< |
try { |
82 |
< |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
83 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
84 |
< |
p1.execute(runnable); |
85 |
< |
assertFalse(runnable.done); |
86 |
< |
Thread.sleep(SHORT_DELAY_MS); |
87 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
88 |
< |
try { |
89 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
90 |
< |
} catch(InterruptedException e){ |
91 |
< |
unexpectedException(); |
92 |
< |
} |
93 |
< |
assertTrue(runnable.done); |
94 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
95 |
< |
joinPool(p1); |
96 |
< |
} |
97 |
< |
catch(Exception e){ |
98 |
< |
unexpectedException(); |
99 |
< |
} |
100 |
< |
|
80 |
> |
public void testExecute() throws InterruptedException { |
81 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
82 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
83 |
> |
p1.execute(runnable); |
84 |
> |
assertFalse(runnable.done); |
85 |
> |
Thread.sleep(SHORT_DELAY_MS); |
86 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
87 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
88 |
> |
assertTrue(runnable.done); |
89 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
90 |
> |
joinPool(p1); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
/** |
95 |
|
* delayed schedule of callable successfully executes after delay |
96 |
|
*/ |
97 |
< |
public void testSchedule1() { |
98 |
< |
try { |
99 |
< |
TrackedCallable callable = new TrackedCallable(); |
100 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
101 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
102 |
< |
assertFalse(callable.done); |
103 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
104 |
< |
assertTrue(callable.done); |
105 |
< |
assertEquals(Boolean.TRUE, f.get()); |
106 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
117 |
< |
joinPool(p1); |
118 |
< |
} catch(RejectedExecutionException e){} |
119 |
< |
catch(Exception e){ |
120 |
< |
e.printStackTrace(); |
121 |
< |
unexpectedException(); |
122 |
< |
} |
97 |
> |
public void testSchedule1() throws Exception { |
98 |
> |
TrackedCallable callable = new TrackedCallable(); |
99 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
100 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
101 |
> |
assertFalse(callable.done); |
102 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
103 |
> |
assertTrue(callable.done); |
104 |
> |
assertEquals(Boolean.TRUE, f.get()); |
105 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
106 |
> |
joinPool(p1); |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* delayed schedule of runnable successfully executes after delay |
111 |
< |
*/ |
112 |
< |
public void testSchedule3() { |
113 |
< |
try { |
114 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
115 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
116 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
117 |
< |
Thread.sleep(SHORT_DELAY_MS); |
118 |
< |
assertFalse(runnable.done); |
119 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
120 |
< |
assertTrue(runnable.done); |
121 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
138 |
< |
joinPool(p1); |
139 |
< |
} catch(Exception e){ |
140 |
< |
unexpectedException(); |
141 |
< |
} |
110 |
> |
* delayed schedule of runnable successfully executes after delay |
111 |
> |
*/ |
112 |
> |
public void testSchedule3() throws InterruptedException { |
113 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
114 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
115 |
> |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
116 |
> |
Thread.sleep(SHORT_DELAY_MS); |
117 |
> |
assertFalse(runnable.done); |
118 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
119 |
> |
assertTrue(runnable.done); |
120 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
121 |
> |
joinPool(p1); |
122 |
|
} |
123 |
|
|
124 |
|
/** |
125 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
126 |
|
*/ |
127 |
< |
public void testSchedule4() { |
128 |
< |
try { |
129 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
130 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
131 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
132 |
< |
assertFalse(runnable.done); |
133 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
134 |
< |
assertTrue(runnable.done); |
135 |
< |
h.cancel(true); |
156 |
< |
joinPool(p1); |
157 |
< |
} catch(Exception e){ |
158 |
< |
unexpectedException(); |
159 |
< |
} |
127 |
> |
public void testSchedule4() throws InterruptedException { |
128 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
129 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
130 |
> |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
131 |
> |
assertFalse(runnable.done); |
132 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
133 |
> |
assertTrue(runnable.done); |
134 |
> |
h.cancel(true); |
135 |
> |
joinPool(p1); |
136 |
|
} |
137 |
|
|
138 |
|
static class RunnableCounter implements Runnable { |
143 |
|
/** |
144 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
145 |
|
*/ |
146 |
< |
public void testSchedule5() { |
147 |
< |
try { |
148 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
149 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
150 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
151 |
< |
assertFalse(runnable.done); |
152 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
153 |
< |
assertTrue(runnable.done); |
154 |
< |
h.cancel(true); |
179 |
< |
joinPool(p1); |
180 |
< |
} catch(Exception e){ |
181 |
< |
unexpectedException(); |
182 |
< |
} |
146 |
> |
public void testSchedule5() throws InterruptedException { |
147 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
148 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
149 |
> |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
150 |
> |
assertFalse(runnable.done); |
151 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
152 |
> |
assertTrue(runnable.done); |
153 |
> |
h.cancel(true); |
154 |
> |
joinPool(p1); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
159 |
|
*/ |
160 |
< |
public void testFixedRateSequence() { |
161 |
< |
try { |
162 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
163 |
< |
RunnableCounter counter = new RunnableCounter(); |
164 |
< |
ScheduledFuture h = |
165 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
166 |
< |
Thread.sleep(SMALL_DELAY_MS); |
167 |
< |
h.cancel(true); |
168 |
< |
int c = counter.count.get(); |
169 |
< |
// By time scaling conventions, we must have at least |
170 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
171 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
172 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
201 |
< |
joinPool(p1); |
202 |
< |
} catch(Exception e){ |
203 |
< |
unexpectedException(); |
204 |
< |
} |
160 |
> |
public void testFixedRateSequence() throws InterruptedException { |
161 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
162 |
> |
RunnableCounter counter = new RunnableCounter(); |
163 |
> |
ScheduledFuture h = |
164 |
> |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
165 |
> |
Thread.sleep(SMALL_DELAY_MS); |
166 |
> |
h.cancel(true); |
167 |
> |
int c = counter.count.get(); |
168 |
> |
// By time scaling conventions, we must have at least |
169 |
> |
// an execution per SHORT delay, but no more than one SHORT more |
170 |
> |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
171 |
> |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
172 |
> |
joinPool(p1); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
177 |
|
*/ |
178 |
< |
public void testFixedDelaySequence() { |
179 |
< |
try { |
180 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
181 |
< |
RunnableCounter counter = new RunnableCounter(); |
182 |
< |
ScheduledFuture h = |
183 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
184 |
< |
Thread.sleep(SMALL_DELAY_MS); |
185 |
< |
h.cancel(true); |
186 |
< |
int c = counter.count.get(); |
187 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
188 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
221 |
< |
joinPool(p1); |
222 |
< |
} catch(Exception e){ |
223 |
< |
unexpectedException(); |
224 |
< |
} |
178 |
> |
public void testFixedDelaySequence() throws InterruptedException { |
179 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
180 |
> |
RunnableCounter counter = new RunnableCounter(); |
181 |
> |
ScheduledFuture h = |
182 |
> |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
183 |
> |
Thread.sleep(SMALL_DELAY_MS); |
184 |
> |
h.cancel(true); |
185 |
> |
int c = counter.count.get(); |
186 |
> |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
187 |
> |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
188 |
> |
joinPool(p1); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
/** |
193 |
< |
* execute (null) throws NPE |
193 |
> |
* execute(null) throws NPE |
194 |
|
*/ |
195 |
< |
public void testExecuteNull() { |
196 |
< |
CustomExecutor se = null; |
195 |
> |
public void testExecuteNull() throws InterruptedException { |
196 |
> |
CustomExecutor se = new CustomExecutor(1); |
197 |
|
try { |
198 |
< |
se = new CustomExecutor(1); |
235 |
< |
se.execute(null); |
198 |
> |
se.execute(null); |
199 |
|
shouldThrow(); |
200 |
< |
} catch(NullPointerException success){} |
201 |
< |
catch(Exception e){ |
239 |
< |
unexpectedException(); |
240 |
< |
} |
241 |
< |
|
242 |
< |
joinPool(se); |
200 |
> |
} catch (NullPointerException success) {} |
201 |
> |
joinPool(se); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* schedule (null) throws NPE |
205 |
> |
* schedule(null) throws NPE |
206 |
|
*/ |
207 |
< |
public void testScheduleNull() { |
207 |
> |
public void testScheduleNull() throws InterruptedException { |
208 |
|
CustomExecutor se = new CustomExecutor(1); |
209 |
< |
try { |
209 |
> |
try { |
210 |
|
TrackedCallable callable = null; |
211 |
< |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
211 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
212 |
|
shouldThrow(); |
213 |
< |
} catch(NullPointerException success){} |
214 |
< |
catch(Exception e){ |
256 |
< |
unexpectedException(); |
257 |
< |
} |
258 |
< |
joinPool(se); |
213 |
> |
} catch (NullPointerException success) {} |
214 |
> |
joinPool(se); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
222 |
|
try { |
223 |
|
se.shutdown(); |
224 |
|
se.schedule(new NoOpRunnable(), |
225 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
225 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
226 |
|
shouldThrow(); |
227 |
< |
} catch(RejectedExecutionException success){ |
227 |
> |
} catch (RejectedExecutionException success) { |
228 |
|
} catch (SecurityException ok) { |
229 |
|
} |
230 |
|
|
231 |
|
joinPool(se); |
276 |
– |
|
232 |
|
} |
233 |
|
|
234 |
|
/** |
239 |
|
try { |
240 |
|
se.shutdown(); |
241 |
|
se.schedule(new NoOpCallable(), |
242 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
242 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
243 |
|
shouldThrow(); |
244 |
< |
} catch(RejectedExecutionException success){ |
244 |
> |
} catch (RejectedExecutionException success) { |
245 |
|
} catch (SecurityException ok) { |
246 |
|
} |
247 |
|
joinPool(se); |
253 |
|
public void testSchedule3_RejectedExecutionException() { |
254 |
|
CustomExecutor se = new CustomExecutor(1); |
255 |
|
try { |
256 |
< |
se.shutdown(); |
257 |
< |
se.schedule(new NoOpCallable(), |
258 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
259 |
< |
shouldThrow(); |
260 |
< |
} catch(RejectedExecutionException success){ |
261 |
< |
} catch (SecurityException ok) { |
262 |
< |
} |
256 |
> |
se.shutdown(); |
257 |
> |
se.schedule(new NoOpCallable(), |
258 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
259 |
> |
shouldThrow(); |
260 |
> |
} catch (RejectedExecutionException success) { |
261 |
> |
} catch (SecurityException ok) { |
262 |
> |
} |
263 |
|
joinPool(se); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
267 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
268 |
|
*/ |
269 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
270 |
|
CustomExecutor se = new CustomExecutor(1); |
271 |
|
try { |
272 |
|
se.shutdown(); |
273 |
|
se.scheduleAtFixedRate(new NoOpRunnable(), |
274 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
274 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
275 |
|
shouldThrow(); |
276 |
< |
} catch(RejectedExecutionException success){ |
276 |
> |
} catch (RejectedExecutionException success) { |
277 |
|
} catch (SecurityException ok) { |
278 |
|
} |
279 |
|
joinPool(se); |
287 |
|
try { |
288 |
|
se.shutdown(); |
289 |
|
se.scheduleWithFixedDelay(new NoOpRunnable(), |
290 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
290 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
291 |
|
shouldThrow(); |
292 |
< |
} catch(RejectedExecutionException success){ |
292 |
> |
} catch (RejectedExecutionException success) { |
293 |
|
} catch (SecurityException ok) { |
294 |
|
} |
295 |
|
joinPool(se); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* getActiveCount increases but doesn't overestimate, when a |
300 |
< |
* thread becomes active |
299 |
> |
* getActiveCount increases but doesn't overestimate, when a |
300 |
> |
* thread becomes active |
301 |
|
*/ |
302 |
< |
public void testGetActiveCount() { |
302 |
> |
public void testGetActiveCount() throws InterruptedException { |
303 |
|
CustomExecutor p2 = new CustomExecutor(2); |
304 |
|
assertEquals(0, p2.getActiveCount()); |
305 |
|
p2.execute(new SmallRunnable()); |
306 |
< |
try { |
352 |
< |
Thread.sleep(SHORT_DELAY_MS); |
353 |
< |
} catch(Exception e){ |
354 |
< |
unexpectedException(); |
355 |
< |
} |
306 |
> |
Thread.sleep(SHORT_DELAY_MS); |
307 |
|
assertEquals(1, p2.getActiveCount()); |
308 |
|
joinPool(p2); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
313 |
< |
* when tasks complete |
312 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
313 |
> |
* when tasks complete |
314 |
|
*/ |
315 |
< |
public void testGetCompletedTaskCount() { |
315 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
316 |
|
CustomExecutor p2 = new CustomExecutor(2); |
317 |
|
assertEquals(0, p2.getCompletedTaskCount()); |
318 |
|
p2.execute(new SmallRunnable()); |
319 |
< |
try { |
369 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
370 |
< |
} catch(Exception e){ |
371 |
< |
unexpectedException(); |
372 |
< |
} |
319 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
320 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
321 |
|
joinPool(p2); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
325 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
326 |
|
*/ |
327 |
|
public void testGetCorePoolSize() { |
328 |
|
CustomExecutor p1 = new CustomExecutor(1); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
335 |
< |
* multiple threads active |
334 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
335 |
> |
* multiple threads active |
336 |
|
*/ |
337 |
< |
public void testGetLargestPoolSize() { |
337 |
> |
public void testGetLargestPoolSize() throws InterruptedException { |
338 |
|
CustomExecutor p2 = new CustomExecutor(2); |
339 |
|
assertEquals(0, p2.getLargestPoolSize()); |
340 |
|
p2.execute(new SmallRunnable()); |
341 |
|
p2.execute(new SmallRunnable()); |
342 |
< |
try { |
396 |
< |
Thread.sleep(SHORT_DELAY_MS); |
397 |
< |
} catch(Exception e){ |
398 |
< |
unexpectedException(); |
399 |
< |
} |
342 |
> |
Thread.sleep(SHORT_DELAY_MS); |
343 |
|
assertEquals(2, p2.getLargestPoolSize()); |
344 |
|
joinPool(p2); |
345 |
|
} |
346 |
|
|
347 |
|
/** |
348 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
349 |
< |
* become active |
348 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
349 |
> |
* become active |
350 |
|
*/ |
351 |
|
public void testGetPoolSize() { |
352 |
|
CustomExecutor p1 = new CustomExecutor(1); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
361 |
< |
* submitted |
360 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
361 |
> |
* submitted |
362 |
|
*/ |
363 |
< |
public void testGetTaskCount() { |
363 |
> |
public void testGetTaskCount() throws InterruptedException { |
364 |
|
CustomExecutor p1 = new CustomExecutor(1); |
365 |
|
assertEquals(0, p1.getTaskCount()); |
366 |
< |
for(int i = 0; i < 5; i++) |
366 |
> |
for (int i = 0; i < 5; i++) |
367 |
|
p1.execute(new SmallRunnable()); |
368 |
< |
try { |
426 |
< |
Thread.sleep(SHORT_DELAY_MS); |
427 |
< |
} catch(Exception e){ |
428 |
< |
unexpectedException(); |
429 |
< |
} |
368 |
> |
Thread.sleep(SHORT_DELAY_MS); |
369 |
|
assertEquals(5, p1.getTaskCount()); |
370 |
|
joinPool(p1); |
371 |
|
} |
375 |
|
*/ |
376 |
|
public void testGetThreadFactory() { |
377 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
378 |
< |
CustomExecutor p = new CustomExecutor(1, tf); |
378 |
> |
CustomExecutor p = new CustomExecutor(1, tf); |
379 |
|
assertSame(tf, p.getThreadFactory()); |
380 |
|
joinPool(p); |
381 |
|
} |
385 |
|
*/ |
386 |
|
public void testSetThreadFactory() { |
387 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
388 |
< |
CustomExecutor p = new CustomExecutor(1); |
388 |
> |
CustomExecutor p = new CustomExecutor(1); |
389 |
|
p.setThreadFactory(tf); |
390 |
|
assertSame(tf, p.getThreadFactory()); |
391 |
|
joinPool(p); |
395 |
|
* setThreadFactory(null) throws NPE |
396 |
|
*/ |
397 |
|
public void testSetThreadFactoryNull() { |
398 |
< |
CustomExecutor p = new CustomExecutor(1); |
398 |
> |
CustomExecutor p = new CustomExecutor(1); |
399 |
|
try { |
400 |
|
p.setThreadFactory(null); |
401 |
|
shouldThrow(); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
< |
* is isShutDown is false before shutdown, true after |
409 |
> |
* isShutDown is false before shutdown, true after |
410 |
|
*/ |
411 |
|
public void testIsShutdown() { |
412 |
< |
|
474 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
412 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
413 |
|
try { |
414 |
|
assertFalse(p1.isShutdown()); |
415 |
|
} |
416 |
|
finally { |
417 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
417 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
418 |
|
} |
419 |
< |
assertTrue(p1.isShutdown()); |
419 |
> |
assertTrue(p1.isShutdown()); |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
|
/** |
424 |
< |
* isTerminated is false before termination, true after |
424 |
> |
* isTerminated is false before termination, true after |
425 |
|
*/ |
426 |
< |
public void testIsTerminated() { |
427 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
426 |
> |
public void testIsTerminated() throws InterruptedException { |
427 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
428 |
|
try { |
429 |
|
p1.execute(new SmallRunnable()); |
430 |
|
} finally { |
431 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
494 |
< |
} |
495 |
< |
try { |
496 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
497 |
< |
assertTrue(p1.isTerminated()); |
498 |
< |
} catch(Exception e){ |
499 |
< |
unexpectedException(); |
431 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
432 |
|
} |
433 |
+ |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
434 |
+ |
assertTrue(p1.isTerminated()); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
< |
* isTerminating is not true when running or when terminated |
438 |
> |
* isTerminating is not true when running or when terminated |
439 |
|
*/ |
440 |
< |
public void testIsTerminating() { |
441 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
440 |
> |
public void testIsTerminating() throws InterruptedException { |
441 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
442 |
|
assertFalse(p1.isTerminating()); |
443 |
|
try { |
444 |
|
p1.execute(new SmallRunnable()); |
445 |
|
assertFalse(p1.isTerminating()); |
446 |
|
} finally { |
447 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
514 |
< |
} |
515 |
< |
try { |
516 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
< |
assertTrue(p1.isTerminated()); |
518 |
< |
assertFalse(p1.isTerminating()); |
519 |
< |
} catch(Exception e){ |
520 |
< |
unexpectedException(); |
447 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
448 |
|
} |
449 |
+ |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
450 |
+ |
assertTrue(p1.isTerminated()); |
451 |
+ |
assertFalse(p1.isTerminating()); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
|
* getQueue returns the work queue, which contains queued tasks |
456 |
|
*/ |
457 |
< |
public void testGetQueue() { |
457 |
> |
public void testGetQueue() throws InterruptedException { |
458 |
|
CustomExecutor p1 = new CustomExecutor(1); |
459 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
460 |
< |
for(int i = 0; i < 5; i++){ |
461 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
460 |
> |
for (int i = 0; i < 5; i++) { |
461 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
462 |
|
} |
463 |
|
try { |
464 |
|
Thread.sleep(SHORT_DELAY_MS); |
465 |
|
BlockingQueue<Runnable> q = p1.getQueue(); |
466 |
|
assertTrue(q.contains(tasks[4])); |
467 |
|
assertFalse(q.contains(tasks[0])); |
538 |
– |
} catch(Exception e) { |
539 |
– |
unexpectedException(); |
468 |
|
} finally { |
469 |
|
joinPool(p1); |
470 |
|
} |
473 |
|
/** |
474 |
|
* remove(task) removes queued task, and fails to remove active task |
475 |
|
*/ |
476 |
< |
public void testRemove() { |
476 |
> |
public void testRemove() throws InterruptedException { |
477 |
|
CustomExecutor p1 = new CustomExecutor(1); |
478 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
479 |
< |
for(int i = 0; i < 5; i++){ |
480 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
479 |
> |
for (int i = 0; i < 5; i++) { |
480 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
481 |
|
} |
482 |
|
try { |
483 |
|
Thread.sleep(SHORT_DELAY_MS); |
491 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
492 |
|
assertTrue(p1.remove((Runnable)tasks[3])); |
493 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
566 |
– |
} catch(Exception e) { |
567 |
– |
unexpectedException(); |
494 |
|
} finally { |
495 |
|
joinPool(p1); |
496 |
|
} |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
< |
* purge removes cancelled tasks from the queue |
500 |
> |
* purge removes cancelled tasks from the queue |
501 |
|
*/ |
502 |
< |
public void testPurge() { |
502 |
> |
public void testPurge() throws InterruptedException { |
503 |
|
CustomExecutor p1 = new CustomExecutor(1); |
504 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
505 |
< |
for(int i = 0; i < 5; i++){ |
506 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
505 |
> |
for (int i = 0; i < 5; i++) { |
506 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
507 |
|
} |
508 |
|
try { |
509 |
|
int max = 5; |
520 |
|
Thread.sleep(1); |
521 |
|
} |
522 |
|
assertTrue(k < SMALL_DELAY_MS); |
597 |
– |
} catch(Exception e) { |
598 |
– |
unexpectedException(); |
523 |
|
} finally { |
524 |
|
joinPool(p1); |
525 |
|
} |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
< |
* shutDownNow returns a list containing tasks that were not run |
529 |
> |
* shutDownNow returns a list containing tasks that were not run |
530 |
|
*/ |
531 |
|
public void testShutDownNow() { |
532 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
533 |
< |
for(int i = 0; i < 5; i++) |
534 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
532 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
533 |
> |
for (int i = 0; i < 5; i++) |
534 |
> |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
535 |
|
List l; |
536 |
|
try { |
537 |
|
l = p1.shutdownNow(); |
538 |
|
} catch (SecurityException ok) { |
539 |
|
return; |
540 |
|
} |
541 |
< |
assertTrue(p1.isShutdown()); |
542 |
< |
assertTrue(l.size() > 0 && l.size() <= 5); |
541 |
> |
assertTrue(p1.isShutdown()); |
542 |
> |
assertTrue(l.size() > 0 && l.size() <= 5); |
543 |
|
joinPool(p1); |
544 |
|
} |
545 |
|
|
547 |
|
* In default setting, shutdown cancels periodic but not delayed |
548 |
|
* tasks at shutdown |
549 |
|
*/ |
550 |
< |
public void testShutDown1() { |
551 |
< |
try { |
552 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
553 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
630 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
631 |
< |
|
632 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
633 |
< |
for(int i = 0; i < 5; i++) |
634 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
635 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
636 |
< |
BlockingQueue q = p1.getQueue(); |
637 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
638 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
639 |
< |
assertFalse(t.isCancelled()); |
640 |
< |
} |
641 |
< |
assertTrue(p1.isShutdown()); |
642 |
< |
Thread.sleep(SMALL_DELAY_MS); |
643 |
< |
for (int i = 0; i < 5; ++i) { |
644 |
< |
assertTrue(tasks[i].isDone()); |
645 |
< |
assertFalse(tasks[i].isCancelled()); |
646 |
< |
} |
550 |
> |
public void testShutDown1() throws InterruptedException { |
551 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
552 |
> |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
553 |
> |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
554 |
|
|
555 |
< |
} |
556 |
< |
catch(Exception ex) { |
557 |
< |
unexpectedException(); |
555 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
556 |
> |
for (int i = 0; i < 5; i++) |
557 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
558 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
559 |
> |
BlockingQueue q = p1.getQueue(); |
560 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
561 |
> |
ScheduledFuture t = (ScheduledFuture)it.next(); |
562 |
> |
assertFalse(t.isCancelled()); |
563 |
> |
} |
564 |
> |
assertTrue(p1.isShutdown()); |
565 |
> |
Thread.sleep(SMALL_DELAY_MS); |
566 |
> |
for (int i = 0; i < 5; ++i) { |
567 |
> |
assertTrue(tasks[i].isDone()); |
568 |
> |
assertFalse(tasks[i].isCancelled()); |
569 |
|
} |
570 |
|
} |
571 |
|
|
574 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
575 |
|
* delayed tasks are cancelled at shutdown |
576 |
|
*/ |
577 |
< |
public void testShutDown2() { |
578 |
< |
try { |
579 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
580 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
581 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
582 |
< |
for(int i = 0; i < 5; i++) |
583 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
584 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
585 |
< |
assertTrue(p1.isShutdown()); |
586 |
< |
BlockingQueue q = p1.getQueue(); |
587 |
< |
assertTrue(q.isEmpty()); |
588 |
< |
Thread.sleep(SMALL_DELAY_MS); |
671 |
< |
assertTrue(p1.isTerminated()); |
672 |
< |
} |
673 |
< |
catch(Exception ex) { |
674 |
< |
unexpectedException(); |
675 |
< |
} |
577 |
> |
public void testShutDown2() throws InterruptedException { |
578 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
579 |
> |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
580 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
581 |
> |
for (int i = 0; i < 5; i++) |
582 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
583 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
584 |
> |
assertTrue(p1.isShutdown()); |
585 |
> |
BlockingQueue q = p1.getQueue(); |
586 |
> |
assertTrue(q.isEmpty()); |
587 |
> |
Thread.sleep(SMALL_DELAY_MS); |
588 |
> |
assertTrue(p1.isTerminated()); |
589 |
|
} |
590 |
|
|
591 |
|
|
593 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
594 |
|
* periodic tasks are not cancelled at shutdown |
595 |
|
*/ |
596 |
< |
public void testShutDown3() { |
597 |
< |
try { |
598 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
599 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
600 |
< |
ScheduledFuture task = |
601 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
602 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
603 |
< |
assertTrue(p1.isShutdown()); |
604 |
< |
BlockingQueue q = p1.getQueue(); |
605 |
< |
assertTrue(q.isEmpty()); |
606 |
< |
Thread.sleep(SHORT_DELAY_MS); |
694 |
< |
assertTrue(p1.isTerminated()); |
695 |
< |
} |
696 |
< |
catch(Exception ex) { |
697 |
< |
unexpectedException(); |
698 |
< |
} |
596 |
> |
public void testShutDown3() throws InterruptedException { |
597 |
> |
CustomExecutor p1 = new CustomExecutor(1); |
598 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
599 |
> |
ScheduledFuture task = |
600 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
601 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
602 |
> |
assertTrue(p1.isShutdown()); |
603 |
> |
BlockingQueue q = p1.getQueue(); |
604 |
> |
assertTrue(q.isEmpty()); |
605 |
> |
Thread.sleep(SHORT_DELAY_MS); |
606 |
> |
assertTrue(p1.isTerminated()); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
611 |
|
* periodic tasks are cancelled at shutdown |
612 |
|
*/ |
613 |
< |
public void testShutDown4() { |
613 |
> |
public void testShutDown4() throws InterruptedException { |
614 |
|
CustomExecutor p1 = new CustomExecutor(1); |
615 |
|
try { |
616 |
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
617 |
|
ScheduledFuture task = |
618 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); |
618 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
619 |
|
assertFalse(task.isCancelled()); |
620 |
< |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
620 |
> |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
621 |
|
assertFalse(task.isCancelled()); |
622 |
|
assertFalse(p1.isTerminated()); |
623 |
|
assertTrue(p1.isShutdown()); |
628 |
|
Thread.sleep(SHORT_DELAY_MS); |
629 |
|
assertTrue(p1.isTerminated()); |
630 |
|
} |
723 |
– |
catch(Exception ex) { |
724 |
– |
unexpectedException(); |
725 |
– |
} |
631 |
|
finally { |
632 |
|
joinPool(p1); |
633 |
|
} |
636 |
|
/** |
637 |
|
* completed submit of callable returns result |
638 |
|
*/ |
639 |
< |
public void testSubmitCallable() { |
639 |
> |
public void testSubmitCallable() throws Exception { |
640 |
|
ExecutorService e = new CustomExecutor(2); |
641 |
|
try { |
642 |
|
Future<String> future = e.submit(new StringTask()); |
643 |
|
String result = future.get(); |
644 |
|
assertSame(TEST_STRING, result); |
740 |
– |
} |
741 |
– |
catch (ExecutionException ex) { |
742 |
– |
unexpectedException(); |
743 |
– |
} |
744 |
– |
catch (InterruptedException ex) { |
745 |
– |
unexpectedException(); |
645 |
|
} finally { |
646 |
|
joinPool(e); |
647 |
|
} |
650 |
|
/** |
651 |
|
* completed submit of runnable returns successfully |
652 |
|
*/ |
653 |
< |
public void testSubmitRunnable() { |
653 |
> |
public void testSubmitRunnable() throws Exception { |
654 |
|
ExecutorService e = new CustomExecutor(2); |
655 |
|
try { |
656 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
657 |
|
future.get(); |
658 |
|
assertTrue(future.isDone()); |
760 |
– |
} |
761 |
– |
catch (ExecutionException ex) { |
762 |
– |
unexpectedException(); |
763 |
– |
} |
764 |
– |
catch (InterruptedException ex) { |
765 |
– |
unexpectedException(); |
659 |
|
} finally { |
660 |
|
joinPool(e); |
661 |
|
} |
664 |
|
/** |
665 |
|
* completed submit of (runnable, result) returns result |
666 |
|
*/ |
667 |
< |
public void testSubmitRunnable2() { |
667 |
> |
public void testSubmitRunnable2() throws Exception { |
668 |
|
ExecutorService e = new CustomExecutor(2); |
669 |
|
try { |
670 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
671 |
|
String result = future.get(); |
672 |
|
assertSame(TEST_STRING, result); |
780 |
– |
} |
781 |
– |
catch (ExecutionException ex) { |
782 |
– |
unexpectedException(); |
783 |
– |
} |
784 |
– |
catch (InterruptedException ex) { |
785 |
– |
unexpectedException(); |
673 |
|
} finally { |
674 |
|
joinPool(e); |
675 |
|
} |
678 |
|
/** |
679 |
|
* invokeAny(null) throws NPE |
680 |
|
*/ |
681 |
< |
public void testInvokeAny1() { |
681 |
> |
public void testInvokeAny1() throws Exception { |
682 |
|
ExecutorService e = new CustomExecutor(2); |
683 |
|
try { |
684 |
|
e.invokeAny(null); |
685 |
+ |
shouldThrow(); |
686 |
|
} catch (NullPointerException success) { |
799 |
– |
} catch(Exception ex) { |
800 |
– |
unexpectedException(); |
687 |
|
} finally { |
688 |
|
joinPool(e); |
689 |
|
} |
692 |
|
/** |
693 |
|
* invokeAny(empty collection) throws IAE |
694 |
|
*/ |
695 |
< |
public void testInvokeAny2() { |
695 |
> |
public void testInvokeAny2() throws Exception { |
696 |
|
ExecutorService e = new CustomExecutor(2); |
697 |
|
try { |
698 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
699 |
+ |
shouldThrow(); |
700 |
|
} catch (IllegalArgumentException success) { |
814 |
– |
} catch(Exception ex) { |
815 |
– |
unexpectedException(); |
701 |
|
} finally { |
702 |
|
joinPool(e); |
703 |
|
} |
706 |
|
/** |
707 |
|
* invokeAny(c) throws NPE if c has null elements |
708 |
|
*/ |
709 |
< |
public void testInvokeAny3() { |
709 |
> |
public void testInvokeAny3() throws Exception { |
710 |
> |
CountDownLatch latch = new CountDownLatch(1); |
711 |
|
ExecutorService e = new CustomExecutor(2); |
712 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
713 |
+ |
l.add(latchAwaitingStringTask(latch)); |
714 |
+ |
l.add(null); |
715 |
|
try { |
827 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
828 |
– |
l.add(new StringTask()); |
829 |
– |
l.add(null); |
716 |
|
e.invokeAny(l); |
717 |
+ |
shouldThrow(); |
718 |
|
} catch (NullPointerException success) { |
832 |
– |
} catch(Exception ex) { |
833 |
– |
unexpectedException(); |
719 |
|
} finally { |
720 |
+ |
latch.countDown(); |
721 |
|
joinPool(e); |
722 |
|
} |
723 |
|
} |
725 |
|
/** |
726 |
|
* invokeAny(c) throws ExecutionException if no task completes |
727 |
|
*/ |
728 |
< |
public void testInvokeAny4() { |
728 |
> |
public void testInvokeAny4() throws Exception { |
729 |
|
ExecutorService e = new CustomExecutor(2); |
730 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
731 |
+ |
l.add(new NPETask()); |
732 |
|
try { |
845 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
846 |
– |
l.add(new NPETask()); |
733 |
|
e.invokeAny(l); |
734 |
+ |
shouldThrow(); |
735 |
|
} catch (ExecutionException success) { |
736 |
< |
} catch(Exception ex) { |
850 |
< |
unexpectedException(); |
736 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
737 |
|
} finally { |
738 |
|
joinPool(e); |
739 |
|
} |
742 |
|
/** |
743 |
|
* invokeAny(c) returns result of some task |
744 |
|
*/ |
745 |
< |
public void testInvokeAny5() { |
745 |
> |
public void testInvokeAny5() throws Exception { |
746 |
|
ExecutorService e = new CustomExecutor(2); |
747 |
|
try { |
748 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
748 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
749 |
|
l.add(new StringTask()); |
750 |
|
l.add(new StringTask()); |
751 |
|
String result = e.invokeAny(l); |
752 |
|
assertSame(TEST_STRING, result); |
867 |
– |
} catch (ExecutionException success) { |
868 |
– |
} catch(Exception ex) { |
869 |
– |
unexpectedException(); |
753 |
|
} finally { |
754 |
|
joinPool(e); |
755 |
|
} |
758 |
|
/** |
759 |
|
* invokeAll(null) throws NPE |
760 |
|
*/ |
761 |
< |
public void testInvokeAll1() { |
761 |
> |
public void testInvokeAll1() throws Exception { |
762 |
|
ExecutorService e = new CustomExecutor(2); |
763 |
|
try { |
764 |
|
e.invokeAll(null); |
765 |
+ |
shouldThrow(); |
766 |
|
} catch (NullPointerException success) { |
883 |
– |
} catch(Exception ex) { |
884 |
– |
unexpectedException(); |
767 |
|
} finally { |
768 |
|
joinPool(e); |
769 |
|
} |
772 |
|
/** |
773 |
|
* invokeAll(empty collection) returns empty collection |
774 |
|
*/ |
775 |
< |
public void testInvokeAll2() { |
775 |
> |
public void testInvokeAll2() throws Exception { |
776 |
|
ExecutorService e = new CustomExecutor(2); |
777 |
|
try { |
778 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
779 |
|
assertTrue(r.isEmpty()); |
898 |
– |
} catch(Exception ex) { |
899 |
– |
unexpectedException(); |
780 |
|
} finally { |
781 |
|
joinPool(e); |
782 |
|
} |
785 |
|
/** |
786 |
|
* invokeAll(c) throws NPE if c has null elements |
787 |
|
*/ |
788 |
< |
public void testInvokeAll3() { |
788 |
> |
public void testInvokeAll3() throws Exception { |
789 |
|
ExecutorService e = new CustomExecutor(2); |
790 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
791 |
+ |
l.add(new StringTask()); |
792 |
+ |
l.add(null); |
793 |
|
try { |
911 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
912 |
– |
l.add(new StringTask()); |
913 |
– |
l.add(null); |
794 |
|
e.invokeAll(l); |
795 |
+ |
shouldThrow(); |
796 |
|
} catch (NullPointerException success) { |
916 |
– |
} catch(Exception ex) { |
917 |
– |
unexpectedException(); |
797 |
|
} finally { |
798 |
|
joinPool(e); |
799 |
|
} |
802 |
|
/** |
803 |
|
* get of invokeAll(c) throws exception on failed task |
804 |
|
*/ |
805 |
< |
public void testInvokeAll4() { |
805 |
> |
public void testInvokeAll4() throws Exception { |
806 |
|
ExecutorService e = new CustomExecutor(2); |
807 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
808 |
+ |
l.add(new NPETask()); |
809 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
810 |
+ |
assertEquals(1, futures.size()); |
811 |
|
try { |
812 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
813 |
< |
l.add(new NPETask()); |
814 |
< |
List<Future<String>> result = e.invokeAll(l); |
815 |
< |
assertEquals(1, result.size()); |
933 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
934 |
< |
it.next().get(); |
935 |
< |
} catch(ExecutionException success) { |
936 |
< |
} catch(Exception ex) { |
937 |
< |
unexpectedException(); |
812 |
> |
futures.get(0).get(); |
813 |
> |
shouldThrow(); |
814 |
> |
} catch (ExecutionException success) { |
815 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
816 |
|
} finally { |
817 |
|
joinPool(e); |
818 |
|
} |
821 |
|
/** |
822 |
|
* invokeAll(c) returns results of all completed tasks |
823 |
|
*/ |
824 |
< |
public void testInvokeAll5() { |
824 |
> |
public void testInvokeAll5() throws Exception { |
825 |
|
ExecutorService e = new CustomExecutor(2); |
826 |
|
try { |
827 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
827 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
828 |
|
l.add(new StringTask()); |
829 |
|
l.add(new StringTask()); |
830 |
< |
List<Future<String>> result = e.invokeAll(l); |
831 |
< |
assertEquals(2, result.size()); |
832 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
833 |
< |
assertSame(TEST_STRING, it.next().get()); |
956 |
< |
} catch (ExecutionException success) { |
957 |
< |
} catch(Exception ex) { |
958 |
< |
unexpectedException(); |
830 |
> |
List<Future<String>> futures = e.invokeAll(l); |
831 |
> |
assertEquals(2, futures.size()); |
832 |
> |
for (Future<String> future : futures) |
833 |
> |
assertSame(TEST_STRING, future.get()); |
834 |
|
} finally { |
835 |
|
joinPool(e); |
836 |
|
} |
839 |
|
/** |
840 |
|
* timed invokeAny(null) throws NPE |
841 |
|
*/ |
842 |
< |
public void testTimedInvokeAny1() { |
842 |
> |
public void testTimedInvokeAny1() throws Exception { |
843 |
|
ExecutorService e = new CustomExecutor(2); |
844 |
|
try { |
845 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
845 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
846 |
> |
shouldThrow(); |
847 |
|
} catch (NullPointerException success) { |
972 |
– |
} catch(Exception ex) { |
973 |
– |
unexpectedException(); |
848 |
|
} finally { |
849 |
|
joinPool(e); |
850 |
|
} |
853 |
|
/** |
854 |
|
* timed invokeAny(,,null) throws NPE |
855 |
|
*/ |
856 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
856 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
857 |
|
ExecutorService e = new CustomExecutor(2); |
858 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
859 |
+ |
l.add(new StringTask()); |
860 |
|
try { |
985 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
986 |
– |
l.add(new StringTask()); |
861 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
862 |
+ |
shouldThrow(); |
863 |
|
} catch (NullPointerException success) { |
989 |
– |
} catch(Exception ex) { |
990 |
– |
unexpectedException(); |
864 |
|
} finally { |
865 |
|
joinPool(e); |
866 |
|
} |
869 |
|
/** |
870 |
|
* timed invokeAny(empty collection) throws IAE |
871 |
|
*/ |
872 |
< |
public void testTimedInvokeAny2() { |
872 |
> |
public void testTimedInvokeAny2() throws Exception { |
873 |
|
ExecutorService e = new CustomExecutor(2); |
874 |
|
try { |
875 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
875 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
876 |
> |
shouldThrow(); |
877 |
|
} catch (IllegalArgumentException success) { |
1004 |
– |
} catch(Exception ex) { |
1005 |
– |
unexpectedException(); |
878 |
|
} finally { |
879 |
|
joinPool(e); |
880 |
|
} |
883 |
|
/** |
884 |
|
* timed invokeAny(c) throws NPE if c has null elements |
885 |
|
*/ |
886 |
< |
public void testTimedInvokeAny3() { |
886 |
> |
public void testTimedInvokeAny3() throws Exception { |
887 |
> |
CountDownLatch latch = new CountDownLatch(1); |
888 |
|
ExecutorService e = new CustomExecutor(2); |
889 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
890 |
+ |
l.add(latchAwaitingStringTask(latch)); |
891 |
+ |
l.add(null); |
892 |
|
try { |
893 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
894 |
< |
l.add(new StringTask()); |
1019 |
< |
l.add(null); |
1020 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
893 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
894 |
> |
shouldThrow(); |
895 |
|
} catch (NullPointerException success) { |
1022 |
– |
} catch(Exception ex) { |
1023 |
– |
ex.printStackTrace(); |
1024 |
– |
unexpectedException(); |
896 |
|
} finally { |
897 |
+ |
latch.countDown(); |
898 |
|
joinPool(e); |
899 |
|
} |
900 |
|
} |
902 |
|
/** |
903 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
904 |
|
*/ |
905 |
< |
public void testTimedInvokeAny4() { |
905 |
> |
public void testTimedInvokeAny4() throws Exception { |
906 |
|
ExecutorService e = new CustomExecutor(2); |
907 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
908 |
+ |
l.add(new NPETask()); |
909 |
|
try { |
910 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
911 |
< |
l.add(new NPETask()); |
912 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
913 |
< |
} catch(ExecutionException success) { |
1040 |
< |
} catch(Exception ex) { |
1041 |
< |
unexpectedException(); |
910 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
911 |
> |
shouldThrow(); |
912 |
> |
} catch (ExecutionException success) { |
913 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
914 |
|
} finally { |
915 |
|
joinPool(e); |
916 |
|
} |
919 |
|
/** |
920 |
|
* timed invokeAny(c) returns result of some task |
921 |
|
*/ |
922 |
< |
public void testTimedInvokeAny5() { |
922 |
> |
public void testTimedInvokeAny5() throws Exception { |
923 |
|
ExecutorService e = new CustomExecutor(2); |
924 |
|
try { |
925 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
925 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
926 |
|
l.add(new StringTask()); |
927 |
|
l.add(new StringTask()); |
928 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
928 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
929 |
|
assertSame(TEST_STRING, result); |
1058 |
– |
} catch (ExecutionException success) { |
1059 |
– |
} catch(Exception ex) { |
1060 |
– |
unexpectedException(); |
930 |
|
} finally { |
931 |
|
joinPool(e); |
932 |
|
} |
935 |
|
/** |
936 |
|
* timed invokeAll(null) throws NPE |
937 |
|
*/ |
938 |
< |
public void testTimedInvokeAll1() { |
938 |
> |
public void testTimedInvokeAll1() throws Exception { |
939 |
|
ExecutorService e = new CustomExecutor(2); |
940 |
|
try { |
941 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
941 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
942 |
> |
shouldThrow(); |
943 |
|
} catch (NullPointerException success) { |
1074 |
– |
} catch(Exception ex) { |
1075 |
– |
unexpectedException(); |
944 |
|
} finally { |
945 |
|
joinPool(e); |
946 |
|
} |
949 |
|
/** |
950 |
|
* timed invokeAll(,,null) throws NPE |
951 |
|
*/ |
952 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
952 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
953 |
|
ExecutorService e = new CustomExecutor(2); |
954 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
955 |
+ |
l.add(new StringTask()); |
956 |
|
try { |
1087 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1088 |
– |
l.add(new StringTask()); |
957 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
958 |
+ |
shouldThrow(); |
959 |
|
} catch (NullPointerException success) { |
1091 |
– |
} catch(Exception ex) { |
1092 |
– |
unexpectedException(); |
960 |
|
} finally { |
961 |
|
joinPool(e); |
962 |
|
} |
965 |
|
/** |
966 |
|
* timed invokeAll(empty collection) returns empty collection |
967 |
|
*/ |
968 |
< |
public void testTimedInvokeAll2() { |
968 |
> |
public void testTimedInvokeAll2() throws Exception { |
969 |
|
ExecutorService e = new CustomExecutor(2); |
970 |
|
try { |
971 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
971 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
972 |
|
assertTrue(r.isEmpty()); |
1106 |
– |
} catch(Exception ex) { |
1107 |
– |
unexpectedException(); |
973 |
|
} finally { |
974 |
|
joinPool(e); |
975 |
|
} |
978 |
|
/** |
979 |
|
* timed invokeAll(c) throws NPE if c has null elements |
980 |
|
*/ |
981 |
< |
public void testTimedInvokeAll3() { |
981 |
> |
public void testTimedInvokeAll3() throws Exception { |
982 |
|
ExecutorService e = new CustomExecutor(2); |
983 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
984 |
+ |
l.add(new StringTask()); |
985 |
+ |
l.add(null); |
986 |
|
try { |
987 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
988 |
< |
l.add(new StringTask()); |
1121 |
< |
l.add(null); |
1122 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
987 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
988 |
> |
shouldThrow(); |
989 |
|
} catch (NullPointerException success) { |
1124 |
– |
} catch(Exception ex) { |
1125 |
– |
unexpectedException(); |
990 |
|
} finally { |
991 |
|
joinPool(e); |
992 |
|
} |
995 |
|
/** |
996 |
|
* get of element of invokeAll(c) throws exception on failed task |
997 |
|
*/ |
998 |
< |
public void testTimedInvokeAll4() { |
998 |
> |
public void testTimedInvokeAll4() throws Exception { |
999 |
|
ExecutorService e = new CustomExecutor(2); |
1000 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1001 |
+ |
l.add(new NPETask()); |
1002 |
+ |
List<Future<String>> futures = |
1003 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1004 |
+ |
assertEquals(1, futures.size()); |
1005 |
|
try { |
1006 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1007 |
< |
l.add(new NPETask()); |
1008 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1009 |
< |
assertEquals(1, result.size()); |
1141 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1142 |
< |
it.next().get(); |
1143 |
< |
} catch(ExecutionException success) { |
1144 |
< |
} catch(Exception ex) { |
1145 |
< |
unexpectedException(); |
1006 |
> |
futures.get(0).get(); |
1007 |
> |
shouldThrow(); |
1008 |
> |
} catch (ExecutionException success) { |
1009 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1010 |
|
} finally { |
1011 |
|
joinPool(e); |
1012 |
|
} |
1015 |
|
/** |
1016 |
|
* timed invokeAll(c) returns results of all completed tasks |
1017 |
|
*/ |
1018 |
< |
public void testTimedInvokeAll5() { |
1018 |
> |
public void testTimedInvokeAll5() throws Exception { |
1019 |
|
ExecutorService e = new CustomExecutor(2); |
1020 |
|
try { |
1021 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1021 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1022 |
|
l.add(new StringTask()); |
1023 |
|
l.add(new StringTask()); |
1024 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1025 |
< |
assertEquals(2, result.size()); |
1026 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1027 |
< |
assertSame(TEST_STRING, it.next().get()); |
1028 |
< |
} catch (ExecutionException success) { |
1165 |
< |
} catch(Exception ex) { |
1166 |
< |
unexpectedException(); |
1024 |
> |
List<Future<String>> futures = |
1025 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1026 |
> |
assertEquals(2, futures.size()); |
1027 |
> |
for (Future<String> future : futures) |
1028 |
> |
assertSame(TEST_STRING, future.get()); |
1029 |
|
} finally { |
1030 |
|
joinPool(e); |
1031 |
|
} |
1034 |
|
/** |
1035 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1036 |
|
*/ |
1037 |
< |
public void testTimedInvokeAll6() { |
1037 |
> |
public void testTimedInvokeAll6() throws Exception { |
1038 |
|
ExecutorService e = new CustomExecutor(2); |
1039 |
|
try { |
1040 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1040 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1041 |
|
l.add(new StringTask()); |
1042 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1043 |
|
l.add(new StringTask()); |
1044 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1045 |
< |
assertEquals(3, result.size()); |
1046 |
< |
Iterator<Future<String>> it = result.iterator(); |
1044 |
> |
List<Future<String>> futures = |
1045 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1046 |
> |
assertEquals(3, futures.size()); |
1047 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1048 |
|
Future<String> f1 = it.next(); |
1049 |
|
Future<String> f2 = it.next(); |
1050 |
|
Future<String> f3 = it.next(); |
1053 |
|
assertTrue(f3.isDone()); |
1054 |
|
assertFalse(f1.isCancelled()); |
1055 |
|
assertTrue(f2.isCancelled()); |
1193 |
– |
} catch(Exception ex) { |
1194 |
– |
unexpectedException(); |
1056 |
|
} finally { |
1057 |
|
joinPool(e); |
1058 |
|
} |
1059 |
|
} |
1060 |
|
|
1200 |
– |
|
1061 |
|
} |