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

Comparing jsr166/src/test/tck/AbstractExecutorServiceTest.java (file contents):
Revision 1.5 by dl, Fri Dec 19 20:42:44 2003 UTC vs.
Revision 1.6 by dl, Mon Dec 22 00:48:55 2003 UTC

# Line 17 | Line 17 | public class AbstractExecutorServiceTest
17          junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(ExecutorsTest.class);
20 >        return new TestSuite(AbstractExecutorServiceTest.class);
21      }
22  
23      /**
24       * A no-frills implementation of AbstractExecutorService, designed
25 <     * to test the submit/invoke methods only.
25 >     * to test the submit methods only.
26       */
27      static class DirectExecutorService extends AbstractExecutorService {
28          public void execute(Runnable r) { r.run(); }
# Line 34 | Line 34 | public class AbstractExecutorServiceTest
34          private volatile boolean shutdown = false;
35      }
36  
37    private static final String TEST_STRING = "a test string";
38
39    private static class StringTask implements Callable<String> {
40        public String call() { return TEST_STRING; }
41    }
42
37      /**
38       * execute of runnable runs it to completion
39       */
# Line 62 | Line 56 | public class AbstractExecutorServiceTest
56  
57  
58      /**
59 <     * execute of a callable runs it to completion
59 >     * completed submit of callable returns result
60       */
61 <    public void testExecuteCallable() {
61 >    public void testSubmitCallable() {
62          try {
63              ExecutorService e = new DirectExecutorService();
64              Future<String> future = e.submit(new StringTask());
# Line 79 | Line 73 | public class AbstractExecutorServiceTest
73          }
74      }
75  
76 +    /**
77 +     * completed submit of runnable returns successfully
78 +     */
79 +    public void testSubmitRunnable() {
80 +        try {
81 +            ExecutorService e = new DirectExecutorService();
82 +            Future<?> future = e.submit(new NoOpRunnable());
83 +            future.get();
84 +            assertTrue(future.isDone());
85 +        }
86 +        catch (ExecutionException ex) {
87 +            unexpectedException();
88 +        }
89 +        catch (InterruptedException ex) {
90 +            unexpectedException();
91 +        }
92 +    }
93 +
94 +    /**
95 +     * completed submit of (runnable, result) returns result
96 +     */
97 +    public void testSubmitRunnable2() {
98 +        try {
99 +            ExecutorService e = new DirectExecutorService();
100 +            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
101 +            String result = future.get();
102 +            assertSame(TEST_STRING, result);
103 +        }
104 +        catch (ExecutionException ex) {
105 +            unexpectedException();
106 +        }
107 +        catch (InterruptedException ex) {
108 +            unexpectedException();
109 +        }
110 +    }
111 +
112  
113      /**
114 <     * execute of a privileged action runs it to completion
114 >     * submit of a privileged action runs it to completion
115       */
116 <    public void testExecutePrivilegedAction() {
116 >    public void testSubmitPrivilegedAction() {
117          Policy savedPolicy = Policy.getPolicy();
118          AdjustablePolicy policy = new AdjustablePolicy();
119          policy.addPermission(new RuntimePermission("getContextClassLoader"));
# Line 111 | Line 141 | public class AbstractExecutorServiceTest
141      }
142  
143      /**
144 <     * execute of a privileged exception action runs it to completion
144 >     * submit of a privileged exception action runs it to completion
145       */
146 <    public void testExecutePrivilegedExceptionAction() {
146 >    public void testSubmitPrivilegedExceptionAction() {
147          Policy savedPolicy = Policy.getPolicy();
148          AdjustablePolicy policy = new AdjustablePolicy();
149          policy.addPermission(new RuntimePermission("getContextClassLoader"));
# Line 141 | Line 171 | public class AbstractExecutorServiceTest
171      }
172  
173      /**
174 <     * execute of a failed privileged exception action reports exception
174 >     * submit of a failed privileged exception action reports exception
175       */
176 <    public void testExecuteFailedPrivilegedExceptionAction() {
176 >    public void testSubmitFailedPrivilegedExceptionAction() {
177          Policy savedPolicy = Policy.getPolicy();
178          AdjustablePolicy policy = new AdjustablePolicy();
179          policy.addPermission(new RuntimePermission("getContextClassLoader"));
# Line 170 | Line 200 | public class AbstractExecutorServiceTest
200      }
201  
202      /**
173     * invoke of a callable runs it to completion
174     */
175    public void testInvokeCallable() {
176        try {
177            ExecutorService e = new DirectExecutorService();
178            String result = e.invoke(new StringTask());
179
180            assertSame(TEST_STRING, result);
181        }
182        catch (ExecutionException ex) {
183            unexpectedException();
184        }
185        catch (InterruptedException ex) {
186            unexpectedException();
187        }
188    }
189
190    /**
203       * execute with a null runnable throws NPE
204       */
205      public void testExecuteNullRunnable() {
# Line 206 | Line 218 | public class AbstractExecutorServiceTest
218  
219  
220      /**
221 <     * execute of a null callable throws NPE
221 >     * submit of a null callable throws NPE
222       */
223 <    public void testExecuteNullCallable() {
223 >    public void testSubmitNullCallable() {
224          try {
225              ExecutorService e = new DirectExecutorService();
226              StringTask t = null;
# Line 223 | Line 235 | public class AbstractExecutorServiceTest
235      }
236  
237      /**
238 <     * invoke of a null callable throws NPE
239 <     */
228 <    public void testInvokeNullCallable() {
229 <        try {
230 <            ExecutorService e = new DirectExecutorService();
231 <            StringTask t = null;
232 <            String result = e.invoke(t);
233 <            shouldThrow();
234 <        }
235 <        catch (NullPointerException success) {
236 <        }
237 <        catch (Exception ex) {
238 <            unexpectedException();
239 <        }
240 <    }
241 <
242 <    /**
243 <     *  execute(Executor, Runnable) throws RejectedExecutionException
244 <     *  if saturated.
238 >     * submit of Runnable throws RejectedExecutionException if
239 >     * saturated.
240       */
241      public void testExecute1() {
242          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
# Line 256 | Line 251 | public class AbstractExecutorServiceTest
251      }
252  
253      /**
254 <     *  execute(Executor, Callable)throws RejectedExecutionException
254 >     * Completed submit of Callable throws RejectedExecutionException
255       *  if saturated.
256       */
257      public void testExecute2() {
# Line 272 | Line 267 | public class AbstractExecutorServiceTest
267  
268  
269      /**
270 <     *  invoke(Executor, Callable) throws InterruptedException if
270 >     *  blocking on submit of Callable throws InterruptedException if
271       *  caller interrupted.
272       */
273 <    public void testInterruptedInvoke() {
273 >    public void testInterruptedSubmit() {
274          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
275          Thread t = new Thread(new Runnable() {
276                  public void run() {
277                      try {
278 <                        p.invoke(new Callable<Object>() {
278 >                        p.submit(new Callable<Object>() {
279                                  public Object call() {
280                                      try {
281                                          Thread.sleep(MEDIUM_DELAY_MS);
# Line 289 | Line 284 | public class AbstractExecutorServiceTest
284                                      }
285                                      return null;
286                                  }
287 <                            });
287 >                            }).get();
288                      } catch(InterruptedException success){
289                      } catch(Exception e) {
290                          unexpectedException();
# Line 308 | Line 303 | public class AbstractExecutorServiceTest
303      }
304  
305      /**
306 <     *  invoke(Executor, Callable) throws InterruptedException if
307 <     *  callable throws exception
306 >     *  get of submit of Callable throws Exception if callable
307 >     *  interrupted
308       */
309 <    public void testInvoke5() {
309 >    public void testSubmitIE() {
310          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
311  
312          final Callable c = new Callable() {
313                  public Object call() {
314                      try {
315 <                        p.invoke(new SmallCallable());
315 >                        p.submit(new SmallCallable()).get();
316                          shouldThrow();
317                      } catch(InterruptedException e){}
318                      catch(RejectedExecutionException e2){}
# Line 348 | Line 343 | public class AbstractExecutorServiceTest
343      }
344  
345      /**
346 <     *  invoke(Executor, Callable) will throw ExecutionException
347 <     *  if callable throws exception
346 >     *  completed submit of Callable throws ExecutionException if
347 >     *  callable throws exception
348       */
349 <    public void testInvoke6() {
349 >    public void testSubmitEE() {
350          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
351  
352          try {
# Line 363 | Line 358 | public class AbstractExecutorServiceTest
358                  };
359  
360              for(int i =0; i < 5; i++){
361 <                p.invoke(c);
361 >                p.submit(c).get();
362              }
363  
364              shouldThrow();
# Line 375 | Line 370 | public class AbstractExecutorServiceTest
370          joinPool(p);
371      }
372  
373 +    /**
374 +     * invokeAny(null) throws NPE
375 +     */
376 +    public void testInvokeAny1() {
377 +        ExecutorService e = new DirectExecutorService();
378 +        try {
379 +            e.invokeAny(null);
380 +        } catch (NullPointerException success) {
381 +        } catch(Exception ex) {
382 +            unexpectedException();
383 +        } finally {
384 +            joinPool(e);
385 +        }
386 +    }
387 +
388 +    /**
389 +     * invokeAny(empty collection) throws IAE
390 +     */
391 +    public void testInvokeAny2() {
392 +        ExecutorService e = new DirectExecutorService();
393 +        try {
394 +            e.invokeAny(new ArrayList<Callable<String>>());
395 +        } catch (IllegalArgumentException success) {
396 +        } catch(Exception ex) {
397 +            unexpectedException();
398 +        } finally {
399 +            joinPool(e);
400 +        }
401 +    }
402 +
403 +    /**
404 +     * invokeAny(c) throws NPE if c has null elements
405 +     */
406 +    public void testInvokeAny3() {
407 +        ExecutorService e = new DirectExecutorService();
408 +        try {
409 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
410 +            l.add(new StringTask());
411 +            l.add(null);
412 +            e.invokeAny(l);
413 +        } catch (NullPointerException success) {
414 +        } catch(Exception ex) {
415 +            unexpectedException();
416 +        } finally {
417 +            joinPool(e);
418 +        }
419 +    }
420 +
421 +    /**
422 +     * invokeAny(c) throws ExecutionException if no task completes
423 +     */
424 +    public void testInvokeAny4() {
425 +        ExecutorService e = new DirectExecutorService();
426 +        try {
427 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
428 +            l.add(new NPETask());
429 +            List<Future<String>> result = e.invokeAll(l);
430 +            assertEquals(1, result.size());
431 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
432 +                it.next().get();
433 +        } catch(ExecutionException success) {
434 +        } catch(Exception ex) {
435 +            unexpectedException();
436 +        } finally {
437 +            joinPool(e);
438 +        }
439 +    }
440 +
441 +    /**
442 +     * invokeAny(c) returns result of some task
443 +     */
444 +    public void testInvokeAny5() {
445 +        ExecutorService e = new DirectExecutorService();
446 +        try {
447 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
448 +            l.add(new StringTask());
449 +            l.add(new StringTask());
450 +            String result = e.invokeAny(l);
451 +            assertSame(TEST_STRING, result);
452 +        } catch (ExecutionException success) {
453 +        } catch(Exception ex) {
454 +            unexpectedException();
455 +        } finally {
456 +            joinPool(e);
457 +        }
458 +    }
459 +
460 +    /**
461 +     * invokeAll(null) throws NPE
462 +     */
463 +    public void testInvokeAll1() {
464 +        ExecutorService e = new DirectExecutorService();
465 +        try {
466 +            e.invokeAll(null);
467 +        } catch (NullPointerException success) {
468 +        } catch(Exception ex) {
469 +            unexpectedException();
470 +        } finally {
471 +            joinPool(e);
472 +        }
473 +    }
474 +
475 +    /**
476 +     * invokeAll(empty collection) returns empty collection
477 +     */
478 +    public void testInvokeAll2() {
479 +        ExecutorService e = new DirectExecutorService();
480 +        try {
481 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
482 +            assertTrue(r.isEmpty());
483 +        } catch(Exception ex) {
484 +            unexpectedException();
485 +        } finally {
486 +            joinPool(e);
487 +        }
488 +    }
489 +
490 +    /**
491 +     * invokeAll(c) throws NPE if c has null elements
492 +     */
493 +    public void testInvokeAll3() {
494 +        ExecutorService e = new DirectExecutorService();
495 +        try {
496 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
497 +            l.add(new StringTask());
498 +            l.add(null);
499 +            e.invokeAll(l);
500 +        } catch (NullPointerException success) {
501 +        } catch(Exception ex) {
502 +            unexpectedException();
503 +        } finally {
504 +            joinPool(e);
505 +        }
506 +    }
507 +
508 +    /**
509 +     * get of element of invokeAll(c) throws exception on failed task
510 +     */
511 +    public void testInvokeAll4() {
512 +        ExecutorService e = new DirectExecutorService();
513 +        try {
514 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
515 +            l.add(new NPETask());
516 +            List<Future<String>> result = e.invokeAll(l);
517 +            assertEquals(1, result.size());
518 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
519 +                it.next().get();
520 +        } catch(ExecutionException success) {
521 +        } catch(Exception ex) {
522 +            unexpectedException();
523 +        } finally {
524 +            joinPool(e);
525 +        }
526 +    }
527 +
528 +    /**
529 +     * invokeAll(c) returns results of all completed tasks
530 +     */
531 +    public void testInvokeAll5() {
532 +        ExecutorService e = new DirectExecutorService();
533 +        try {
534 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
535 +            l.add(new StringTask());
536 +            l.add(new StringTask());
537 +            List<Future<String>> result = e.invokeAll(l);
538 +            assertEquals(2, result.size());
539 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
540 +                assertSame(TEST_STRING, it.next().get());
541 +        } catch (ExecutionException success) {
542 +        } catch(Exception ex) {
543 +            unexpectedException();
544 +        } finally {
545 +            joinPool(e);
546 +        }
547 +    }
548 +
549   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines