[cvs] / jsr166 / src / test / tck / AbstractExecutorServiceTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/AbstractExecutorServiceTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6, Mon Dec 22 00:48:55 2003 UTC revision 1.13, Mon Jan 12 19:39:21 2004 UTC
# Line 1  Line 1 
1  /*  /*
2   * Written by members of JCP JSR-166 Expert Group and released to the   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * public domain. Use, modify, and redistribute this code in any way   * Expert Group and released to the public domain, as explained at
4   * without acknowledgement. Other contributors include Andrew Wright,   * http://creativecommons.org/licenses/publicdomain
5   * Jeffrey Hayes, Pat Fischer, Mike Judd.   * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7   */   */
8    
9    
# Line 35  Line 36 
36      }      }
37    
38      /**      /**
39       * execute of runnable runs it to completion       * execute(runnable) runs it to completion
40       */       */
41      public void testExecuteRunnable() {      public void testExecuteRunnable() {
42          try {          try {
# Line 56  Line 57 
57    
58    
59      /**      /**
60       * completed submit of callable returns result       * Completed submit(callable) returns result
61       */       */
62      public void testSubmitCallable() {      public void testSubmitCallable() {
63          try {          try {
# Line 74  Line 75 
75      }      }
76    
77      /**      /**
78       * completed submit of runnable returns successfully       * Completed submit(runnable) returns successfully
79       */       */
80      public void testSubmitRunnable() {      public void testSubmitRunnable() {
81          try {          try {
# Line 92  Line 93 
93      }      }
94    
95      /**      /**
96       * completed submit of (runnable, result) returns result       * Completed submit(runnable, result) returns result
97       */       */
98      public void testSubmitRunnable2() {      public void testSubmitRunnable2() {
99          try {          try {
# Line 111  Line 112 
112    
113    
114      /**      /**
115       * submit of a privileged action runs it to completion       * A submitted privileged action to completion
116       */       */
117      public void testSubmitPrivilegedAction() {      public void testSubmitPrivilegedAction() {
118          Policy savedPolicy = Policy.getPolicy();          Policy savedPolicy = Policy.getPolicy();
# Line 141  Line 142 
142      }      }
143    
144      /**      /**
145       * submit of a privileged exception action runs it to completion       * A submitted a privileged exception action runs to completion
146       */       */
147      public void testSubmitPrivilegedExceptionAction() {      public void testSubmitPrivilegedExceptionAction() {
148          Policy savedPolicy = Policy.getPolicy();          Policy savedPolicy = Policy.getPolicy();
# Line 171  Line 172 
172      }      }
173    
174      /**      /**
175       * submit of a failed privileged exception action reports exception       * A submitted failed privileged exception action reports exception
176       */       */
177      public void testSubmitFailedPrivilegedExceptionAction() {      public void testSubmitFailedPrivilegedExceptionAction() {
178          Policy savedPolicy = Policy.getPolicy();          Policy savedPolicy = Policy.getPolicy();
# Line 200  Line 201 
201      }      }
202    
203      /**      /**
204       * execute with a null runnable throws NPE       * execute(null runnable) throws NPE
205       */       */
206      public void testExecuteNullRunnable() {      public void testExecuteNullRunnable() {
207          try {          try {
# Line 218  Line 219 
219    
220    
221      /**      /**
222       * submit of a null callable throws NPE       * submit(null callable) throws NPE
223       */       */
224      public void testSubmitNullCallable() {      public void testSubmitNullCallable() {
225          try {          try {
# Line 235  Line 236 
236      }      }
237    
238      /**      /**
239       * submit of Runnable throws RejectedExecutionException if       * submit(runnable) throws RejectedExecutionException if
240       * saturated.       * executor is saturated.
241       */       */
242      public void testExecute1() {      public void testExecute1() {
243          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
244          try {          try {
245    
246              for(int i = 0; i < 5; ++i){              for(int i = 0; i < 5; ++i){
# Line 251  Line 252 
252      }      }
253    
254      /**      /**
255       * Completed submit of Callable throws RejectedExecutionException       * submit(callable) throws RejectedExecutionException
256       *  if saturated.       * if executor is saturated.
257       */       */
258      public void testExecute2() {      public void testExecute2() {
259           ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));           ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
260          try {          try {
261              for(int i = 0; i < 5; ++i) {              for(int i = 0; i < 5; ++i) {
262                  p.submit(new SmallCallable());                  p.submit(new SmallCallable());
# Line 267  Line 268 
268    
269    
270      /**      /**
271       *  blocking on submit of Callable throws InterruptedException if       *  Blocking on submit(callable) throws InterruptedException if
272       *  caller interrupted.       *  caller interrupted.
273       */       */
274      public void testInterruptedSubmit() {      public void testInterruptedSubmit() {
275          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
276          Thread t = new Thread(new Runnable() {          Thread t = new Thread(new Runnable() {
277                  public void run() {                  public void run() {
278                      try {                      try {
# Line 303  Line 304 
304      }      }
305    
306      /**      /**
307       *  get of submit of Callable throws Exception if callable       *  get of submitted callable throws Exception if callable
308       *  interrupted       *  interrupted
309       */       */
310      public void testSubmitIE() {      public void testSubmitIE() {
311          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));          final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
312    
313          final Callable c = new Callable() {          final Callable c = new Callable() {
314                  public Object call() {                  public Object call() {
# Line 343  Line 344 
344      }      }
345    
346      /**      /**
347       *  completed submit of Callable throws ExecutionException if       *  get of submit(callable) throws ExecutionException if callable
348       *  callable throws exception       *  throws exception
349       */       */
350      public void testSubmitEE() {      public void testSubmitEE() {
351          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
352    
353          try {          try {
354              Callable c = new Callable() {              Callable c = new Callable() {
# Line 412  Line 413 
413              e.invokeAny(l);              e.invokeAny(l);
414          } catch (NullPointerException success) {          } catch (NullPointerException success) {
415          } catch(Exception ex) {          } catch(Exception ex) {
416                ex.printStackTrace();
417              unexpectedException();              unexpectedException();
418          } finally {          } finally {
419              joinPool(e);              joinPool(e);
# Line 419  Line 421 
421      }      }
422    
423      /**      /**
424       * invokeAny(c) throws ExecutionException if no task completes       * invokeAny(c) throws ExecutionException if no task in c completes
425       */       */
426      public void testInvokeAny4() {      public void testInvokeAny4() {
427          ExecutorService e = new DirectExecutorService();          ExecutorService e = new DirectExecutorService();
428          try {          try {
429              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
430              l.add(new NPETask());              l.add(new NPETask());
431              List<Future<String>> result = e.invokeAll(l);              e.invokeAny(l);
             assertEquals(1, result.size());  
             for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)  
                 it.next().get();  
432          } catch(ExecutionException success) {          } catch(ExecutionException success) {
433          } catch(Exception ex) {          } catch(Exception ex) {
434              unexpectedException();              unexpectedException();
# Line 439  Line 438 
438      }      }
439    
440      /**      /**
441       * invokeAny(c) returns result of some task       * invokeAny(c) returns result of some task in c if at least one completes
442       */       */
443      public void testInvokeAny5() {      public void testInvokeAny5() {
444          ExecutorService e = new DirectExecutorService();          ExecutorService e = new DirectExecutorService();
# Line 506  Line 505 
505      }      }
506    
507      /**      /**
508       * get of element of invokeAll(c) throws exception on failed task       * get of returned element of invokeAll(c) throws exception on failed task
509       */       */
510      public void testInvokeAll4() {      public void testInvokeAll4() {
511          ExecutorService e = new DirectExecutorService();          ExecutorService e = new DirectExecutorService();
# Line 526  Line 525 
525      }      }
526    
527      /**      /**
528       * invokeAll(c) returns results of all completed tasks       * invokeAll(c) returns results of all completed tasks in c
529       */       */
530      public void testInvokeAll5() {      public void testInvokeAll5() {
531          ExecutorService e = new DirectExecutorService();          ExecutorService e = new DirectExecutorService();
# Line 546  Line 545 
545          }          }
546      }      }
547    
548    
549        /**
550         * timed invokeAny(null) throws NPE
551         */
552        public void testTimedInvokeAny1() {
553            ExecutorService e = new DirectExecutorService();
554            try {
555                e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
556            } catch (NullPointerException success) {
557            } catch(Exception ex) {
558                unexpectedException();
559            } finally {
560                joinPool(e);
561            }
562        }
563    
564        /**
565         * timed invokeAny(null time unit) throws NPE
566         */
567        public void testTimedInvokeAnyNullTimeUnit() {
568            ExecutorService e = new DirectExecutorService();
569            try {
570                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
571                l.add(new StringTask());
572                e.invokeAny(l, MEDIUM_DELAY_MS, null);
573            } catch (NullPointerException success) {
574            } catch(Exception ex) {
575                unexpectedException();
576            } finally {
577                joinPool(e);
578            }
579        }
580    
581        /**
582         * timed invokeAny(empty collection) throws IAE
583         */
584        public void testTimedInvokeAny2() {
585            ExecutorService e = new DirectExecutorService();
586            try {
587                e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
588            } catch (IllegalArgumentException success) {
589            } catch(Exception ex) {
590                unexpectedException();
591            } finally {
592                joinPool(e);
593            }
594        }
595    
596        /**
597         * timed invokeAny(c) throws NPE if c has null elements
598         */
599        public void testTimedInvokeAny3() {
600            ExecutorService e = new DirectExecutorService();
601            try {
602                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
603                l.add(new StringTask());
604                l.add(null);
605                e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
606            } catch (NullPointerException success) {
607            } catch(Exception ex) {
608                ex.printStackTrace();
609                unexpectedException();
610            } finally {
611                joinPool(e);
612            }
613        }
614    
615        /**
616         * timed invokeAny(c) throws ExecutionException if no task completes
617         */
618        public void testTimedInvokeAny4() {
619            ExecutorService e = new DirectExecutorService();
620            try {
621                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
622                l.add(new NPETask());
623                e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
624            } catch(ExecutionException success) {
625            } catch(Exception ex) {
626                unexpectedException();
627            } finally {
628                joinPool(e);
629            }
630        }
631    
632        /**
633         * timed invokeAny(c) returns result of some task in c
634         */
635        public void testTimedInvokeAny5() {
636            ExecutorService e = new DirectExecutorService();
637            try {
638                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
639                l.add(new StringTask());
640                l.add(new StringTask());
641                String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
642                assertSame(TEST_STRING, result);
643            } catch (ExecutionException success) {
644            } catch(Exception ex) {
645                unexpectedException();
646            } finally {
647                joinPool(e);
648            }
649        }
650    
651        /**
652         * timed invokeAll(null) throws NPE
653         */
654        public void testTimedInvokeAll1() {
655            ExecutorService e = new DirectExecutorService();
656            try {
657                e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
658            } catch (NullPointerException success) {
659            } catch(Exception ex) {
660                unexpectedException();
661            } finally {
662                joinPool(e);
663            }
664        }
665    
666        /**
667         * timed invokeAll(null time unit) throws NPE
668         */
669        public void testTimedInvokeAllNullTimeUnit() {
670            ExecutorService e = new DirectExecutorService();
671            try {
672                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
673                l.add(new StringTask());
674                e.invokeAll(l, MEDIUM_DELAY_MS, null);
675            } catch (NullPointerException success) {
676            } catch(Exception ex) {
677                unexpectedException();
678            } finally {
679                joinPool(e);
680            }
681        }
682    
683        /**
684         * timed invokeAll(empty collection) returns empty collection
685         */
686        public void testTimedInvokeAll2() {
687            ExecutorService e = new DirectExecutorService();
688            try {
689                List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
690                assertTrue(r.isEmpty());
691            } catch(Exception ex) {
692                unexpectedException();
693            } finally {
694                joinPool(e);
695            }
696        }
697    
698        /**
699         * timed invokeAll(c) throws NPE if c has null elements
700         */
701        public void testTimedInvokeAll3() {
702            ExecutorService e = new DirectExecutorService();
703            try {
704                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
705                l.add(new StringTask());
706                l.add(null);
707                e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
708            } catch (NullPointerException success) {
709            } catch(Exception ex) {
710                unexpectedException();
711            } finally {
712                joinPool(e);
713            }
714        }
715    
716        /**
717         * get of returned element of invokeAll(c) throws exception on failed task
718         */
719        public void testTimedInvokeAll4() {
720            ExecutorService e = new DirectExecutorService();
721            try {
722                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
723                l.add(new NPETask());
724                List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
725                assertEquals(1, result.size());
726                for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
727                    it.next().get();
728            } catch(ExecutionException success) {
729            } catch(Exception ex) {
730                unexpectedException();
731            } finally {
732                joinPool(e);
733            }
734        }
735    
736        /**
737         * timed invokeAll(c) returns results of all completed tasks in c
738         */
739        public void testTimedInvokeAll5() {
740            ExecutorService e = new DirectExecutorService();
741            try {
742                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
743                l.add(new StringTask());
744                l.add(new StringTask());
745                List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
746                assertEquals(2, result.size());
747                for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
748                    assertSame(TEST_STRING, it.next().get());
749            } catch (ExecutionException success) {
750            } catch(Exception ex) {
751                unexpectedException();
752            } finally {
753                joinPool(e);
754            }
755        }
756    
757        /**
758         * timed invokeAll cancels tasks not completed by timeout
759         */
760        public void testTimedInvokeAll6() {
761            ExecutorService e = new DirectExecutorService();
762            try {
763                ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
764                l.add(new StringTask());
765                l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
766                l.add(new StringTask());
767                List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
768                assertEquals(3, result.size());
769                Iterator<Future<String>> it = result.iterator();
770                Future<String> f1 = it.next();
771                Future<String> f2 = it.next();
772                Future<String> f3 = it.next();
773                assertTrue(f1.isDone());
774                assertFalse(f1.isCancelled());
775                assertTrue(f2.isDone());
776                assertTrue(f3.isDone());
777                assertTrue(f3.isCancelled());
778            } catch(Exception ex) {
779                unexpectedException();
780            } finally {
781                joinPool(e);
782            }
783        }
784    
785  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.13

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8