ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
Revision: 1.61
Committed: Mon Feb 22 23:16:06 2016 UTC (8 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.60: +47 -21 lines
Log Message:
make tests more robust against slow VMs; fix for JDK-8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.19 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7 jsr166 1.6 import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 jsr166 1.61 import static java.util.concurrent.TimeUnit.NANOSECONDS;
9 jsr166 1.38 import static java.util.concurrent.TimeUnit.SECONDS;
10 jsr166 1.34
11     import java.util.ArrayList;
12 jsr166 1.38 import java.util.HashSet;
13 jsr166 1.34 import java.util.List;
14     import java.util.concurrent.BlockingQueue;
15     import java.util.concurrent.Callable;
16 jsr166 1.36 import java.util.concurrent.CancellationException;
17 jsr166 1.34 import java.util.concurrent.CountDownLatch;
18     import java.util.concurrent.Delayed;
19     import java.util.concurrent.ExecutionException;
20     import java.util.concurrent.Executors;
21     import java.util.concurrent.ExecutorService;
22     import java.util.concurrent.Future;
23     import java.util.concurrent.RejectedExecutionException;
24     import java.util.concurrent.RejectedExecutionHandler;
25     import java.util.concurrent.RunnableScheduledFuture;
26     import java.util.concurrent.ScheduledFuture;
27     import java.util.concurrent.ScheduledThreadPoolExecutor;
28     import java.util.concurrent.ThreadFactory;
29     import java.util.concurrent.ThreadPoolExecutor;
30     import java.util.concurrent.TimeoutException;
31     import java.util.concurrent.TimeUnit;
32 jsr166 1.61 import java.util.concurrent.atomic.AtomicBoolean;
33 jsr166 1.28 import java.util.concurrent.atomic.AtomicInteger;
34 jsr166 1.61 import java.util.concurrent.atomic.AtomicLong;
35 dl 1.1
36 jsr166 1.34 import junit.framework.Test;
37     import junit.framework.TestSuite;
38    
39 dl 1.1 public class ScheduledExecutorSubclassTest extends JSR166TestCase {
40     public static void main(String[] args) {
41 jsr166 1.35 main(suite(), args);
42 dl 1.1 }
43     public static Test suite() {
44 jsr166 1.7 return new TestSuite(ScheduledExecutorSubclassTest.class);
45 dl 1.1 }
46    
47 jsr166 1.2 static class CustomTask<V> implements RunnableScheduledFuture<V> {
48 dl 1.1 RunnableScheduledFuture<V> task;
49     volatile boolean ran;
50     CustomTask(RunnableScheduledFuture<V> t) { task = t; }
51     public boolean isPeriodic() { return task.isPeriodic(); }
52 jsr166 1.2 public void run() {
53 dl 1.1 ran = true;
54 jsr166 1.2 task.run();
55 dl 1.1 }
56     public long getDelay(TimeUnit unit) { return task.getDelay(unit); }
57     public int compareTo(Delayed t) {
58 jsr166 1.2 return task.compareTo(((CustomTask)t).task);
59 dl 1.1 }
60     public boolean cancel(boolean mayInterruptIfRunning) {
61     return task.cancel(mayInterruptIfRunning);
62     }
63     public boolean isCancelled() { return task.isCancelled(); }
64     public boolean isDone() { return task.isDone(); }
65 jsr166 1.30 public V get() throws InterruptedException, ExecutionException {
66 dl 1.1 V v = task.get();
67     assertTrue(ran);
68     return v;
69     }
70 jsr166 1.30 public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
71 dl 1.1 V v = task.get(time, unit);
72     assertTrue(ran);
73     return v;
74     }
75     }
76 jsr166 1.2
77 dl 1.1 public class CustomExecutor extends ScheduledThreadPoolExecutor {
78    
79     protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
80     return new CustomTask<V>(task);
81     }
82    
83     protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
84     return new CustomTask<V>(task);
85     }
86 jsr166 1.29 CustomExecutor(int corePoolSize) { super(corePoolSize); }
87 dl 1.1 CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
88     super(corePoolSize, handler);
89     }
90    
91     CustomExecutor(int corePoolSize, ThreadFactory threadFactory) {
92     super(corePoolSize, threadFactory);
93     }
94 jsr166 1.2 CustomExecutor(int corePoolSize, ThreadFactory threadFactory,
95 dl 1.1 RejectedExecutionHandler handler) {
96     super(corePoolSize, threadFactory, handler);
97     }
98 jsr166 1.2
99 dl 1.1 }
100 jsr166 1.2
101 dl 1.1 /**
102     * execute successfully executes a runnable
103     */
104 jsr166 1.6 public void testExecute() throws InterruptedException {
105 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
106     try (PoolCleaner cleaner = cleaner(p)) {
107     final CountDownLatch done = new CountDownLatch(1);
108     final Runnable task = new CheckedRunnable() {
109     public void realRun() { done.countDown(); }};
110 jsr166 1.15 p.execute(task);
111 jsr166 1.51 await(done);
112 jsr166 1.15 }
113 dl 1.1 }
114    
115     /**
116     * delayed schedule of callable successfully executes after delay
117     */
118 jsr166 1.6 public void testSchedule1() throws Exception {
119 jsr166 1.51 final CountDownLatch done = new CountDownLatch(1);
120 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
121 jsr166 1.51 try (PoolCleaner cleaner = cleaner(p, done)) {
122 jsr166 1.46 final long startTime = System.nanoTime();
123 jsr166 1.15 Callable task = new CheckedCallable<Boolean>() {
124     public Boolean realCall() {
125     done.countDown();
126 jsr166 1.26 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
127 jsr166 1.15 return Boolean.TRUE;
128     }};
129 jsr166 1.26 Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
130     assertSame(Boolean.TRUE, f.get());
131     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
132 jsr166 1.15 }
133 dl 1.1 }
134    
135     /**
136 jsr166 1.14 * delayed schedule of runnable successfully executes after delay
137 dl 1.1 */
138 jsr166 1.15 public void testSchedule3() throws Exception {
139 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
140     try (PoolCleaner cleaner = cleaner(p)) {
141     final long startTime = System.nanoTime();
142     final CountDownLatch done = new CountDownLatch(1);
143 jsr166 1.15 Runnable task = new CheckedRunnable() {
144     public void realRun() {
145     done.countDown();
146 jsr166 1.26 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
147 jsr166 1.15 }};
148 jsr166 1.26 Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
149     await(done);
150     assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
151     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
152 jsr166 1.15 }
153 dl 1.1 }
154 jsr166 1.2
155 dl 1.1 /**
156     * scheduleAtFixedRate executes runnable after given initial delay
157     */
158 jsr166 1.6 public void testSchedule4() throws InterruptedException {
159 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
160     try (PoolCleaner cleaner = cleaner(p)) {
161     final long startTime = System.nanoTime();
162     final CountDownLatch done = new CountDownLatch(1);
163 jsr166 1.15 Runnable task = new CheckedRunnable() {
164     public void realRun() {
165     done.countDown();
166 jsr166 1.26 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
167 jsr166 1.15 }};
168     ScheduledFuture f =
169 jsr166 1.26 p.scheduleAtFixedRate(task, timeoutMillis(),
170     LONG_DELAY_MS, MILLISECONDS);
171     await(done);
172     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
173 jsr166 1.15 f.cancel(true);
174     }
175 dl 1.1 }
176    
177     /**
178     * scheduleWithFixedDelay executes runnable after given initial delay
179     */
180 jsr166 1.6 public void testSchedule5() throws InterruptedException {
181 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
182     try (PoolCleaner cleaner = cleaner(p)) {
183     final long startTime = System.nanoTime();
184     final CountDownLatch done = new CountDownLatch(1);
185 jsr166 1.15 Runnable task = new CheckedRunnable() {
186     public void realRun() {
187     done.countDown();
188 jsr166 1.26 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
189 jsr166 1.15 }};
190     ScheduledFuture f =
191 jsr166 1.26 p.scheduleWithFixedDelay(task, timeoutMillis(),
192     LONG_DELAY_MS, MILLISECONDS);
193     await(done);
194     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
195 jsr166 1.15 f.cancel(true);
196     }
197     }
198    
199     static class RunnableCounter implements Runnable {
200     AtomicInteger count = new AtomicInteger(0);
201     public void run() { count.getAndIncrement(); }
202 dl 1.1 }
203 jsr166 1.2
204 dl 1.1 /**
205 jsr166 1.61 * scheduleAtFixedRate executes series of tasks at given rate.
206     * Eventually, it must hold that:
207     * cycles - 1 <= elapsedMillis/delay < cycles
208 dl 1.1 */
209 jsr166 1.6 public void testFixedRateSequence() throws InterruptedException {
210 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
211     try (PoolCleaner cleaner = cleaner(p)) {
212 jsr166 1.31 for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
213 jsr166 1.61 final long startTime = System.nanoTime();
214     final int cycles = 8;
215 jsr166 1.31 final CountDownLatch done = new CountDownLatch(cycles);
216 jsr166 1.61 final Runnable task = new CheckedRunnable() {
217 jsr166 1.31 public void realRun() { done.countDown(); }};
218 jsr166 1.61 final ScheduledFuture periodicTask =
219 jsr166 1.31 p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
220 jsr166 1.61 final int totalDelayMillis = (cycles - 1) * delay;
221     await(done, totalDelayMillis + LONG_DELAY_MS);
222 jsr166 1.60 periodicTask.cancel(true);
223 jsr166 1.61 final long elapsedMillis = millisElapsedSince(startTime);
224     assertTrue(elapsedMillis >= totalDelayMillis);
225     if (elapsedMillis <= cycles * delay)
226 jsr166 1.31 return;
227 jsr166 1.61 // else retry with longer delay
228 jsr166 1.31 }
229 jsr166 1.59 fail("unexpected execution rate");
230 jsr166 1.31 }
231 dl 1.1 }
232    
233     /**
234 jsr166 1.61 * scheduleWithFixedDelay executes series of tasks with given period.
235     * Eventually, it must hold that each task starts at least delay and at
236     * most 2 * delay after the termination of the previous task.
237 dl 1.1 */
238 jsr166 1.6 public void testFixedDelaySequence() throws InterruptedException {
239 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
240     try (PoolCleaner cleaner = cleaner(p)) {
241 jsr166 1.31 for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
242 jsr166 1.61 final long startTime = System.nanoTime();
243     final AtomicLong previous = new AtomicLong(startTime);
244     final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
245     final int cycles = 8;
246 jsr166 1.31 final CountDownLatch done = new CountDownLatch(cycles);
247 jsr166 1.61 final int d = delay;
248     final Runnable task = new CheckedRunnable() {
249     public void realRun() {
250     long now = System.nanoTime();
251     long elapsedMillis
252     = NANOSECONDS.toMillis(now - previous.get());
253     if (done.getCount() == cycles) { // first execution
254     if (elapsedMillis >= d)
255     tryLongerDelay.set(true);
256     } else {
257     assertTrue(elapsedMillis >= d);
258     if (elapsedMillis >= 2 * d)
259     tryLongerDelay.set(true);
260     }
261     previous.set(now);
262     done.countDown();
263     }};
264     final ScheduledFuture periodicTask =
265 jsr166 1.31 p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
266 jsr166 1.61 final int totalDelayMillis = (cycles - 1) * delay;
267     await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
268 jsr166 1.60 periodicTask.cancel(true);
269 jsr166 1.61 final long elapsedMillis = millisElapsedSince(startTime);
270     assertTrue(elapsedMillis >= totalDelayMillis);
271     if (!tryLongerDelay.get())
272 jsr166 1.31 return;
273 jsr166 1.61 // else retry with longer delay
274 jsr166 1.31 }
275 jsr166 1.59 fail("unexpected execution rate");
276 jsr166 1.31 }
277 dl 1.1 }
278    
279     /**
280 jsr166 1.12 * execute(null) throws NPE
281 dl 1.1 */
282 jsr166 1.6 public void testExecuteNull() throws InterruptedException {
283 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
284     try (PoolCleaner cleaner = cleaner(p)) {
285     try {
286     p.execute(null);
287     shouldThrow();
288     } catch (NullPointerException success) {}
289     }
290 dl 1.1 }
291    
292     /**
293 jsr166 1.12 * schedule(null) throws NPE
294 dl 1.1 */
295 jsr166 1.6 public void testScheduleNull() throws InterruptedException {
296 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
297     try (PoolCleaner cleaner = cleaner(p)) {
298     try {
299     TrackedCallable callable = null;
300     Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
301     shouldThrow();
302     } catch (NullPointerException success) {}
303     }
304 dl 1.1 }
305 jsr166 1.2
306 dl 1.1 /**
307     * execute throws RejectedExecutionException if shutdown
308     */
309     public void testSchedule1_RejectedExecutionException() {
310 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
311     try (PoolCleaner cleaner = cleaner(p)) {
312     try {
313     p.shutdown();
314     p.schedule(new NoOpRunnable(),
315     MEDIUM_DELAY_MS, MILLISECONDS);
316     shouldThrow();
317     } catch (RejectedExecutionException success) {
318     } catch (SecurityException ok) {}
319 dl 1.1 }
320     }
321    
322     /**
323     * schedule throws RejectedExecutionException if shutdown
324     */
325     public void testSchedule2_RejectedExecutionException() {
326 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
327     try (PoolCleaner cleaner = cleaner(p)) {
328     try {
329     p.shutdown();
330     p.schedule(new NoOpCallable(),
331     MEDIUM_DELAY_MS, MILLISECONDS);
332     shouldThrow();
333     } catch (RejectedExecutionException success) {
334     } catch (SecurityException ok) {}
335 dl 1.1 }
336     }
337    
338     /**
339     * schedule callable throws RejectedExecutionException if shutdown
340     */
341 jsr166 1.20 public void testSchedule3_RejectedExecutionException() {
342 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
343     try (PoolCleaner cleaner = cleaner(p)) {
344     try {
345     p.shutdown();
346     p.schedule(new NoOpCallable(),
347     MEDIUM_DELAY_MS, MILLISECONDS);
348     shouldThrow();
349     } catch (RejectedExecutionException success) {
350     } catch (SecurityException ok) {}
351 jsr166 1.20 }
352 dl 1.1 }
353    
354     /**
355 jsr166 1.14 * scheduleAtFixedRate throws RejectedExecutionException if shutdown
356 dl 1.1 */
357     public void testScheduleAtFixedRate1_RejectedExecutionException() {
358 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
359     try (PoolCleaner cleaner = cleaner(p)) {
360     try {
361     p.shutdown();
362     p.scheduleAtFixedRate(new NoOpRunnable(),
363     MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
364     shouldThrow();
365     } catch (RejectedExecutionException success) {
366     } catch (SecurityException ok) {}
367 jsr166 1.2 }
368 dl 1.1 }
369 jsr166 1.2
370 dl 1.1 /**
371     * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
372     */
373     public void testScheduleWithFixedDelay1_RejectedExecutionException() {
374 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
375     try (PoolCleaner cleaner = cleaner(p)) {
376     try {
377     p.shutdown();
378     p.scheduleWithFixedDelay(new NoOpRunnable(),
379     MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
380     shouldThrow();
381     } catch (RejectedExecutionException success) {
382     } catch (SecurityException ok) {}
383 jsr166 1.2 }
384 dl 1.1 }
385    
386     /**
387 jsr166 1.14 * getActiveCount increases but doesn't overestimate, when a
388     * thread becomes active
389 dl 1.1 */
390 jsr166 1.6 public void testGetActiveCount() throws InterruptedException {
391 jsr166 1.51 final CountDownLatch done = new CountDownLatch(1);
392 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(2);
393 jsr166 1.51 try (PoolCleaner cleaner = cleaner(p, done)) {
394 jsr166 1.46 final CountDownLatch threadStarted = new CountDownLatch(1);
395 jsr166 1.15 assertEquals(0, p.getActiveCount());
396     p.execute(new CheckedRunnable() {
397     public void realRun() throws InterruptedException {
398     threadStarted.countDown();
399     assertEquals(1, p.getActiveCount());
400 jsr166 1.51 await(done);
401 jsr166 1.15 }});
402 jsr166 1.51 await(threadStarted);
403 jsr166 1.15 assertEquals(1, p.getActiveCount());
404     }
405 dl 1.1 }
406 jsr166 1.2
407 dl 1.1 /**
408 jsr166 1.14 * getCompletedTaskCount increases, but doesn't overestimate,
409     * when tasks complete
410 dl 1.1 */
411 jsr166 1.9 public void testGetCompletedTaskCount() throws InterruptedException {
412 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(2);
413 jsr166 1.46 try (PoolCleaner cleaner = cleaner(p)) {
414     final CountDownLatch threadStarted = new CountDownLatch(1);
415     final CountDownLatch threadProceed = new CountDownLatch(1);
416     final CountDownLatch threadDone = new CountDownLatch(1);
417 jsr166 1.15 assertEquals(0, p.getCompletedTaskCount());
418     p.execute(new CheckedRunnable() {
419     public void realRun() throws InterruptedException {
420     threadStarted.countDown();
421     assertEquals(0, p.getCompletedTaskCount());
422     threadProceed.await();
423     threadDone.countDown();
424     }});
425 jsr166 1.27 await(threadStarted);
426 jsr166 1.15 assertEquals(0, p.getCompletedTaskCount());
427     threadProceed.countDown();
428     threadDone.await();
429 jsr166 1.26 long startTime = System.nanoTime();
430     while (p.getCompletedTaskCount() != 1) {
431     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
432     fail("timed out");
433     Thread.yield();
434     }
435 jsr166 1.15 }
436 dl 1.1 }
437 jsr166 1.2
438 dl 1.1 /**
439 jsr166 1.14 * getCorePoolSize returns size given in constructor if not otherwise set
440 dl 1.1 */
441     public void testGetCorePoolSize() {
442 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
443     try (PoolCleaner cleaner = cleaner(p)) {
444     assertEquals(1, p.getCorePoolSize());
445     }
446 dl 1.1 }
447 jsr166 1.2
448 dl 1.1 /**
449 jsr166 1.14 * getLargestPoolSize increases, but doesn't overestimate, when
450     * multiple threads active
451 dl 1.1 */
452 jsr166 1.6 public void testGetLargestPoolSize() throws InterruptedException {
453 jsr166 1.15 final int THREADS = 3;
454 jsr166 1.51 final CountDownLatch done = new CountDownLatch(1);
455 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(THREADS);
456 jsr166 1.51 try (PoolCleaner cleaner = cleaner(p, done)) {
457 jsr166 1.46 final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
458 jsr166 1.15 assertEquals(0, p.getLargestPoolSize());
459     for (int i = 0; i < THREADS; i++)
460     p.execute(new CheckedRunnable() {
461     public void realRun() throws InterruptedException {
462     threadsStarted.countDown();
463 jsr166 1.51 await(done);
464 jsr166 1.15 assertEquals(THREADS, p.getLargestPoolSize());
465     }});
466 jsr166 1.53 await(threadsStarted);
467 jsr166 1.15 assertEquals(THREADS, p.getLargestPoolSize());
468     }
469 jsr166 1.46 assertEquals(THREADS, p.getLargestPoolSize());
470 dl 1.1 }
471 jsr166 1.2
472 dl 1.1 /**
473 jsr166 1.14 * getPoolSize increases, but doesn't overestimate, when threads
474     * become active
475 dl 1.1 */
476 jsr166 1.15 public void testGetPoolSize() throws InterruptedException {
477 jsr166 1.51 final CountDownLatch done = new CountDownLatch(1);
478 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(1);
479 jsr166 1.51 try (PoolCleaner cleaner = cleaner(p, done)) {
480 jsr166 1.46 final CountDownLatch threadStarted = new CountDownLatch(1);
481 jsr166 1.15 assertEquals(0, p.getPoolSize());
482     p.execute(new CheckedRunnable() {
483     public void realRun() throws InterruptedException {
484     threadStarted.countDown();
485     assertEquals(1, p.getPoolSize());
486 jsr166 1.51 await(done);
487 jsr166 1.15 }});
488 jsr166 1.51 await(threadStarted);
489 jsr166 1.15 assertEquals(1, p.getPoolSize());
490     }
491 dl 1.1 }
492 jsr166 1.2
493 dl 1.1 /**
494 jsr166 1.14 * getTaskCount increases, but doesn't overestimate, when tasks
495     * submitted
496 dl 1.1 */
497 jsr166 1.6 public void testGetTaskCount() throws InterruptedException {
498 jsr166 1.48 final int TASKS = 3;
499     final CountDownLatch done = new CountDownLatch(1);
500 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(1);
501 jsr166 1.48 try (PoolCleaner cleaner = cleaner(p, done)) {
502 jsr166 1.46 final CountDownLatch threadStarted = new CountDownLatch(1);
503 jsr166 1.15 assertEquals(0, p.getTaskCount());
504 jsr166 1.48 assertEquals(0, p.getCompletedTaskCount());
505     p.execute(new CheckedRunnable() {
506     public void realRun() throws InterruptedException {
507     threadStarted.countDown();
508 jsr166 1.51 await(done);
509 jsr166 1.48 }});
510 jsr166 1.51 await(threadStarted);
511 jsr166 1.48 assertEquals(1, p.getTaskCount());
512     assertEquals(0, p.getCompletedTaskCount());
513     for (int i = 0; i < TASKS; i++) {
514     assertEquals(1 + i, p.getTaskCount());
515 jsr166 1.15 p.execute(new CheckedRunnable() {
516     public void realRun() throws InterruptedException {
517     threadStarted.countDown();
518 jsr166 1.48 assertEquals(1 + TASKS, p.getTaskCount());
519 jsr166 1.51 await(done);
520 jsr166 1.15 }});
521 jsr166 1.48 }
522     assertEquals(1 + TASKS, p.getTaskCount());
523     assertEquals(0, p.getCompletedTaskCount());
524 jsr166 1.15 }
525 jsr166 1.48 assertEquals(1 + TASKS, p.getTaskCount());
526     assertEquals(1 + TASKS, p.getCompletedTaskCount());
527 dl 1.1 }
528    
529 jsr166 1.2 /**
530 dl 1.1 * getThreadFactory returns factory in constructor if not set
531     */
532     public void testGetThreadFactory() {
533 jsr166 1.46 final ThreadFactory threadFactory = new SimpleThreadFactory();
534     final CustomExecutor p = new CustomExecutor(1, threadFactory);
535     try (PoolCleaner cleaner = cleaner(p)) {
536     assertSame(threadFactory, p.getThreadFactory());
537     }
538 dl 1.1 }
539    
540 jsr166 1.2 /**
541 dl 1.1 * setThreadFactory sets the thread factory returned by getThreadFactory
542     */
543     public void testSetThreadFactory() {
544 jsr166 1.46 final ThreadFactory threadFactory = new SimpleThreadFactory();
545     final CustomExecutor p = new CustomExecutor(1);
546     try (PoolCleaner cleaner = cleaner(p)) {
547     p.setThreadFactory(threadFactory);
548     assertSame(threadFactory, p.getThreadFactory());
549     }
550 dl 1.1 }
551    
552 jsr166 1.2 /**
553 dl 1.1 * setThreadFactory(null) throws NPE
554     */
555     public void testSetThreadFactoryNull() {
556 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
557     try (PoolCleaner cleaner = cleaner(p)) {
558     try {
559     p.setThreadFactory(null);
560     shouldThrow();
561     } catch (NullPointerException success) {}
562 dl 1.1 }
563     }
564 jsr166 1.2
565 dl 1.1 /**
566 jsr166 1.23 * isShutdown is false before shutdown, true after
567 dl 1.1 */
568     public void testIsShutdown() {
569 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
570     try (PoolCleaner cleaner = cleaner(p)) {
571 jsr166 1.15 assertFalse(p.isShutdown());
572     try { p.shutdown(); } catch (SecurityException ok) { return; }
573 jsr166 1.46 assertTrue(p.isShutdown());
574 dl 1.1 }
575     }
576    
577     /**
578 jsr166 1.14 * isTerminated is false before termination, true after
579 dl 1.1 */
580 jsr166 1.6 public void testIsTerminated() throws InterruptedException {
581 jsr166 1.51 final CountDownLatch done = new CountDownLatch(1);
582 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(1);
583 jsr166 1.46 try (PoolCleaner cleaner = cleaner(p)) {
584     final CountDownLatch threadStarted = new CountDownLatch(1);
585 jsr166 1.15 p.execute(new CheckedRunnable() {
586     public void realRun() throws InterruptedException {
587 jsr166 1.18 assertFalse(p.isTerminated());
588 jsr166 1.15 threadStarted.countDown();
589 jsr166 1.51 await(done);
590 jsr166 1.15 }});
591 jsr166 1.51 await(threadStarted);
592     assertFalse(p.isTerminated());
593 jsr166 1.18 assertFalse(p.isTerminating());
594 jsr166 1.15 done.countDown();
595     try { p.shutdown(); } catch (SecurityException ok) { return; }
596 jsr166 1.46 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
597     assertTrue(p.isTerminated());
598 dl 1.1 }
599     }
600    
601     /**
602 jsr166 1.14 * isTerminating is not true when running or when terminated
603 dl 1.1 */
604 jsr166 1.6 public void testIsTerminating() throws InterruptedException {
605 jsr166 1.51 final CountDownLatch done = new CountDownLatch(1);
606 jsr166 1.15 final ThreadPoolExecutor p = new CustomExecutor(1);
607 jsr166 1.46 try (PoolCleaner cleaner = cleaner(p)) {
608     final CountDownLatch threadStarted = new CountDownLatch(1);
609 jsr166 1.15 assertFalse(p.isTerminating());
610     p.execute(new CheckedRunnable() {
611     public void realRun() throws InterruptedException {
612 jsr166 1.17 assertFalse(p.isTerminating());
613 jsr166 1.15 threadStarted.countDown();
614 jsr166 1.51 await(done);
615 jsr166 1.15 }});
616 jsr166 1.54 await(threadStarted);
617 jsr166 1.15 assertFalse(p.isTerminating());
618     done.countDown();
619     try { p.shutdown(); } catch (SecurityException ok) { return; }
620 jsr166 1.46 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
621     assertTrue(p.isTerminated());
622     assertFalse(p.isTerminating());
623 jsr166 1.15 }
624 dl 1.1 }
625    
626     /**
627     * getQueue returns the work queue, which contains queued tasks
628     */
629 jsr166 1.6 public void testGetQueue() throws InterruptedException {
630 jsr166 1.55 final CountDownLatch done = new CountDownLatch(1);
631 jsr166 1.46 final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
632 jsr166 1.55 try (PoolCleaner cleaner = cleaner(p, done)) {
633 jsr166 1.46 final CountDownLatch threadStarted = new CountDownLatch(1);
634 jsr166 1.15 ScheduledFuture[] tasks = new ScheduledFuture[5];
635     for (int i = 0; i < tasks.length; i++) {
636     Runnable r = new CheckedRunnable() {
637     public void realRun() throws InterruptedException {
638     threadStarted.countDown();
639 jsr166 1.52 await(done);
640 jsr166 1.15 }};
641     tasks[i] = p.schedule(r, 1, MILLISECONDS);
642     }
643 jsr166 1.54 await(threadStarted);
644 jsr166 1.15 BlockingQueue<Runnable> q = p.getQueue();
645     assertTrue(q.contains(tasks[tasks.length - 1]));
646 dl 1.1 assertFalse(q.contains(tasks[0]));
647     }
648     }
649    
650     /**
651     * remove(task) removes queued task, and fails to remove active task
652     */
653 jsr166 1.6 public void testRemove() throws InterruptedException {
654 jsr166 1.55 final CountDownLatch done = new CountDownLatch(1);
655 jsr166 1.15 final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
656 jsr166 1.55 try (PoolCleaner cleaner = cleaner(p, done)) {
657 jsr166 1.46 ScheduledFuture[] tasks = new ScheduledFuture[5];
658     final CountDownLatch threadStarted = new CountDownLatch(1);
659 jsr166 1.15 for (int i = 0; i < tasks.length; i++) {
660     Runnable r = new CheckedRunnable() {
661     public void realRun() throws InterruptedException {
662     threadStarted.countDown();
663 jsr166 1.52 await(done);
664 jsr166 1.15 }};
665     tasks[i] = p.schedule(r, 1, MILLISECONDS);
666     }
667 jsr166 1.54 await(threadStarted);
668 jsr166 1.15 BlockingQueue<Runnable> q = p.getQueue();
669     assertFalse(p.remove((Runnable)tasks[0]));
670 dl 1.1 assertTrue(q.contains((Runnable)tasks[4]));
671     assertTrue(q.contains((Runnable)tasks[3]));
672 jsr166 1.15 assertTrue(p.remove((Runnable)tasks[4]));
673     assertFalse(p.remove((Runnable)tasks[4]));
674 dl 1.1 assertFalse(q.contains((Runnable)tasks[4]));
675     assertTrue(q.contains((Runnable)tasks[3]));
676 jsr166 1.15 assertTrue(p.remove((Runnable)tasks[3]));
677 dl 1.1 assertFalse(q.contains((Runnable)tasks[3]));
678     }
679     }
680    
681     /**
682 jsr166 1.14 * purge removes cancelled tasks from the queue
683 dl 1.1 */
684 jsr166 1.6 public void testPurge() throws InterruptedException {
685 jsr166 1.49 final ScheduledFuture[] tasks = new ScheduledFuture[5];
686     final Runnable releaser = new Runnable() { public void run() {
687     for (ScheduledFuture task : tasks)
688     if (task != null) task.cancel(true); }};
689 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
690 jsr166 1.49 try (PoolCleaner cleaner = cleaner(p, releaser)) {
691     for (int i = 0; i < tasks.length; i++)
692     tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
693     LONG_DELAY_MS, MILLISECONDS);
694 jsr166 1.15 int max = tasks.length;
695 dl 1.1 if (tasks[4].cancel(true)) --max;
696     if (tasks[3].cancel(true)) --max;
697     // There must eventually be an interference-free point at
698     // which purge will not fail. (At worst, when queue is empty.)
699 jsr166 1.22 long startTime = System.nanoTime();
700     do {
701 jsr166 1.15 p.purge();
702     long count = p.getTaskCount();
703 jsr166 1.22 if (count == max)
704     return;
705 jsr166 1.49 } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
706 jsr166 1.22 fail("Purge failed to remove cancelled tasks");
707 dl 1.1 }
708     }
709    
710     /**
711 jsr166 1.37 * shutdownNow returns a list containing tasks that were not run,
712     * and those tasks are drained from the queue
713 dl 1.1 */
714 jsr166 1.40 public void testShutdownNow() throws InterruptedException {
715     final int poolSize = 2;
716     final int count = 5;
717     final AtomicInteger ran = new AtomicInteger(0);
718     final CustomExecutor p = new CustomExecutor(poolSize);
719 jsr166 1.47 final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
720 jsr166 1.42 Runnable waiter = new CheckedRunnable() { public void realRun() {
721 jsr166 1.40 threadsStarted.countDown();
722     try {
723     MILLISECONDS.sleep(2 * LONG_DELAY_MS);
724     } catch (InterruptedException success) {}
725     ran.getAndIncrement();
726     }};
727     for (int i = 0; i < count; i++)
728     p.execute(waiter);
729 jsr166 1.53 await(threadsStarted);
730 jsr166 1.41 assertEquals(poolSize, p.getActiveCount());
731     assertEquals(0, p.getCompletedTaskCount());
732 jsr166 1.40 final List<Runnable> queuedTasks;
733     try {
734     queuedTasks = p.shutdownNow();
735     } catch (SecurityException ok) {
736     return; // Allowed in case test doesn't have privs
737     }
738     assertTrue(p.isShutdown());
739     assertTrue(p.getQueue().isEmpty());
740     assertEquals(count - poolSize, queuedTasks.size());
741     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
742     assertTrue(p.isTerminated());
743     assertEquals(poolSize, ran.get());
744 jsr166 1.41 assertEquals(poolSize, p.getCompletedTaskCount());
745 jsr166 1.40 }
746    
747     /**
748     * shutdownNow returns a list containing tasks that were not run,
749     * and those tasks are drained from the queue
750     */
751 jsr166 1.38 public void testShutdownNow_delayedTasks() throws InterruptedException {
752 jsr166 1.46 final CustomExecutor p = new CustomExecutor(1);
753 jsr166 1.38 List<ScheduledFuture> tasks = new ArrayList<>();
754     for (int i = 0; i < 3; i++) {
755     Runnable r = new NoOpRunnable();
756     tasks.add(p.schedule(r, 9, SECONDS));
757     tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
758     tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
759     }
760 jsr166 1.39 if (testImplementationDetails)
761     assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
762 jsr166 1.38 final List<Runnable> queuedTasks;
763 dl 1.1 try {
764 jsr166 1.38 queuedTasks = p.shutdownNow();
765 jsr166 1.2 } catch (SecurityException ok) {
766 jsr166 1.38 return; // Allowed in case test doesn't have privs
767     }
768     assertTrue(p.isShutdown());
769     assertTrue(p.getQueue().isEmpty());
770 jsr166 1.39 if (testImplementationDetails)
771     assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
772 jsr166 1.38 assertEquals(tasks.size(), queuedTasks.size());
773     for (ScheduledFuture task : tasks) {
774     assertFalse(((CustomTask)task).ran);
775     assertFalse(task.isDone());
776     assertFalse(task.isCancelled());
777 dl 1.1 }
778 jsr166 1.38 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
779     assertTrue(p.isTerminated());
780 dl 1.1 }
781    
782     /**
783 jsr166 1.42 * By default, periodic tasks are cancelled at shutdown.
784     * By default, delayed tasks keep running after shutdown.
785     * Check that changing the default values work:
786     * - setExecuteExistingDelayedTasksAfterShutdownPolicy
787     * - setContinueExistingPeriodicTasksAfterShutdownPolicy
788     */
789     public void testShutdown_cancellation() throws Exception {
790     Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE };
791     for (Boolean policy : allBooleans)
792     {
793     final int poolSize = 2;
794     final CustomExecutor p = new CustomExecutor(poolSize);
795     final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE);
796     final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE);
797     final boolean effectiveRemovePolicy = (policy == Boolean.TRUE);
798     if (policy != null) {
799     p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy);
800     p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy);
801     p.setRemoveOnCancelPolicy(policy);
802     }
803     assertEquals(effectiveDelayedPolicy,
804     p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
805     assertEquals(effectivePeriodicPolicy,
806     p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
807     assertEquals(effectiveRemovePolicy,
808     p.getRemoveOnCancelPolicy());
809     // Strategy: Wedge the pool with poolSize "blocker" threads
810     final AtomicInteger ran = new AtomicInteger(0);
811     final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
812     final CountDownLatch unblock = new CountDownLatch(1);
813     final CountDownLatch periodicLatch1 = new CountDownLatch(2);
814     final CountDownLatch periodicLatch2 = new CountDownLatch(2);
815     Runnable task = new CheckedRunnable() { public void realRun()
816     throws InterruptedException {
817     poolBlocked.countDown();
818     assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS));
819     ran.getAndIncrement();
820     }};
821     List<Future<?>> blockers = new ArrayList<>();
822     List<Future<?>> periodics = new ArrayList<>();
823     List<Future<?>> delayeds = new ArrayList<>();
824     for (int i = 0; i < poolSize; i++)
825     blockers.add(p.submit(task));
826     assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS));
827    
828     periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1),
829     1, 1, MILLISECONDS));
830     periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2),
831     1, 1, MILLISECONDS));
832     delayeds.add(p.schedule(task, 1, MILLISECONDS));
833 jsr166 1.2
834 jsr166 1.42 assertTrue(p.getQueue().containsAll(periodics));
835     assertTrue(p.getQueue().containsAll(delayeds));
836 jsr166 1.15 try { p.shutdown(); } catch (SecurityException ok) { return; }
837 jsr166 1.42 assertTrue(p.isShutdown());
838     assertFalse(p.isTerminated());
839     for (Future<?> periodic : periodics) {
840     assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled());
841     assertTrue(effectivePeriodicPolicy ^ periodic.isDone());
842     }
843     for (Future<?> delayed : delayeds) {
844     assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled());
845     assertTrue(effectiveDelayedPolicy ^ delayed.isDone());
846     }
847     if (testImplementationDetails) {
848     assertEquals(effectivePeriodicPolicy,
849     p.getQueue().containsAll(periodics));
850     assertEquals(effectiveDelayedPolicy,
851     p.getQueue().containsAll(delayeds));
852     }
853     // Release all pool threads
854     unblock.countDown();
855    
856     for (Future<?> delayed : delayeds) {
857     if (effectiveDelayedPolicy) {
858     assertNull(delayed.get());
859     }
860 jsr166 1.6 }
861 jsr166 1.42 if (effectivePeriodicPolicy) {
862     assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS));
863     assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS));
864     for (Future<?> periodic : periodics) {
865     assertTrue(periodic.cancel(false));
866     assertTrue(periodic.isCancelled());
867     assertTrue(periodic.isDone());
868     }
869 dl 1.1 }
870 jsr166 1.42 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
871 jsr166 1.15 assertTrue(p.isTerminated());
872 jsr166 1.42 assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
873     }}
874 dl 1.1
875     /**
876     * completed submit of callable returns result
877     */
878 jsr166 1.6 public void testSubmitCallable() throws Exception {
879 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
880     try (PoolCleaner cleaner = cleaner(e)) {
881 dl 1.1 Future<String> future = e.submit(new StringTask());
882     String result = future.get();
883     assertSame(TEST_STRING, result);
884     }
885     }
886    
887     /**
888     * completed submit of runnable returns successfully
889     */
890 jsr166 1.6 public void testSubmitRunnable() throws Exception {
891 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
892     try (PoolCleaner cleaner = cleaner(e)) {
893 dl 1.1 Future<?> future = e.submit(new NoOpRunnable());
894     future.get();
895     assertTrue(future.isDone());
896     }
897     }
898    
899     /**
900     * completed submit of (runnable, result) returns result
901     */
902 jsr166 1.6 public void testSubmitRunnable2() throws Exception {
903 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
904     try (PoolCleaner cleaner = cleaner(e)) {
905 dl 1.1 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
906     String result = future.get();
907     assertSame(TEST_STRING, result);
908     }
909     }
910    
911     /**
912     * invokeAny(null) throws NPE
913     */
914 jsr166 1.6 public void testInvokeAny1() throws Exception {
915 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
916     try (PoolCleaner cleaner = cleaner(e)) {
917     try {
918     e.invokeAny(null);
919     shouldThrow();
920     } catch (NullPointerException success) {}
921 dl 1.1 }
922     }
923    
924     /**
925     * invokeAny(empty collection) throws IAE
926     */
927 jsr166 1.6 public void testInvokeAny2() throws Exception {
928 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
929     try (PoolCleaner cleaner = cleaner(e)) {
930     try {
931     e.invokeAny(new ArrayList<Callable<String>>());
932     shouldThrow();
933     } catch (IllegalArgumentException success) {}
934 dl 1.1 }
935     }
936    
937     /**
938     * invokeAny(c) throws NPE if c has null elements
939     */
940 jsr166 1.6 public void testInvokeAny3() throws Exception {
941 jsr166 1.46 final CountDownLatch latch = new CountDownLatch(1);
942     final ExecutorService e = new CustomExecutor(2);
943     try (PoolCleaner cleaner = cleaner(e)) {
944     List<Callable<String>> l = new ArrayList<Callable<String>>();
945     l.add(latchAwaitingStringTask(latch));
946     l.add(null);
947     try {
948     e.invokeAny(l);
949     shouldThrow();
950     } catch (NullPointerException success) {}
951 jsr166 1.6 latch.countDown();
952 dl 1.1 }
953     }
954    
955     /**
956     * invokeAny(c) throws ExecutionException if no task completes
957     */
958 jsr166 1.6 public void testInvokeAny4() throws Exception {
959 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
960     try (PoolCleaner cleaner = cleaner(e)) {
961     List<Callable<String>> l = new ArrayList<Callable<String>>();
962     l.add(new NPETask());
963     try {
964     e.invokeAny(l);
965     shouldThrow();
966     } catch (ExecutionException success) {
967     assertTrue(success.getCause() instanceof NullPointerException);
968     }
969 dl 1.1 }
970     }
971    
972     /**
973     * invokeAny(c) returns result of some task
974     */
975 jsr166 1.6 public void testInvokeAny5() throws Exception {
976 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
977     try (PoolCleaner cleaner = cleaner(e)) {
978 jsr166 1.11 List<Callable<String>> l = new ArrayList<Callable<String>>();
979 dl 1.1 l.add(new StringTask());
980     l.add(new StringTask());
981     String result = e.invokeAny(l);
982     assertSame(TEST_STRING, result);
983     }
984     }
985    
986     /**
987     * invokeAll(null) throws NPE
988     */
989 jsr166 1.6 public void testInvokeAll1() throws Exception {
990 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
991     try (PoolCleaner cleaner = cleaner(e)) {
992     try {
993     e.invokeAll(null);
994     shouldThrow();
995     } catch (NullPointerException success) {}
996 dl 1.1 }
997     }
998    
999     /**
1000     * invokeAll(empty collection) returns empty collection
1001     */
1002 jsr166 1.6 public void testInvokeAll2() throws Exception {
1003 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1004     try (PoolCleaner cleaner = cleaner(e)) {
1005 dl 1.1 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1006     assertTrue(r.isEmpty());
1007     }
1008     }
1009    
1010     /**
1011     * invokeAll(c) throws NPE if c has null elements
1012     */
1013 jsr166 1.6 public void testInvokeAll3() throws Exception {
1014 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1015     try (PoolCleaner cleaner = cleaner(e)) {
1016     List<Callable<String>> l = new ArrayList<Callable<String>>();
1017     l.add(new StringTask());
1018     l.add(null);
1019     try {
1020     e.invokeAll(l);
1021     shouldThrow();
1022     } catch (NullPointerException success) {}
1023 dl 1.1 }
1024     }
1025    
1026     /**
1027     * get of invokeAll(c) throws exception on failed task
1028     */
1029 jsr166 1.6 public void testInvokeAll4() throws Exception {
1030 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1031     try (PoolCleaner cleaner = cleaner(e)) {
1032     List<Callable<String>> l = new ArrayList<Callable<String>>();
1033     l.add(new NPETask());
1034     List<Future<String>> futures = e.invokeAll(l);
1035     assertEquals(1, futures.size());
1036     try {
1037     futures.get(0).get();
1038     shouldThrow();
1039     } catch (ExecutionException success) {
1040     assertTrue(success.getCause() instanceof NullPointerException);
1041     }
1042 dl 1.1 }
1043     }
1044    
1045     /**
1046     * invokeAll(c) returns results of all completed tasks
1047     */
1048 jsr166 1.6 public void testInvokeAll5() throws Exception {
1049 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1050     try (PoolCleaner cleaner = cleaner(e)) {
1051 jsr166 1.11 List<Callable<String>> l = new ArrayList<Callable<String>>();
1052 dl 1.1 l.add(new StringTask());
1053     l.add(new StringTask());
1054 jsr166 1.11 List<Future<String>> futures = e.invokeAll(l);
1055     assertEquals(2, futures.size());
1056     for (Future<String> future : futures)
1057 jsr166 1.6 assertSame(TEST_STRING, future.get());
1058 dl 1.1 }
1059     }
1060    
1061     /**
1062     * timed invokeAny(null) throws NPE
1063     */
1064 jsr166 1.6 public void testTimedInvokeAny1() throws Exception {
1065 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1066     try (PoolCleaner cleaner = cleaner(e)) {
1067     try {
1068     e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1069     shouldThrow();
1070     } catch (NullPointerException success) {}
1071 dl 1.1 }
1072     }
1073    
1074     /**
1075     * timed invokeAny(,,null) throws NPE
1076     */
1077 jsr166 1.6 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1078 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1079     try (PoolCleaner cleaner = cleaner(e)) {
1080     List<Callable<String>> l = new ArrayList<Callable<String>>();
1081     l.add(new StringTask());
1082     try {
1083     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1084     shouldThrow();
1085     } catch (NullPointerException success) {}
1086 dl 1.1 }
1087     }
1088    
1089     /**
1090     * timed invokeAny(empty collection) throws IAE
1091     */
1092 jsr166 1.6 public void testTimedInvokeAny2() throws Exception {
1093 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1094     try (PoolCleaner cleaner = cleaner(e)) {
1095     try {
1096     e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1097     shouldThrow();
1098     } catch (IllegalArgumentException success) {}
1099 dl 1.1 }
1100     }
1101    
1102     /**
1103     * timed invokeAny(c) throws NPE if c has null elements
1104     */
1105 jsr166 1.6 public void testTimedInvokeAny3() throws Exception {
1106 jsr166 1.11 CountDownLatch latch = new CountDownLatch(1);
1107 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1108     try (PoolCleaner cleaner = cleaner(e)) {
1109     List<Callable<String>> l = new ArrayList<Callable<String>>();
1110     l.add(latchAwaitingStringTask(latch));
1111     l.add(null);
1112     try {
1113     e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1114     shouldThrow();
1115     } catch (NullPointerException success) {}
1116 jsr166 1.6 latch.countDown();
1117 dl 1.1 }
1118     }
1119    
1120     /**
1121     * timed invokeAny(c) throws ExecutionException if no task completes
1122     */
1123 jsr166 1.6 public void testTimedInvokeAny4() throws Exception {
1124 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1125     try (PoolCleaner cleaner = cleaner(e)) {
1126 jsr166 1.57 long startTime = System.nanoTime();
1127 jsr166 1.46 List<Callable<String>> l = new ArrayList<Callable<String>>();
1128     l.add(new NPETask());
1129     try {
1130 jsr166 1.57 e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1131 jsr166 1.46 shouldThrow();
1132     } catch (ExecutionException success) {
1133     assertTrue(success.getCause() instanceof NullPointerException);
1134     }
1135 jsr166 1.57 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1136 dl 1.1 }
1137     }
1138    
1139     /**
1140     * timed invokeAny(c) returns result of some task
1141     */
1142 jsr166 1.6 public void testTimedInvokeAny5() throws Exception {
1143 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1144     try (PoolCleaner cleaner = cleaner(e)) {
1145 jsr166 1.58 long startTime = System.nanoTime();
1146 jsr166 1.11 List<Callable<String>> l = new ArrayList<Callable<String>>();
1147 dl 1.1 l.add(new StringTask());
1148     l.add(new StringTask());
1149 jsr166 1.58 String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1150 dl 1.1 assertSame(TEST_STRING, result);
1151 jsr166 1.58 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1152 dl 1.1 }
1153     }
1154    
1155     /**
1156     * timed invokeAll(null) throws NPE
1157     */
1158 jsr166 1.6 public void testTimedInvokeAll1() throws Exception {
1159 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1160     try (PoolCleaner cleaner = cleaner(e)) {
1161     try {
1162     e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1163     shouldThrow();
1164     } catch (NullPointerException success) {}
1165 dl 1.1 }
1166     }
1167    
1168     /**
1169     * timed invokeAll(,,null) throws NPE
1170     */
1171 jsr166 1.6 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1172 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1173     try (PoolCleaner cleaner = cleaner(e)) {
1174     List<Callable<String>> l = new ArrayList<Callable<String>>();
1175     l.add(new StringTask());
1176     try {
1177     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1178     shouldThrow();
1179     } catch (NullPointerException success) {}
1180 dl 1.1 }
1181     }
1182    
1183     /**
1184     * timed invokeAll(empty collection) returns empty collection
1185     */
1186 jsr166 1.6 public void testTimedInvokeAll2() throws Exception {
1187 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1188     try (PoolCleaner cleaner = cleaner(e)) {
1189 jsr166 1.6 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1190 dl 1.1 assertTrue(r.isEmpty());
1191     }
1192     }
1193    
1194     /**
1195     * timed invokeAll(c) throws NPE if c has null elements
1196     */
1197 jsr166 1.6 public void testTimedInvokeAll3() throws Exception {
1198 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1199     try (PoolCleaner cleaner = cleaner(e)) {
1200     List<Callable<String>> l = new ArrayList<Callable<String>>();
1201     l.add(new StringTask());
1202     l.add(null);
1203     try {
1204     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1205     shouldThrow();
1206     } catch (NullPointerException success) {}
1207 dl 1.1 }
1208     }
1209    
1210     /**
1211     * get of element of invokeAll(c) throws exception on failed task
1212     */
1213 jsr166 1.6 public void testTimedInvokeAll4() throws Exception {
1214 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1215     try (PoolCleaner cleaner = cleaner(e)) {
1216     List<Callable<String>> l = new ArrayList<Callable<String>>();
1217     l.add(new NPETask());
1218     List<Future<String>> futures =
1219     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1220     assertEquals(1, futures.size());
1221     try {
1222     futures.get(0).get();
1223     shouldThrow();
1224     } catch (ExecutionException success) {
1225     assertTrue(success.getCause() instanceof NullPointerException);
1226     }
1227 dl 1.1 }
1228     }
1229    
1230     /**
1231     * timed invokeAll(c) returns results of all completed tasks
1232     */
1233 jsr166 1.6 public void testTimedInvokeAll5() throws Exception {
1234 jsr166 1.46 final ExecutorService e = new CustomExecutor(2);
1235     try (PoolCleaner cleaner = cleaner(e)) {
1236 jsr166 1.11 List<Callable<String>> l = new ArrayList<Callable<String>>();
1237 dl 1.1 l.add(new StringTask());
1238     l.add(new StringTask());
1239 jsr166 1.11 List<Future<String>> futures =
1240 jsr166 1.43 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1241 jsr166 1.11 assertEquals(2, futures.size());
1242     for (Future<String> future : futures)
1243 jsr166 1.6 assertSame(TEST_STRING, future.get());
1244 dl 1.1 }
1245     }
1246    
1247     /**
1248     * timed invokeAll(c) cancels tasks not completed by timeout
1249     */
1250 jsr166 1.6 public void testTimedInvokeAll6() throws Exception {
1251 jsr166 1.56 for (long timeout = timeoutMillis();;) {
1252     final CountDownLatch done = new CountDownLatch(1);
1253     final Callable<String> waiter = new CheckedCallable<String>() {
1254     public String realCall() {
1255     try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1256     catch (InterruptedException ok) {}
1257     return "1"; }};
1258     final ExecutorService p = new CustomExecutor(2);
1259     try (PoolCleaner cleaner = cleaner(p, done)) {
1260 jsr166 1.36 List<Callable<String>> tasks = new ArrayList<>();
1261     tasks.add(new StringTask("0"));
1262 jsr166 1.56 tasks.add(waiter);
1263 jsr166 1.36 tasks.add(new StringTask("2"));
1264     long startTime = System.nanoTime();
1265     List<Future<String>> futures =
1266 jsr166 1.56 p.invokeAll(tasks, timeout, MILLISECONDS);
1267 jsr166 1.36 assertEquals(tasks.size(), futures.size());
1268     assertTrue(millisElapsedSince(startTime) >= timeout);
1269     for (Future future : futures)
1270     assertTrue(future.isDone());
1271     assertTrue(futures.get(1).isCancelled());
1272     try {
1273     assertEquals("0", futures.get(0).get());
1274     assertEquals("2", futures.get(2).get());
1275     break;
1276     } catch (CancellationException retryWithLongerTimeout) {
1277     timeout *= 2;
1278     if (timeout >= LONG_DELAY_MS / 2)
1279     fail("expected exactly one task to be cancelled");
1280     }
1281     }
1282 dl 1.1 }
1283     }
1284    
1285     }