7 |
|
|
8 |
|
import java.util.concurrent.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.List; |
10 |
> |
import java.util.*; |
11 |
|
|
12 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
1025 |
|
joinPool(tpe); |
1026 |
|
} |
1027 |
|
} |
1028 |
+ |
|
1029 |
+ |
/** |
1030 |
+ |
* completed submit of callable returns result |
1031 |
+ |
*/ |
1032 |
+ |
public void testSubmitCallable() { |
1033 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1034 |
+ |
try { |
1035 |
+ |
Future<String> future = e.submit(new StringTask()); |
1036 |
+ |
String result = future.get(); |
1037 |
+ |
assertSame(TEST_STRING, result); |
1038 |
+ |
} |
1039 |
+ |
catch (ExecutionException ex) { |
1040 |
+ |
unexpectedException(); |
1041 |
+ |
} |
1042 |
+ |
catch (InterruptedException ex) { |
1043 |
+ |
unexpectedException(); |
1044 |
+ |
} finally { |
1045 |
+ |
joinPool(e); |
1046 |
+ |
} |
1047 |
+ |
} |
1048 |
+ |
|
1049 |
+ |
/** |
1050 |
+ |
* completed submit of runnable returns successfully |
1051 |
+ |
*/ |
1052 |
+ |
public void testSubmitRunnable() { |
1053 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1054 |
+ |
try { |
1055 |
+ |
Future<?> future = e.submit(new NoOpRunnable()); |
1056 |
+ |
future.get(); |
1057 |
+ |
assertTrue(future.isDone()); |
1058 |
+ |
} |
1059 |
+ |
catch (ExecutionException ex) { |
1060 |
+ |
unexpectedException(); |
1061 |
+ |
} |
1062 |
+ |
catch (InterruptedException ex) { |
1063 |
+ |
unexpectedException(); |
1064 |
+ |
} finally { |
1065 |
+ |
joinPool(e); |
1066 |
+ |
} |
1067 |
+ |
} |
1068 |
+ |
|
1069 |
+ |
/** |
1070 |
+ |
* completed submit of (runnable, result) returns result |
1071 |
+ |
*/ |
1072 |
+ |
public void testSubmitRunnable2() { |
1073 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1074 |
+ |
try { |
1075 |
+ |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1076 |
+ |
String result = future.get(); |
1077 |
+ |
assertSame(TEST_STRING, result); |
1078 |
+ |
} |
1079 |
+ |
catch (ExecutionException ex) { |
1080 |
+ |
unexpectedException(); |
1081 |
+ |
} |
1082 |
+ |
catch (InterruptedException ex) { |
1083 |
+ |
unexpectedException(); |
1084 |
+ |
} finally { |
1085 |
+ |
joinPool(e); |
1086 |
+ |
} |
1087 |
+ |
} |
1088 |
+ |
|
1089 |
+ |
|
1090 |
+ |
|
1091 |
+ |
|
1092 |
+ |
|
1093 |
+ |
/** |
1094 |
+ |
* invokeAny(null) throws NPE |
1095 |
+ |
*/ |
1096 |
+ |
public void testInvokeAny1() { |
1097 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1098 |
+ |
try { |
1099 |
+ |
e.invokeAny(null); |
1100 |
+ |
} catch (NullPointerException success) { |
1101 |
+ |
} catch(Exception ex) { |
1102 |
+ |
unexpectedException(); |
1103 |
+ |
} finally { |
1104 |
+ |
joinPool(e); |
1105 |
+ |
} |
1106 |
+ |
} |
1107 |
+ |
|
1108 |
+ |
/** |
1109 |
+ |
* invokeAny(empty collection) throws IAE |
1110 |
+ |
*/ |
1111 |
+ |
public void testInvokeAny2() { |
1112 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1113 |
+ |
try { |
1114 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
1115 |
+ |
} catch (IllegalArgumentException success) { |
1116 |
+ |
} catch(Exception ex) { |
1117 |
+ |
unexpectedException(); |
1118 |
+ |
} finally { |
1119 |
+ |
joinPool(e); |
1120 |
+ |
} |
1121 |
+ |
} |
1122 |
+ |
|
1123 |
+ |
/** |
1124 |
+ |
* invokeAny(c) throws NPE if c has null elements |
1125 |
+ |
*/ |
1126 |
+ |
public void testInvokeAny3() { |
1127 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1128 |
+ |
try { |
1129 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1130 |
+ |
l.add(new StringTask()); |
1131 |
+ |
l.add(null); |
1132 |
+ |
e.invokeAny(l); |
1133 |
+ |
} catch (NullPointerException success) { |
1134 |
+ |
} catch(Exception ex) { |
1135 |
+ |
unexpectedException(); |
1136 |
+ |
} finally { |
1137 |
+ |
joinPool(e); |
1138 |
+ |
} |
1139 |
+ |
} |
1140 |
+ |
|
1141 |
+ |
/** |
1142 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
1143 |
+ |
*/ |
1144 |
+ |
public void testInvokeAny4() { |
1145 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1146 |
+ |
try { |
1147 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1148 |
+ |
l.add(new NPETask()); |
1149 |
+ |
e.invokeAny(l); |
1150 |
+ |
} catch (ExecutionException success) { |
1151 |
+ |
} catch(Exception ex) { |
1152 |
+ |
unexpectedException(); |
1153 |
+ |
} finally { |
1154 |
+ |
joinPool(e); |
1155 |
+ |
} |
1156 |
+ |
} |
1157 |
+ |
|
1158 |
+ |
/** |
1159 |
+ |
* invokeAny(c) returns result of some task |
1160 |
+ |
*/ |
1161 |
+ |
public void testInvokeAny5() { |
1162 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1163 |
+ |
try { |
1164 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1165 |
+ |
l.add(new StringTask()); |
1166 |
+ |
l.add(new StringTask()); |
1167 |
+ |
String result = e.invokeAny(l); |
1168 |
+ |
assertSame(TEST_STRING, result); |
1169 |
+ |
} catch (ExecutionException success) { |
1170 |
+ |
} catch(Exception ex) { |
1171 |
+ |
unexpectedException(); |
1172 |
+ |
} finally { |
1173 |
+ |
joinPool(e); |
1174 |
+ |
} |
1175 |
+ |
} |
1176 |
+ |
|
1177 |
+ |
/** |
1178 |
+ |
* invokeAll(null) throws NPE |
1179 |
+ |
*/ |
1180 |
+ |
public void testInvokeAll1() { |
1181 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1182 |
+ |
try { |
1183 |
+ |
e.invokeAll(null); |
1184 |
+ |
} catch (NullPointerException success) { |
1185 |
+ |
} catch(Exception ex) { |
1186 |
+ |
unexpectedException(); |
1187 |
+ |
} finally { |
1188 |
+ |
joinPool(e); |
1189 |
+ |
} |
1190 |
+ |
} |
1191 |
+ |
|
1192 |
+ |
/** |
1193 |
+ |
* invokeAll(empty collection) returns empty collection |
1194 |
+ |
*/ |
1195 |
+ |
public void testInvokeAll2() { |
1196 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1197 |
+ |
try { |
1198 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1199 |
+ |
assertTrue(r.isEmpty()); |
1200 |
+ |
} catch(Exception ex) { |
1201 |
+ |
unexpectedException(); |
1202 |
+ |
} finally { |
1203 |
+ |
joinPool(e); |
1204 |
+ |
} |
1205 |
+ |
} |
1206 |
+ |
|
1207 |
+ |
/** |
1208 |
+ |
* invokeAll(c) throws NPE if c has null elements |
1209 |
+ |
*/ |
1210 |
+ |
public void testInvokeAll3() { |
1211 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1212 |
+ |
try { |
1213 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1214 |
+ |
l.add(new StringTask()); |
1215 |
+ |
l.add(null); |
1216 |
+ |
e.invokeAll(l); |
1217 |
+ |
} catch (NullPointerException success) { |
1218 |
+ |
} catch(Exception ex) { |
1219 |
+ |
unexpectedException(); |
1220 |
+ |
} finally { |
1221 |
+ |
joinPool(e); |
1222 |
+ |
} |
1223 |
+ |
} |
1224 |
+ |
|
1225 |
+ |
/** |
1226 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1227 |
+ |
*/ |
1228 |
+ |
public void testInvokeAll4() { |
1229 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1230 |
+ |
try { |
1231 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1232 |
+ |
l.add(new NPETask()); |
1233 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1234 |
+ |
assertEquals(1, result.size()); |
1235 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1236 |
+ |
it.next().get(); |
1237 |
+ |
} catch(ExecutionException success) { |
1238 |
+ |
} catch(Exception ex) { |
1239 |
+ |
unexpectedException(); |
1240 |
+ |
} finally { |
1241 |
+ |
joinPool(e); |
1242 |
+ |
} |
1243 |
+ |
} |
1244 |
+ |
|
1245 |
+ |
/** |
1246 |
+ |
* invokeAll(c) returns results of all completed tasks |
1247 |
+ |
*/ |
1248 |
+ |
public void testInvokeAll5() { |
1249 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1250 |
+ |
try { |
1251 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1252 |
+ |
l.add(new StringTask()); |
1253 |
+ |
l.add(new StringTask()); |
1254 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1255 |
+ |
assertEquals(2, result.size()); |
1256 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1257 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1258 |
+ |
} catch (ExecutionException success) { |
1259 |
+ |
} catch(Exception ex) { |
1260 |
+ |
unexpectedException(); |
1261 |
+ |
} finally { |
1262 |
+ |
joinPool(e); |
1263 |
+ |
} |
1264 |
+ |
} |
1265 |
+ |
|
1266 |
+ |
|
1267 |
|
} |