ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
Revision: 1.36
Committed: Mon Sep 14 03:27:11 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +24 -11 lines
Log Message:
improve testTimedInvokeAll6

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