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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.11 by dl, Thu Dec 4 20:54:46 2003 UTC vs.
Revision 1.20 by dl, Mon Apr 12 12:03:10 2004 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 java.util.concurrent.*;
10   import junit.framework.*;
11 < import java.util.List;
11 > import java.util.*;
12  
13   public class ThreadPoolExecutorTest extends JSR166TestCase {
14      public static void main(String[] args) {
# Line 35 | Line 36 | public class ThreadPoolExecutorTest exte
36          }
37      }
38  
39 +    static class FailingThreadFactory implements ThreadFactory{
40 +        int calls = 0;
41 +        public Thread newThread(Runnable r){
42 +            if (++calls > 1) return null;
43 +            return new Thread(r);
44 +        }  
45 +    }
46 +    
47 +
48      /**
49       *  execute successfully executes a runnable
50       */
# Line 116 | Line 126 | public class ThreadPoolExecutorTest exte
126              unexpectedException();
127          }
128          assertEquals(1, p2.getCompletedTaskCount());
129 <        p2.shutdown();
129 >        try { p2.shutdown(); } catch(SecurityException ok) { return; }
130          joinPool(p2);
131      }
132      
# Line 146 | Line 156 | public class ThreadPoolExecutorTest exte
156          ThreadFactory tf = new SimpleThreadFactory();
157          ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
158          assertSame(tf, p.getThreadFactory());
149        p.shutdown();
159          joinPool(p);
160      }
161  
# Line 158 | Line 167 | public class ThreadPoolExecutorTest exte
167          ThreadFactory tf = new SimpleThreadFactory();
168          p.setThreadFactory(tf);
169          assertSame(tf, p.getThreadFactory());
161        p.shutdown();
170          joinPool(p);
171      }
172  
# Line 184 | Line 192 | public class ThreadPoolExecutorTest exte
192          RejectedExecutionHandler h = new NoOpREHandler();
193          ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
194          assertSame(h, p.getRejectedExecutionHandler());
187        p.shutdown();
195          joinPool(p);
196      }
197  
# Line 197 | Line 204 | public class ThreadPoolExecutorTest exte
204          RejectedExecutionHandler h = new NoOpREHandler();
205          p.setRejectedExecutionHandler(h);
206          assertSame(h, p.getRejectedExecutionHandler());
200        p.shutdown();
207          joinPool(p);
208      }
209  
# Line 280 | Line 286 | public class ThreadPoolExecutorTest exte
286          
287          ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
288          assertFalse(p1.isShutdown());
289 <        p1.shutdown();
289 >        try { p1.shutdown(); } catch(SecurityException ok) { return; }
290          assertTrue(p1.isShutdown());
291          joinPool(p1);
292      }
# Line 295 | Line 301 | public class ThreadPoolExecutorTest exte
301          try {
302              p1.execute(new MediumRunnable());
303          } finally {
304 <            p1.shutdown();
304 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
305          }
306          try {
307              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 315 | Line 321 | public class ThreadPoolExecutorTest exte
321              p1.execute(new SmallRunnable());
322              assertFalse(p1.isTerminating());
323          } finally {
324 <            p1.shutdown();
324 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
325          }
326          try {
327              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 343 | Line 349 | public class ThreadPoolExecutorTest exte
349              assertSame(q, wq);
350              assertFalse(wq.contains(tasks[0]));
351              assertTrue(wq.contains(tasks[4]));
346            p1.shutdownNow();
352          } catch(Exception e) {
353              unexpectedException();
354          } finally {
# Line 373 | Line 378 | public class ThreadPoolExecutorTest exte
378              assertTrue(q.contains(tasks[3]));
379              assertTrue(p1.remove(tasks[3]));
380              assertFalse(q.contains(tasks[3]));
376            p1.shutdownNow();
381          } catch(Exception e) {
382              unexpectedException();
383          } finally {
# Line 396 | Line 400 | public class ThreadPoolExecutorTest exte
400          p1.purge();
401          long count = p1.getTaskCount();
402          assertTrue(count >= 2 && count < 5);
399        p1.shutdownNow();
403          joinPool(p1);
404      }
405  
# Line 411 | Line 414 | public class ThreadPoolExecutorTest exte
414                  p1.execute(new MediumPossiblyInterruptedRunnable());
415          }
416          finally {
417 <            l = p1.shutdownNow();
417 >            try {
418 >                l = p1.shutdownNow();
419 >            } catch (SecurityException ok) { return; }
420 >            
421          }
422          assertTrue(p1.isShutdown());
423          assertTrue(l.size() <= 4);
# Line 772 | Line 778 | public class ThreadPoolExecutorTest exte
778              for(int i = 1; i < 5; ++i) {
779                  assertTrue(tasks[i].done);
780              }
781 <            p.shutdownNow();
781 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
782          } catch(RejectedExecutionException ex){
783              unexpectedException();
784          } finally {
# Line 799 | Line 805 | public class ThreadPoolExecutorTest exte
805              for(int i = 0; i < 5; ++i){
806                  assertFalse(tasks[i].done);
807              }
808 <            p.shutdownNow();
808 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
809          } catch(RejectedExecutionException ex){
810              unexpectedException();
811          } finally {
# Line 822 | Line 828 | public class ThreadPoolExecutorTest exte
828              p.execute(r3);
829              assertFalse(p.getQueue().contains(r2));
830              assertTrue(p.getQueue().contains(r3));
831 <            p.shutdownNow();
831 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
832          } catch(RejectedExecutionException ex){
833              unexpectedException();
834          } finally {
# Line 836 | Line 842 | public class ThreadPoolExecutorTest exte
842      public void testRejectedExecutionExceptionOnShutdown() {
843          ThreadPoolExecutor tpe =
844              new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
845 <        tpe.shutdown();
845 >        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
846          try {
847              tpe.execute(new NoOpRunnable());
848              shouldThrow();
# Line 852 | Line 858 | public class ThreadPoolExecutorTest exte
858          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
859          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
860  
861 <        p.shutdown();
861 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
862          try {
863              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
864              p.execute(r);
# Line 871 | Line 877 | public class ThreadPoolExecutorTest exte
877          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
878          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
879  
880 <        p.shutdown();
880 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
881          try {
882              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
883              p.execute(r);
# Line 891 | Line 897 | public class ThreadPoolExecutorTest exte
897          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
898          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
899  
900 <        p.shutdown();
900 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
901          try {
902              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
903              p.execute(r);
# Line 931 | Line 937 | public class ThreadPoolExecutorTest exte
937              shouldThrow();
938          } catch(IllegalArgumentException success){
939          } finally {
940 <            tpe.shutdown();
940 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
941          }
942          joinPool(tpe);
943      }  
# Line 950 | Line 956 | public class ThreadPoolExecutorTest exte
956              shouldThrow();
957          } catch(IllegalArgumentException success){
958          } finally {
959 <            tpe.shutdown();
959 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
960          }
961          joinPool(tpe);
962      }
# Line 969 | Line 975 | public class ThreadPoolExecutorTest exte
975              shouldThrow();
976          } catch(IllegalArgumentException success){
977          } finally {
978 <            tpe.shutdown();
978 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
979          }
980          joinPool(tpe);
981      }
# Line 990 | Line 996 | public class ThreadPoolExecutorTest exte
996              shouldThrow();
997          } catch(IllegalArgumentException success){
998          } finally {
999 <            tpe.shutdown();
999 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1000          }
1001          joinPool(tpe);
1002      }
# Line 1000 | Line 1006 | public class ThreadPoolExecutorTest exte
1006       */
1007      public void testTerminated() {
1008          ExtendedTPE tpe = new ExtendedTPE();
1009 <        tpe.shutdown();
1009 >        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1010          assertTrue(tpe.terminatedCalled);
1011          joinPool(tpe);
1012      }
# Line 1017 | Line 1023 | public class ThreadPoolExecutorTest exte
1023              assertTrue(r.done);
1024              assertTrue(tpe.beforeCalled);
1025              assertTrue(tpe.afterCalled);
1026 <            tpe.shutdown();
1026 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1027          }
1028          catch(Exception ex) {
1029              unexpectedException();
# Line 1025 | Line 1031 | public class ThreadPoolExecutorTest exte
1031              joinPool(tpe);
1032          }
1033      }
1034 +
1035 +    /**
1036 +     * completed submit of callable returns result
1037 +     */
1038 +    public void testSubmitCallable() {
1039 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1040 +        try {
1041 +            Future<String> future = e.submit(new StringTask());
1042 +            String result = future.get();
1043 +            assertSame(TEST_STRING, result);
1044 +        }
1045 +        catch (ExecutionException ex) {
1046 +            unexpectedException();
1047 +        }
1048 +        catch (InterruptedException ex) {
1049 +            unexpectedException();
1050 +        } finally {
1051 +            joinPool(e);
1052 +        }
1053 +    }
1054 +
1055 +    /**
1056 +     * completed submit of runnable returns successfully
1057 +     */
1058 +    public void testSubmitRunnable() {
1059 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1060 +        try {
1061 +            Future<?> future = e.submit(new NoOpRunnable());
1062 +            future.get();
1063 +            assertTrue(future.isDone());
1064 +        }
1065 +        catch (ExecutionException ex) {
1066 +            unexpectedException();
1067 +        }
1068 +        catch (InterruptedException ex) {
1069 +            unexpectedException();
1070 +        } finally {
1071 +            joinPool(e);
1072 +        }
1073 +    }
1074 +
1075 +    /**
1076 +     * completed submit of (runnable, result) returns result
1077 +     */
1078 +    public void testSubmitRunnable2() {
1079 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1080 +        try {
1081 +            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1082 +            String result = future.get();
1083 +            assertSame(TEST_STRING, result);
1084 +        }
1085 +        catch (ExecutionException ex) {
1086 +            unexpectedException();
1087 +        }
1088 +        catch (InterruptedException ex) {
1089 +            unexpectedException();
1090 +        } finally {
1091 +            joinPool(e);
1092 +        }
1093 +    }
1094 +
1095 +
1096 +
1097 +
1098 +
1099 +    /**
1100 +     * invokeAny(null) throws NPE
1101 +     */
1102 +    public void testInvokeAny1() {
1103 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1104 +        try {
1105 +            e.invokeAny(null);
1106 +        } catch (NullPointerException success) {
1107 +        } catch(Exception ex) {
1108 +            unexpectedException();
1109 +        } finally {
1110 +            joinPool(e);
1111 +        }
1112 +    }
1113 +
1114 +    /**
1115 +     * invokeAny(empty collection) throws IAE
1116 +     */
1117 +    public void testInvokeAny2() {
1118 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1119 +        try {
1120 +            e.invokeAny(new ArrayList<Callable<String>>());
1121 +        } catch (IllegalArgumentException success) {
1122 +        } catch(Exception ex) {
1123 +            unexpectedException();
1124 +        } finally {
1125 +            joinPool(e);
1126 +        }
1127 +    }
1128 +
1129 +    /**
1130 +     * invokeAny(c) throws NPE if c has null elements
1131 +     */
1132 +    public void testInvokeAny3() {
1133 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1134 +        try {
1135 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1136 +            l.add(new StringTask());
1137 +            l.add(null);
1138 +            e.invokeAny(l);
1139 +        } catch (NullPointerException success) {
1140 +        } catch(Exception ex) {
1141 +            unexpectedException();
1142 +        } finally {
1143 +            joinPool(e);
1144 +        }
1145 +    }
1146 +
1147 +    /**
1148 +     * invokeAny(c) throws ExecutionException if no task completes
1149 +     */
1150 +    public void testInvokeAny4() {
1151 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1152 +        try {
1153 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1154 +            l.add(new NPETask());
1155 +            e.invokeAny(l);
1156 +        } catch (ExecutionException success) {
1157 +        } catch(Exception ex) {
1158 +            unexpectedException();
1159 +        } finally {
1160 +            joinPool(e);
1161 +        }
1162 +    }
1163 +
1164 +    /**
1165 +     * invokeAny(c) returns result of some task
1166 +     */
1167 +    public void testInvokeAny5() {
1168 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1169 +        try {
1170 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1171 +            l.add(new StringTask());
1172 +            l.add(new StringTask());
1173 +            String result = e.invokeAny(l);
1174 +            assertSame(TEST_STRING, result);
1175 +        } catch (ExecutionException success) {
1176 +        } catch(Exception ex) {
1177 +            unexpectedException();
1178 +        } finally {
1179 +            joinPool(e);
1180 +        }
1181 +    }
1182 +
1183 +    /**
1184 +     * invokeAll(null) throws NPE
1185 +     */
1186 +    public void testInvokeAll1() {
1187 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1188 +        try {
1189 +            e.invokeAll(null);
1190 +        } catch (NullPointerException success) {
1191 +        } catch(Exception ex) {
1192 +            unexpectedException();
1193 +        } finally {
1194 +            joinPool(e);
1195 +        }
1196 +    }
1197 +
1198 +    /**
1199 +     * invokeAll(empty collection) returns empty collection
1200 +     */
1201 +    public void testInvokeAll2() {
1202 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1203 +        try {
1204 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1205 +            assertTrue(r.isEmpty());
1206 +        } catch(Exception ex) {
1207 +            unexpectedException();
1208 +        } finally {
1209 +            joinPool(e);
1210 +        }
1211 +    }
1212 +
1213 +    /**
1214 +     * invokeAll(c) throws NPE if c has null elements
1215 +     */
1216 +    public void testInvokeAll3() {
1217 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1218 +        try {
1219 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1220 +            l.add(new StringTask());
1221 +            l.add(null);
1222 +            e.invokeAll(l);
1223 +        } catch (NullPointerException success) {
1224 +        } catch(Exception ex) {
1225 +            unexpectedException();
1226 +        } finally {
1227 +            joinPool(e);
1228 +        }
1229 +    }
1230 +
1231 +    /**
1232 +     * get of element of invokeAll(c) throws exception on failed task
1233 +     */
1234 +    public void testInvokeAll4() {
1235 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1236 +        try {
1237 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1238 +            l.add(new NPETask());
1239 +            List<Future<String>> result = e.invokeAll(l);
1240 +            assertEquals(1, result.size());
1241 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1242 +                it.next().get();
1243 +        } catch(ExecutionException success) {
1244 +        } catch(Exception ex) {
1245 +            unexpectedException();
1246 +        } finally {
1247 +            joinPool(e);
1248 +        }
1249 +    }
1250 +
1251 +    /**
1252 +     * invokeAll(c) returns results of all completed tasks
1253 +     */
1254 +    public void testInvokeAll5() {
1255 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1256 +        try {
1257 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1258 +            l.add(new StringTask());
1259 +            l.add(new StringTask());
1260 +            List<Future<String>> result = e.invokeAll(l);
1261 +            assertEquals(2, result.size());
1262 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1263 +                assertSame(TEST_STRING, it.next().get());
1264 +        } catch (ExecutionException success) {
1265 +        } catch(Exception ex) {
1266 +            unexpectedException();
1267 +        } finally {
1268 +            joinPool(e);
1269 +        }
1270 +    }
1271 +
1272 +
1273 +
1274 +    /**
1275 +     * timed invokeAny(null) throws NPE
1276 +     */
1277 +    public void testTimedInvokeAny1() {
1278 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1279 +        try {
1280 +            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1281 +        } catch (NullPointerException success) {
1282 +        } catch(Exception ex) {
1283 +            unexpectedException();
1284 +        } finally {
1285 +            joinPool(e);
1286 +        }
1287 +    }
1288 +
1289 +    /**
1290 +     * timed invokeAny(,,null) throws NPE
1291 +     */
1292 +    public void testTimedInvokeAnyNullTimeUnit() {
1293 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1294 +        try {
1295 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1296 +            l.add(new StringTask());
1297 +            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1298 +        } catch (NullPointerException success) {
1299 +        } catch(Exception ex) {
1300 +            unexpectedException();
1301 +        } finally {
1302 +            joinPool(e);
1303 +        }
1304 +    }
1305 +
1306 +    /**
1307 +     * timed invokeAny(empty collection) throws IAE
1308 +     */
1309 +    public void testTimedInvokeAny2() {
1310 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1311 +        try {
1312 +            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1313 +        } catch (IllegalArgumentException success) {
1314 +        } catch(Exception ex) {
1315 +            unexpectedException();
1316 +        } finally {
1317 +            joinPool(e);
1318 +        }
1319 +    }
1320 +
1321 +    /**
1322 +     * timed invokeAny(c) throws NPE if c has null elements
1323 +     */
1324 +    public void testTimedInvokeAny3() {
1325 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1326 +        try {
1327 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1328 +            l.add(new StringTask());
1329 +            l.add(null);
1330 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1331 +        } catch (NullPointerException success) {
1332 +        } catch(Exception ex) {
1333 +            ex.printStackTrace();
1334 +            unexpectedException();
1335 +        } finally {
1336 +            joinPool(e);
1337 +        }
1338 +    }
1339 +
1340 +    /**
1341 +     * timed invokeAny(c) throws ExecutionException if no task completes
1342 +     */
1343 +    public void testTimedInvokeAny4() {
1344 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1345 +        try {
1346 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1347 +            l.add(new NPETask());
1348 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1349 +        } catch(ExecutionException success) {
1350 +        } catch(Exception ex) {
1351 +            unexpectedException();
1352 +        } finally {
1353 +            joinPool(e);
1354 +        }
1355 +    }
1356 +
1357 +    /**
1358 +     * timed invokeAny(c) returns result of some task
1359 +     */
1360 +    public void testTimedInvokeAny5() {
1361 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1362 +        try {
1363 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1364 +            l.add(new StringTask());
1365 +            l.add(new StringTask());
1366 +            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1367 +            assertSame(TEST_STRING, result);
1368 +        } catch (ExecutionException success) {
1369 +        } catch(Exception ex) {
1370 +            unexpectedException();
1371 +        } finally {
1372 +            joinPool(e);
1373 +        }
1374 +    }
1375 +
1376 +    /**
1377 +     * timed invokeAll(null) throws NPE
1378 +     */
1379 +    public void testTimedInvokeAll1() {
1380 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1381 +        try {
1382 +            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1383 +        } catch (NullPointerException success) {
1384 +        } catch(Exception ex) {
1385 +            unexpectedException();
1386 +        } finally {
1387 +            joinPool(e);
1388 +        }
1389 +    }
1390 +
1391 +    /**
1392 +     * timed invokeAll(,,null) throws NPE
1393 +     */
1394 +    public void testTimedInvokeAllNullTimeUnit() {
1395 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1396 +        try {
1397 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1398 +            l.add(new StringTask());
1399 +            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1400 +        } catch (NullPointerException success) {
1401 +        } catch(Exception ex) {
1402 +            unexpectedException();
1403 +        } finally {
1404 +            joinPool(e);
1405 +        }
1406 +    }
1407 +
1408 +    /**
1409 +     * timed invokeAll(empty collection) returns empty collection
1410 +     */
1411 +    public void testTimedInvokeAll2() {
1412 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1413 +        try {
1414 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1415 +            assertTrue(r.isEmpty());
1416 +        } catch(Exception ex) {
1417 +            unexpectedException();
1418 +        } finally {
1419 +            joinPool(e);
1420 +        }
1421 +    }
1422 +
1423 +    /**
1424 +     * timed invokeAll(c) throws NPE if c has null elements
1425 +     */
1426 +    public void testTimedInvokeAll3() {
1427 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1428 +        try {
1429 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1430 +            l.add(new StringTask());
1431 +            l.add(null);
1432 +            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1433 +        } catch (NullPointerException success) {
1434 +        } catch(Exception ex) {
1435 +            unexpectedException();
1436 +        } finally {
1437 +            joinPool(e);
1438 +        }
1439 +    }
1440 +
1441 +    /**
1442 +     * get of element of invokeAll(c) throws exception on failed task
1443 +     */
1444 +    public void testTimedInvokeAll4() {
1445 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1446 +        try {
1447 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1448 +            l.add(new NPETask());
1449 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1450 +            assertEquals(1, result.size());
1451 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1452 +                it.next().get();
1453 +        } catch(ExecutionException success) {
1454 +        } catch(Exception ex) {
1455 +            unexpectedException();
1456 +        } finally {
1457 +            joinPool(e);
1458 +        }
1459 +    }
1460 +
1461 +    /**
1462 +     * timed invokeAll(c) returns results of all completed tasks
1463 +     */
1464 +    public void testTimedInvokeAll5() {
1465 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1466 +        try {
1467 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1468 +            l.add(new StringTask());
1469 +            l.add(new StringTask());
1470 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1471 +            assertEquals(2, result.size());
1472 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1473 +                assertSame(TEST_STRING, it.next().get());
1474 +        } catch (ExecutionException success) {
1475 +        } catch(Exception ex) {
1476 +            unexpectedException();
1477 +        } finally {
1478 +            joinPool(e);
1479 +        }
1480 +    }
1481 +
1482 +    /**
1483 +     * timed invokeAll(c) cancels tasks not completed by timeout
1484 +     */
1485 +    public void testTimedInvokeAll6() {
1486 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1487 +        try {
1488 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1489 +            l.add(new StringTask());
1490 +            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1491 +            l.add(new StringTask());
1492 +            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1493 +            assertEquals(3, result.size());
1494 +            Iterator<Future<String>> it = result.iterator();
1495 +            Future<String> f1 = it.next();
1496 +            Future<String> f2 = it.next();
1497 +            Future<String> f3 = it.next();
1498 +            assertTrue(f1.isDone());
1499 +            assertTrue(f2.isDone());
1500 +            assertTrue(f3.isDone());
1501 +            assertFalse(f1.isCancelled());
1502 +            assertTrue(f2.isCancelled());
1503 +        } catch(Exception ex) {
1504 +            unexpectedException();
1505 +        } finally {
1506 +            joinPool(e);
1507 +        }
1508 +    }
1509 +
1510 +    /**
1511 +     * Execution continues if there is at least one thread even if
1512 +     * thread factory fails to create more
1513 +     */
1514 +    public void testFailingThreadFactory() {
1515 +        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1516 +        try {
1517 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1518 +            for (int k = 0; k < 100; ++k) {
1519 +                e.execute(new NoOpRunnable());
1520 +            }
1521 +            Thread.sleep(LONG_DELAY_MS);
1522 +        } catch(Exception ex) {
1523 +            unexpectedException();
1524 +        } finally {
1525 +            joinPool(e);
1526 +        }
1527 +    }
1528   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines