1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
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(ScheduledExecutorSubclassTest.class); |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
– |
|
51 |
|
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
52 |
|
|
53 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) { |
72 |
|
|
73 |
|
} |
74 |
|
|
76 |
– |
|
77 |
– |
|
75 |
|
/** |
76 |
|
* execute successfully executes a runnable |
77 |
|
*/ |
78 |
|
public void testExecute() throws InterruptedException { |
79 |
< |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
80 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
81 |
< |
p1.execute(runnable); |
82 |
< |
assertFalse(runnable.done); |
83 |
< |
Thread.sleep(SHORT_DELAY_MS); |
84 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
85 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
86 |
< |
assertTrue(runnable.done); |
87 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
88 |
< |
joinPool(p1); |
79 |
> |
CustomExecutor p = new CustomExecutor(1); |
80 |
> |
final CountDownLatch done = new CountDownLatch(1); |
81 |
> |
final Runnable task = new CheckedRunnable() { |
82 |
> |
public void realRun() { |
83 |
> |
done.countDown(); |
84 |
> |
}}; |
85 |
> |
try { |
86 |
> |
p.execute(task); |
87 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
> |
} finally { |
89 |
> |
joinPool(p); |
90 |
> |
} |
91 |
|
} |
92 |
|
|
94 |
– |
|
93 |
|
/** |
94 |
|
* delayed schedule of callable successfully executes after delay |
95 |
|
*/ |
96 |
|
public void testSchedule1() throws Exception { |
97 |
< |
TrackedCallable callable = new TrackedCallable(); |
98 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
99 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
100 |
< |
assertFalse(callable.done); |
101 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
102 |
< |
assertTrue(callable.done); |
103 |
< |
assertEquals(Boolean.TRUE, f.get()); |
104 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
105 |
< |
joinPool(p1); |
97 |
> |
CustomExecutor p = new CustomExecutor(1); |
98 |
> |
final long startTime = System.nanoTime(); |
99 |
> |
final CountDownLatch done = new CountDownLatch(1); |
100 |
> |
try { |
101 |
> |
Callable task = new CheckedCallable<Boolean>() { |
102 |
> |
public Boolean realCall() { |
103 |
> |
done.countDown(); |
104 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
105 |
> |
return Boolean.TRUE; |
106 |
> |
}}; |
107 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
108 |
> |
assertSame(Boolean.TRUE, f.get()); |
109 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
110 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
111 |
> |
} finally { |
112 |
> |
joinPool(p); |
113 |
> |
} |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
< |
* delayed schedule of runnable successfully executes after delay |
118 |
< |
*/ |
119 |
< |
public void testSchedule3() throws InterruptedException { |
120 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
121 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
122 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
123 |
< |
Thread.sleep(SHORT_DELAY_MS); |
124 |
< |
assertFalse(runnable.done); |
125 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
126 |
< |
assertTrue(runnable.done); |
127 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
128 |
< |
joinPool(p1); |
117 |
> |
* delayed schedule of runnable successfully executes after delay |
118 |
> |
*/ |
119 |
> |
public void testSchedule3() throws Exception { |
120 |
> |
CustomExecutor p = new CustomExecutor(1); |
121 |
> |
final long startTime = System.nanoTime(); |
122 |
> |
final CountDownLatch done = new CountDownLatch(1); |
123 |
> |
try { |
124 |
> |
Runnable task = new CheckedRunnable() { |
125 |
> |
public void realRun() { |
126 |
> |
done.countDown(); |
127 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
> |
}}; |
129 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
130 |
> |
await(done); |
131 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
132 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
133 |
> |
} finally { |
134 |
> |
joinPool(p); |
135 |
> |
} |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
140 |
|
*/ |
141 |
|
public void testSchedule4() throws InterruptedException { |
142 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
143 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
144 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
145 |
< |
assertFalse(runnable.done); |
146 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
147 |
< |
assertTrue(runnable.done); |
148 |
< |
h.cancel(true); |
149 |
< |
joinPool(p1); |
150 |
< |
} |
151 |
< |
|
152 |
< |
static class RunnableCounter implements Runnable { |
153 |
< |
AtomicInteger count = new AtomicInteger(0); |
154 |
< |
public void run() { count.getAndIncrement(); } |
142 |
> |
CustomExecutor p = new CustomExecutor(1); |
143 |
> |
final long startTime = System.nanoTime(); |
144 |
> |
final CountDownLatch done = new CountDownLatch(1); |
145 |
> |
try { |
146 |
> |
Runnable task = new CheckedRunnable() { |
147 |
> |
public void realRun() { |
148 |
> |
done.countDown(); |
149 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
150 |
> |
}}; |
151 |
> |
ScheduledFuture f = |
152 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
153 |
> |
LONG_DELAY_MS, MILLISECONDS); |
154 |
> |
await(done); |
155 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
156 |
> |
f.cancel(true); |
157 |
> |
} finally { |
158 |
> |
joinPool(p); |
159 |
> |
} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
164 |
|
*/ |
165 |
|
public void testSchedule5() throws InterruptedException { |
166 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
167 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
168 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
169 |
< |
assertFalse(runnable.done); |
170 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
171 |
< |
assertTrue(runnable.done); |
172 |
< |
h.cancel(true); |
173 |
< |
joinPool(p1); |
166 |
> |
CustomExecutor p = new CustomExecutor(1); |
167 |
> |
final long startTime = System.nanoTime(); |
168 |
> |
final CountDownLatch done = new CountDownLatch(1); |
169 |
> |
try { |
170 |
> |
Runnable task = new CheckedRunnable() { |
171 |
> |
public void realRun() { |
172 |
> |
done.countDown(); |
173 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
174 |
> |
}}; |
175 |
> |
ScheduledFuture f = |
176 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
177 |
> |
LONG_DELAY_MS, MILLISECONDS); |
178 |
> |
await(done); |
179 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
180 |
> |
f.cancel(true); |
181 |
> |
} finally { |
182 |
> |
joinPool(p); |
183 |
> |
} |
184 |
> |
} |
185 |
> |
|
186 |
> |
static class RunnableCounter implements Runnable { |
187 |
> |
AtomicInteger count = new AtomicInteger(0); |
188 |
> |
public void run() { count.getAndIncrement(); } |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
193 |
|
*/ |
194 |
|
public void testFixedRateSequence() throws InterruptedException { |
195 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
195 |
> |
CustomExecutor p = new CustomExecutor(1); |
196 |
|
RunnableCounter counter = new RunnableCounter(); |
197 |
|
ScheduledFuture h = |
198 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
199 |
< |
Thread.sleep(SMALL_DELAY_MS); |
198 |
> |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
199 |
> |
delay(SMALL_DELAY_MS); |
200 |
|
h.cancel(true); |
201 |
|
int c = counter.count.get(); |
202 |
|
// By time scaling conventions, we must have at least |
203 |
|
// an execution per SHORT delay, but no more than one SHORT more |
204 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
205 |
|
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
206 |
< |
joinPool(p1); |
206 |
> |
joinPool(p); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
211 |
|
*/ |
212 |
|
public void testFixedDelaySequence() throws InterruptedException { |
213 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
213 |
> |
CustomExecutor p = new CustomExecutor(1); |
214 |
|
RunnableCounter counter = new RunnableCounter(); |
215 |
|
ScheduledFuture h = |
216 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
217 |
< |
Thread.sleep(SMALL_DELAY_MS); |
216 |
> |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
217 |
> |
delay(SMALL_DELAY_MS); |
218 |
|
h.cancel(true); |
219 |
|
int c = counter.count.get(); |
220 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
221 |
|
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
222 |
< |
joinPool(p1); |
222 |
> |
joinPool(p); |
223 |
|
} |
224 |
|
|
192 |
– |
|
225 |
|
/** |
226 |
< |
* execute (null) throws NPE |
226 |
> |
* execute(null) throws NPE |
227 |
|
*/ |
228 |
|
public void testExecuteNull() throws InterruptedException { |
229 |
|
CustomExecutor se = new CustomExecutor(1); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* schedule (null) throws NPE |
238 |
> |
* schedule(null) throws NPE |
239 |
|
*/ |
240 |
|
public void testScheduleNull() throws InterruptedException { |
241 |
|
CustomExecutor se = new CustomExecutor(1); |
283 |
|
/** |
284 |
|
* schedule callable throws RejectedExecutionException if shutdown |
285 |
|
*/ |
286 |
< |
public void testSchedule3_RejectedExecutionException() { |
287 |
< |
CustomExecutor se = new CustomExecutor(1); |
288 |
< |
try { |
289 |
< |
se.shutdown(); |
290 |
< |
se.schedule(new NoOpCallable(), |
291 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
292 |
< |
shouldThrow(); |
293 |
< |
} catch (RejectedExecutionException success) { |
294 |
< |
} catch (SecurityException ok) { |
295 |
< |
} |
296 |
< |
joinPool(se); |
286 |
> |
public void testSchedule3_RejectedExecutionException() { |
287 |
> |
CustomExecutor se = new CustomExecutor(1); |
288 |
> |
try { |
289 |
> |
se.shutdown(); |
290 |
> |
se.schedule(new NoOpCallable(), |
291 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
292 |
> |
shouldThrow(); |
293 |
> |
} catch (RejectedExecutionException success) { |
294 |
> |
} catch (SecurityException ok) { |
295 |
> |
} |
296 |
> |
joinPool(se); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
300 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
301 |
|
*/ |
302 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
303 |
|
CustomExecutor se = new CustomExecutor(1); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* getActiveCount increases but doesn't overestimate, when a |
333 |
< |
* thread becomes active |
332 |
> |
* getActiveCount increases but doesn't overestimate, when a |
333 |
> |
* thread becomes active |
334 |
|
*/ |
335 |
|
public void testGetActiveCount() throws InterruptedException { |
336 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
337 |
< |
assertEquals(0, p2.getActiveCount()); |
338 |
< |
p2.execute(new SmallRunnable()); |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
340 |
< |
assertEquals(1, p2.getActiveCount()); |
341 |
< |
joinPool(p2); |
336 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
337 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
338 |
> |
final CountDownLatch done = new CountDownLatch(1); |
339 |
> |
try { |
340 |
> |
assertEquals(0, p.getActiveCount()); |
341 |
> |
p.execute(new CheckedRunnable() { |
342 |
> |
public void realRun() throws InterruptedException { |
343 |
> |
threadStarted.countDown(); |
344 |
> |
assertEquals(1, p.getActiveCount()); |
345 |
> |
done.await(); |
346 |
> |
}}); |
347 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
348 |
> |
assertEquals(1, p.getActiveCount()); |
349 |
> |
} finally { |
350 |
> |
done.countDown(); |
351 |
> |
joinPool(p); |
352 |
> |
} |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
357 |
< |
* when tasks complete |
356 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
357 |
> |
* when tasks complete |
358 |
|
*/ |
359 |
< |
public void testGetCompletedTaskCount()throws InterruptedException { |
360 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
361 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
362 |
< |
p2.execute(new SmallRunnable()); |
363 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
364 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
365 |
< |
joinPool(p2); |
359 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
360 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
361 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
362 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
363 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
364 |
> |
try { |
365 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
366 |
> |
p.execute(new CheckedRunnable() { |
367 |
> |
public void realRun() throws InterruptedException { |
368 |
> |
threadStarted.countDown(); |
369 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
370 |
> |
threadProceed.await(); |
371 |
> |
threadDone.countDown(); |
372 |
> |
}}); |
373 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
374 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
375 |
> |
threadProceed.countDown(); |
376 |
> |
threadDone.await(); |
377 |
> |
long startTime = System.nanoTime(); |
378 |
> |
while (p.getCompletedTaskCount() != 1) { |
379 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
380 |
> |
fail("timed out"); |
381 |
> |
Thread.yield(); |
382 |
> |
} |
383 |
> |
} finally { |
384 |
> |
joinPool(p); |
385 |
> |
} |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
389 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
390 |
|
*/ |
391 |
|
public void testGetCorePoolSize() { |
392 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
393 |
< |
assertEquals(1, p1.getCorePoolSize()); |
394 |
< |
joinPool(p1); |
392 |
> |
CustomExecutor p = new CustomExecutor(1); |
393 |
> |
assertEquals(1, p.getCorePoolSize()); |
394 |
> |
joinPool(p); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
399 |
< |
* multiple threads active |
398 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
399 |
> |
* multiple threads active |
400 |
|
*/ |
401 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
402 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
403 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
404 |
< |
p2.execute(new SmallRunnable()); |
405 |
< |
p2.execute(new SmallRunnable()); |
406 |
< |
Thread.sleep(SHORT_DELAY_MS); |
407 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
408 |
< |
joinPool(p2); |
402 |
> |
final int THREADS = 3; |
403 |
> |
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
404 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
405 |
> |
final CountDownLatch done = new CountDownLatch(1); |
406 |
> |
try { |
407 |
> |
assertEquals(0, p.getLargestPoolSize()); |
408 |
> |
for (int i = 0; i < THREADS; i++) |
409 |
> |
p.execute(new CheckedRunnable() { |
410 |
> |
public void realRun() throws InterruptedException { |
411 |
> |
threadsStarted.countDown(); |
412 |
> |
done.await(); |
413 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
414 |
> |
}}); |
415 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
416 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
417 |
> |
} finally { |
418 |
> |
done.countDown(); |
419 |
> |
joinPool(p); |
420 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
421 |
> |
} |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
426 |
< |
* become active |
425 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
426 |
> |
* become active |
427 |
|
*/ |
428 |
< |
public void testGetPoolSize() { |
429 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
430 |
< |
assertEquals(0, p1.getPoolSize()); |
431 |
< |
p1.execute(new SmallRunnable()); |
432 |
< |
assertEquals(1, p1.getPoolSize()); |
433 |
< |
joinPool(p1); |
428 |
> |
public void testGetPoolSize() throws InterruptedException { |
429 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
430 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
431 |
> |
final CountDownLatch done = new CountDownLatch(1); |
432 |
> |
try { |
433 |
> |
assertEquals(0, p.getPoolSize()); |
434 |
> |
p.execute(new CheckedRunnable() { |
435 |
> |
public void realRun() throws InterruptedException { |
436 |
> |
threadStarted.countDown(); |
437 |
> |
assertEquals(1, p.getPoolSize()); |
438 |
> |
done.await(); |
439 |
> |
}}); |
440 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
441 |
> |
assertEquals(1, p.getPoolSize()); |
442 |
> |
} finally { |
443 |
> |
done.countDown(); |
444 |
> |
joinPool(p); |
445 |
> |
} |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
450 |
< |
* submitted |
449 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
450 |
> |
* submitted |
451 |
|
*/ |
452 |
|
public void testGetTaskCount() throws InterruptedException { |
453 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
454 |
< |
assertEquals(0, p1.getTaskCount()); |
455 |
< |
for (int i = 0; i < 5; i++) |
456 |
< |
p1.execute(new SmallRunnable()); |
457 |
< |
Thread.sleep(SHORT_DELAY_MS); |
458 |
< |
assertEquals(5, p1.getTaskCount()); |
459 |
< |
joinPool(p1); |
453 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
454 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
455 |
> |
final CountDownLatch done = new CountDownLatch(1); |
456 |
> |
final int TASKS = 5; |
457 |
> |
try { |
458 |
> |
assertEquals(0, p.getTaskCount()); |
459 |
> |
for (int i = 0; i < TASKS; i++) |
460 |
> |
p.execute(new CheckedRunnable() { |
461 |
> |
public void realRun() throws InterruptedException { |
462 |
> |
threadStarted.countDown(); |
463 |
> |
done.await(); |
464 |
> |
}}); |
465 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
466 |
> |
assertEquals(TASKS, p.getTaskCount()); |
467 |
> |
} finally { |
468 |
> |
done.countDown(); |
469 |
> |
joinPool(p); |
470 |
> |
} |
471 |
|
} |
472 |
|
|
473 |
|
/** |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* is isShutDown is false before shutdown, true after |
509 |
> |
* isShutdown is false before shutdown, true after |
510 |
|
*/ |
511 |
|
public void testIsShutdown() { |
512 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
512 |
> |
CustomExecutor p = new CustomExecutor(1); |
513 |
|
try { |
514 |
< |
assertFalse(p1.isShutdown()); |
514 |
> |
assertFalse(p.isShutdown()); |
515 |
|
} |
516 |
|
finally { |
517 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
517 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
518 |
|
} |
519 |
< |
assertTrue(p1.isShutdown()); |
519 |
> |
assertTrue(p.isShutdown()); |
520 |
|
} |
521 |
|
|
423 |
– |
|
522 |
|
/** |
523 |
< |
* isTerminated is false before termination, true after |
523 |
> |
* isTerminated is false before termination, true after |
524 |
|
*/ |
525 |
|
public void testIsTerminated() throws InterruptedException { |
526 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
526 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
527 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
528 |
> |
final CountDownLatch done = new CountDownLatch(1); |
529 |
> |
assertFalse(p.isTerminated()); |
530 |
|
try { |
531 |
< |
p1.execute(new SmallRunnable()); |
531 |
> |
p.execute(new CheckedRunnable() { |
532 |
> |
public void realRun() throws InterruptedException { |
533 |
> |
assertFalse(p.isTerminated()); |
534 |
> |
threadStarted.countDown(); |
535 |
> |
done.await(); |
536 |
> |
}}); |
537 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
538 |
> |
assertFalse(p.isTerminating()); |
539 |
> |
done.countDown(); |
540 |
|
} finally { |
541 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
541 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
542 |
|
} |
543 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
544 |
< |
assertTrue(p1.isTerminated()); |
543 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
544 |
> |
assertTrue(p.isTerminated()); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
< |
* isTerminating is not true when running or when terminated |
548 |
> |
* isTerminating is not true when running or when terminated |
549 |
|
*/ |
550 |
|
public void testIsTerminating() throws InterruptedException { |
551 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
552 |
< |
assertFalse(p1.isTerminating()); |
553 |
< |
try { |
554 |
< |
p1.execute(new SmallRunnable()); |
555 |
< |
assertFalse(p1.isTerminating()); |
556 |
< |
} finally { |
557 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
558 |
< |
} |
559 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
560 |
< |
assertTrue(p1.isTerminated()); |
561 |
< |
assertFalse(p1.isTerminating()); |
551 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
552 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
553 |
> |
final CountDownLatch done = new CountDownLatch(1); |
554 |
> |
try { |
555 |
> |
assertFalse(p.isTerminating()); |
556 |
> |
p.execute(new CheckedRunnable() { |
557 |
> |
public void realRun() throws InterruptedException { |
558 |
> |
assertFalse(p.isTerminating()); |
559 |
> |
threadStarted.countDown(); |
560 |
> |
done.await(); |
561 |
> |
}}); |
562 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
563 |
> |
assertFalse(p.isTerminating()); |
564 |
> |
done.countDown(); |
565 |
> |
} finally { |
566 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
567 |
> |
} |
568 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
569 |
> |
assertTrue(p.isTerminated()); |
570 |
> |
assertFalse(p.isTerminating()); |
571 |
|
} |
572 |
|
|
573 |
|
/** |
574 |
|
* getQueue returns the work queue, which contains queued tasks |
575 |
|
*/ |
576 |
|
public void testGetQueue() throws InterruptedException { |
577 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
578 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
579 |
< |
for (int i = 0; i < 5; i++) { |
580 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
581 |
< |
} |
582 |
< |
try { |
583 |
< |
Thread.sleep(SHORT_DELAY_MS); |
584 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
585 |
< |
assertTrue(q.contains(tasks[4])); |
577 |
> |
ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
578 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
579 |
> |
final CountDownLatch done = new CountDownLatch(1); |
580 |
> |
try { |
581 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
582 |
> |
for (int i = 0; i < tasks.length; i++) { |
583 |
> |
Runnable r = new CheckedRunnable() { |
584 |
> |
public void realRun() throws InterruptedException { |
585 |
> |
threadStarted.countDown(); |
586 |
> |
done.await(); |
587 |
> |
}}; |
588 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
589 |
> |
} |
590 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
591 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
592 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
593 |
|
assertFalse(q.contains(tasks[0])); |
594 |
|
} finally { |
595 |
< |
joinPool(p1); |
595 |
> |
done.countDown(); |
596 |
> |
joinPool(p); |
597 |
|
} |
598 |
|
} |
599 |
|
|
601 |
|
* remove(task) removes queued task, and fails to remove active task |
602 |
|
*/ |
603 |
|
public void testRemove() throws InterruptedException { |
604 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
604 |
> |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
605 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
606 |
< |
for (int i = 0; i < 5; i++) { |
607 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
482 |
< |
} |
606 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
607 |
> |
final CountDownLatch done = new CountDownLatch(1); |
608 |
|
try { |
609 |
< |
Thread.sleep(SHORT_DELAY_MS); |
610 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
611 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
609 |
> |
for (int i = 0; i < tasks.length; i++) { |
610 |
> |
Runnable r = new CheckedRunnable() { |
611 |
> |
public void realRun() throws InterruptedException { |
612 |
> |
threadStarted.countDown(); |
613 |
> |
done.await(); |
614 |
> |
}}; |
615 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
616 |
> |
} |
617 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
618 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
619 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
620 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
621 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
622 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
623 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
622 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
623 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
624 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
625 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
626 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
626 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
627 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
628 |
|
} finally { |
629 |
< |
joinPool(p1); |
629 |
> |
done.countDown(); |
630 |
> |
joinPool(p); |
631 |
|
} |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* purge removes cancelled tasks from the queue |
635 |
> |
* purge removes cancelled tasks from the queue |
636 |
|
*/ |
637 |
|
public void testPurge() throws InterruptedException { |
638 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
638 |
> |
CustomExecutor p = new CustomExecutor(1); |
639 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
640 |
< |
for (int i = 0; i < 5; i++) { |
641 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
642 |
< |
} |
640 |
> |
for (int i = 0; i < tasks.length; i++) |
641 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
642 |
> |
LONG_DELAY_MS, MILLISECONDS); |
643 |
|
try { |
644 |
< |
int max = 5; |
644 |
> |
int max = tasks.length; |
645 |
|
if (tasks[4].cancel(true)) --max; |
646 |
|
if (tasks[3].cancel(true)) --max; |
647 |
|
// There must eventually be an interference-free point at |
648 |
|
// which purge will not fail. (At worst, when queue is empty.) |
649 |
< |
int k; |
650 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
651 |
< |
p1.purge(); |
652 |
< |
long count = p1.getTaskCount(); |
653 |
< |
if (count >= 0 && count <= max) |
654 |
< |
break; |
655 |
< |
Thread.sleep(1); |
656 |
< |
} |
523 |
< |
assertTrue(k < SMALL_DELAY_MS); |
649 |
> |
long startTime = System.nanoTime(); |
650 |
> |
do { |
651 |
> |
p.purge(); |
652 |
> |
long count = p.getTaskCount(); |
653 |
> |
if (count == max) |
654 |
> |
return; |
655 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
656 |
> |
fail("Purge failed to remove cancelled tasks"); |
657 |
|
} finally { |
658 |
< |
joinPool(p1); |
658 |
> |
for (ScheduledFuture task : tasks) |
659 |
> |
task.cancel(true); |
660 |
> |
joinPool(p); |
661 |
|
} |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* shutDownNow returns a list containing tasks that were not run |
665 |
> |
* shutdownNow returns a list containing tasks that were not run |
666 |
|
*/ |
667 |
< |
public void testShutDownNow() { |
668 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
667 |
> |
public void testShutdownNow() { |
668 |
> |
CustomExecutor p = new CustomExecutor(1); |
669 |
|
for (int i = 0; i < 5; i++) |
670 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
671 |
< |
List l; |
670 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
671 |
> |
LONG_DELAY_MS, MILLISECONDS); |
672 |
|
try { |
673 |
< |
l = p1.shutdownNow(); |
673 |
> |
List<Runnable> l = p.shutdownNow(); |
674 |
> |
assertTrue(p.isShutdown()); |
675 |
> |
assertEquals(5, l.size()); |
676 |
|
} catch (SecurityException ok) { |
677 |
< |
return; |
677 |
> |
// Allowed in case test doesn't have privs |
678 |
> |
} finally { |
679 |
> |
joinPool(p); |
680 |
|
} |
542 |
– |
assertTrue(p1.isShutdown()); |
543 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
544 |
– |
joinPool(p1); |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
|
* In default setting, shutdown cancels periodic but not delayed |
685 |
|
* tasks at shutdown |
686 |
|
*/ |
687 |
< |
public void testShutDown1() throws InterruptedException { |
688 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
689 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
690 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
687 |
> |
public void testShutdown1() throws InterruptedException { |
688 |
> |
CustomExecutor p = new CustomExecutor(1); |
689 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
690 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
691 |
|
|
692 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
693 |
< |
for (int i = 0; i < 5; i++) |
694 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
695 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
696 |
< |
BlockingQueue q = p1.getQueue(); |
697 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
698 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
699 |
< |
assertFalse(t.isCancelled()); |
700 |
< |
} |
701 |
< |
assertTrue(p1.isShutdown()); |
702 |
< |
Thread.sleep(SMALL_DELAY_MS); |
703 |
< |
for (int i = 0; i < 5; ++i) { |
704 |
< |
assertTrue(tasks[i].isDone()); |
705 |
< |
assertFalse(tasks[i].isCancelled()); |
693 |
> |
for (int i = 0; i < tasks.length; i++) |
694 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
695 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
696 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
697 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
698 |
> |
for (ScheduledFuture task : tasks) { |
699 |
> |
assertFalse(task.isDone()); |
700 |
> |
assertFalse(task.isCancelled()); |
701 |
> |
assertTrue(q.contains(task)); |
702 |
> |
} |
703 |
> |
assertTrue(p.isShutdown()); |
704 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
705 |
> |
assertTrue(p.isTerminated()); |
706 |
> |
for (ScheduledFuture task : tasks) { |
707 |
> |
assertTrue(task.isDone()); |
708 |
> |
assertFalse(task.isCancelled()); |
709 |
|
} |
710 |
|
} |
711 |
|
|
573 |
– |
|
712 |
|
/** |
713 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
714 |
|
* delayed tasks are cancelled at shutdown |
715 |
|
*/ |
716 |
< |
public void testShutDown2() throws InterruptedException { |
717 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
718 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
716 |
> |
public void testShutdown2() throws InterruptedException { |
717 |
> |
CustomExecutor p = new CustomExecutor(1); |
718 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
719 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
720 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
721 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
722 |
< |
for (int i = 0; i < 5; i++) |
723 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
724 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
725 |
< |
assertTrue(p1.isShutdown()); |
726 |
< |
BlockingQueue q = p1.getQueue(); |
722 |
> |
for (int i = 0; i < tasks.length; i++) |
723 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
724 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
725 |
> |
BlockingQueue q = p.getQueue(); |
726 |
> |
assertEquals(tasks.length, q.size()); |
727 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
728 |
> |
assertTrue(p.isShutdown()); |
729 |
|
assertTrue(q.isEmpty()); |
730 |
< |
Thread.sleep(SMALL_DELAY_MS); |
731 |
< |
assertTrue(p1.isTerminated()); |
730 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
731 |
> |
assertTrue(p.isTerminated()); |
732 |
> |
for (ScheduledFuture task : tasks) { |
733 |
> |
assertTrue(task.isDone()); |
734 |
> |
assertTrue(task.isCancelled()); |
735 |
> |
} |
736 |
|
} |
737 |
|
|
592 |
– |
|
738 |
|
/** |
739 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
740 |
< |
* periodic tasks are not cancelled at shutdown |
740 |
> |
* periodic tasks are cancelled at shutdown |
741 |
|
*/ |
742 |
< |
public void testShutDown3() throws InterruptedException { |
743 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
744 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
742 |
> |
public void testShutdown3() throws InterruptedException { |
743 |
> |
CustomExecutor p = new CustomExecutor(1); |
744 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
745 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
746 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
747 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
748 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
749 |
> |
long initialDelay = LONG_DELAY_MS; |
750 |
|
ScheduledFuture task = |
751 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
752 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
753 |
< |
assertTrue(p1.isShutdown()); |
754 |
< |
BlockingQueue q = p1.getQueue(); |
755 |
< |
assertTrue(q.isEmpty()); |
756 |
< |
Thread.sleep(SHORT_DELAY_MS); |
757 |
< |
assertTrue(p1.isTerminated()); |
751 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
752 |
> |
5, MILLISECONDS); |
753 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
754 |
> |
assertTrue(p.isShutdown()); |
755 |
> |
assertTrue(p.getQueue().isEmpty()); |
756 |
> |
assertTrue(task.isDone()); |
757 |
> |
assertTrue(task.isCancelled()); |
758 |
> |
joinPool(p); |
759 |
|
} |
760 |
|
|
761 |
|
/** |
762 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
763 |
< |
* periodic tasks are cancelled at shutdown |
763 |
> |
* periodic tasks are not cancelled at shutdown |
764 |
|
*/ |
765 |
< |
public void testShutDown4() throws InterruptedException { |
766 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
765 |
> |
public void testShutdown4() throws InterruptedException { |
766 |
> |
CustomExecutor p = new CustomExecutor(1); |
767 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
768 |
|
try { |
769 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
769 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
770 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
771 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
772 |
> |
final Runnable r = new CheckedRunnable() { |
773 |
> |
public void realRun() { |
774 |
> |
counter.countDown(); |
775 |
> |
}}; |
776 |
|
ScheduledFuture task = |
777 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
777 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
778 |
> |
assertFalse(task.isDone()); |
779 |
|
assertFalse(task.isCancelled()); |
780 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
780 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
781 |
|
assertFalse(task.isCancelled()); |
782 |
< |
assertFalse(p1.isTerminated()); |
783 |
< |
assertTrue(p1.isShutdown()); |
784 |
< |
Thread.sleep(SHORT_DELAY_MS); |
782 |
> |
assertFalse(p.isTerminated()); |
783 |
> |
assertTrue(p.isShutdown()); |
784 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
785 |
|
assertFalse(task.isCancelled()); |
786 |
< |
assertTrue(task.cancel(true)); |
786 |
> |
assertTrue(task.cancel(false)); |
787 |
|
assertTrue(task.isDone()); |
788 |
< |
Thread.sleep(SHORT_DELAY_MS); |
789 |
< |
assertTrue(p1.isTerminated()); |
788 |
> |
assertTrue(task.isCancelled()); |
789 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
790 |
> |
assertTrue(p.isTerminated()); |
791 |
|
} |
792 |
|
finally { |
793 |
< |
joinPool(p1); |
793 |
> |
joinPool(p); |
794 |
|
} |
795 |
|
} |
796 |
|
|
868 |
|
* invokeAny(c) throws NPE if c has null elements |
869 |
|
*/ |
870 |
|
public void testInvokeAny3() throws Exception { |
871 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
871 |
> |
CountDownLatch latch = new CountDownLatch(1); |
872 |
|
ExecutorService e = new CustomExecutor(2); |
873 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
874 |
+ |
l.add(latchAwaitingStringTask(latch)); |
875 |
+ |
l.add(null); |
876 |
|
try { |
714 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
715 |
– |
l.add(new Callable<String>() { |
716 |
– |
public String call() { |
717 |
– |
try { |
718 |
– |
latch.await(); |
719 |
– |
} catch (InterruptedException ok) {} |
720 |
– |
return TEST_STRING; |
721 |
– |
}}); |
722 |
– |
l.add(null); |
877 |
|
e.invokeAny(l); |
878 |
|
shouldThrow(); |
879 |
|
} catch (NullPointerException success) { |
888 |
|
*/ |
889 |
|
public void testInvokeAny4() throws Exception { |
890 |
|
ExecutorService e = new CustomExecutor(2); |
891 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
892 |
+ |
l.add(new NPETask()); |
893 |
|
try { |
738 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
739 |
– |
l.add(new NPETask()); |
894 |
|
e.invokeAny(l); |
895 |
|
shouldThrow(); |
896 |
|
} catch (ExecutionException success) { |
906 |
|
public void testInvokeAny5() throws Exception { |
907 |
|
ExecutorService e = new CustomExecutor(2); |
908 |
|
try { |
909 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
909 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
910 |
|
l.add(new StringTask()); |
911 |
|
l.add(new StringTask()); |
912 |
|
String result = e.invokeAny(l); |
948 |
|
*/ |
949 |
|
public void testInvokeAll3() throws Exception { |
950 |
|
ExecutorService e = new CustomExecutor(2); |
951 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
952 |
+ |
l.add(new StringTask()); |
953 |
+ |
l.add(null); |
954 |
|
try { |
798 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
799 |
– |
l.add(new StringTask()); |
800 |
– |
l.add(null); |
955 |
|
e.invokeAll(l); |
956 |
|
shouldThrow(); |
957 |
|
} catch (NullPointerException success) { |
965 |
|
*/ |
966 |
|
public void testInvokeAll4() throws Exception { |
967 |
|
ExecutorService e = new CustomExecutor(2); |
968 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
969 |
+ |
l.add(new NPETask()); |
970 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
971 |
+ |
assertEquals(1, futures.size()); |
972 |
|
try { |
973 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
816 |
< |
l.add(new NPETask()); |
817 |
< |
List<Future<String>> result = e.invokeAll(l); |
818 |
< |
assertEquals(1, result.size()); |
819 |
< |
for (Future<String> future : result) |
820 |
< |
future.get(); |
973 |
> |
futures.get(0).get(); |
974 |
|
shouldThrow(); |
975 |
|
} catch (ExecutionException success) { |
976 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
985 |
|
public void testInvokeAll5() throws Exception { |
986 |
|
ExecutorService e = new CustomExecutor(2); |
987 |
|
try { |
988 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
988 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
989 |
|
l.add(new StringTask()); |
990 |
|
l.add(new StringTask()); |
991 |
< |
List<Future<String>> result = e.invokeAll(l); |
992 |
< |
assertEquals(2, result.size()); |
993 |
< |
for (Future<String> future : result) |
991 |
> |
List<Future<String>> futures = e.invokeAll(l); |
992 |
> |
assertEquals(2, futures.size()); |
993 |
> |
for (Future<String> future : futures) |
994 |
|
assertSame(TEST_STRING, future.get()); |
995 |
|
} finally { |
996 |
|
joinPool(e); |
1016 |
|
*/ |
1017 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1018 |
|
ExecutorService e = new CustomExecutor(2); |
1019 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1020 |
+ |
l.add(new StringTask()); |
1021 |
|
try { |
867 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
– |
l.add(new StringTask()); |
1022 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1023 |
|
shouldThrow(); |
1024 |
|
} catch (NullPointerException success) { |
1045 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1046 |
|
*/ |
1047 |
|
public void testTimedInvokeAny3() throws Exception { |
1048 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
1048 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1049 |
|
ExecutorService e = new CustomExecutor(2); |
1050 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1051 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1052 |
+ |
l.add(null); |
1053 |
|
try { |
898 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
899 |
– |
l.add(new Callable<String>() { |
900 |
– |
public String call() { |
901 |
– |
try { |
902 |
– |
latch.await(); |
903 |
– |
} catch (InterruptedException ok) {} |
904 |
– |
return TEST_STRING; |
905 |
– |
}}); |
906 |
– |
l.add(null); |
1054 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1055 |
|
shouldThrow(); |
1056 |
|
} catch (NullPointerException success) { |
1065 |
|
*/ |
1066 |
|
public void testTimedInvokeAny4() throws Exception { |
1067 |
|
ExecutorService e = new CustomExecutor(2); |
1068 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1069 |
+ |
l.add(new NPETask()); |
1070 |
|
try { |
922 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
923 |
– |
l.add(new NPETask()); |
1071 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1072 |
|
shouldThrow(); |
1073 |
|
} catch (ExecutionException success) { |
1083 |
|
public void testTimedInvokeAny5() throws Exception { |
1084 |
|
ExecutorService e = new CustomExecutor(2); |
1085 |
|
try { |
1086 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1086 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1087 |
|
l.add(new StringTask()); |
1088 |
|
l.add(new StringTask()); |
1089 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1112 |
|
*/ |
1113 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1114 |
|
ExecutorService e = new CustomExecutor(2); |
1115 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1116 |
+ |
l.add(new StringTask()); |
1117 |
|
try { |
969 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
970 |
– |
l.add(new StringTask()); |
1118 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1119 |
|
shouldThrow(); |
1120 |
|
} catch (NullPointerException success) { |
1141 |
|
*/ |
1142 |
|
public void testTimedInvokeAll3() throws Exception { |
1143 |
|
ExecutorService e = new CustomExecutor(2); |
1144 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1145 |
+ |
l.add(new StringTask()); |
1146 |
+ |
l.add(null); |
1147 |
|
try { |
998 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
– |
l.add(new StringTask()); |
1000 |
– |
l.add(null); |
1148 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1149 |
|
shouldThrow(); |
1150 |
|
} catch (NullPointerException success) { |
1158 |
|
*/ |
1159 |
|
public void testTimedInvokeAll4() throws Exception { |
1160 |
|
ExecutorService e = new CustomExecutor(2); |
1161 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1162 |
+ |
l.add(new NPETask()); |
1163 |
+ |
List<Future<String>> futures = |
1164 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1165 |
+ |
assertEquals(1, futures.size()); |
1166 |
|
try { |
1167 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1016 |
< |
l.add(new NPETask()); |
1017 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1018 |
< |
assertEquals(1, result.size()); |
1019 |
< |
for (Future<String> future : result) |
1020 |
< |
future.get(); |
1167 |
> |
futures.get(0).get(); |
1168 |
|
shouldThrow(); |
1169 |
|
} catch (ExecutionException success) { |
1170 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1179 |
|
public void testTimedInvokeAll5() throws Exception { |
1180 |
|
ExecutorService e = new CustomExecutor(2); |
1181 |
|
try { |
1182 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1182 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1183 |
|
l.add(new StringTask()); |
1184 |
|
l.add(new StringTask()); |
1185 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1186 |
< |
assertEquals(2, result.size()); |
1187 |
< |
for (Future<String> future : result) |
1185 |
> |
List<Future<String>> futures = |
1186 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1187 |
> |
assertEquals(2, futures.size()); |
1188 |
> |
for (Future<String> future : futures) |
1189 |
|
assertSame(TEST_STRING, future.get()); |
1190 |
|
} finally { |
1191 |
|
joinPool(e); |
1198 |
|
public void testTimedInvokeAll6() throws Exception { |
1199 |
|
ExecutorService e = new CustomExecutor(2); |
1200 |
|
try { |
1201 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1201 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1202 |
|
l.add(new StringTask()); |
1203 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1204 |
|
l.add(new StringTask()); |
1205 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1206 |
< |
assertEquals(3, result.size()); |
1207 |
< |
Iterator<Future<String>> it = result.iterator(); |
1205 |
> |
List<Future<String>> futures = |
1206 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1207 |
> |
assertEquals(3, futures.size()); |
1208 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1209 |
|
Future<String> f1 = it.next(); |
1210 |
|
Future<String> f2 = it.next(); |
1211 |
|
Future<String> f3 = it.next(); |