ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.10 by dl, Mon Dec 22 00:48:56 2003 UTC vs.
Revision 1.27 by jsr166, Sat Nov 21 17:38:05 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines