25 |
|
* 1. shutdown and related methods are tested via super.joinPool. |
26 |
|
* |
27 |
|
* 2. newTaskFor and adapters are tested in submit/invoke tests |
28 |
< |
* |
28 |
> |
* |
29 |
|
* 3. We cannot portably test monitoring methods such as |
30 |
|
* getStealCount() since they rely ultimately on random task |
31 |
|
* stealing that may cause tasks not to be stolen/propagated |
32 |
|
* across threads, especially on uniprocessors. |
33 |
< |
* |
33 |
> |
* |
34 |
|
* 4. There are no independently testable ForkJoinWorkerThread |
35 |
|
* methods, but they are covered here and in task tests. |
36 |
|
*/ |
52 |
|
|
53 |
|
static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
54 |
|
int calls = 0; |
55 |
< |
public ForkJoinWorkerThread newThread(ForkJoinPool p){ |
55 |
> |
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
56 |
|
if (++calls > 1) return null; |
57 |
|
return new FailingFJWSubclass(p); |
58 |
< |
} |
58 |
> |
} |
59 |
|
} |
60 |
|
|
61 |
< |
static class SubFJP extends ForkJoinPool { // to expose protected |
61 |
> |
static class SubFJP extends ForkJoinPool { // to expose protected |
62 |
|
SubFJP() { super(1); } |
63 |
|
public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
64 |
|
return super.drainTasksTo(c); |
83 |
|
} |
84 |
|
|
85 |
|
// A simple recursive task for testing |
86 |
< |
static final class FibTask extends RecursiveTask<Integer> { |
86 |
> |
static final class FibTask extends RecursiveTask<Integer> { |
87 |
|
final int number; |
88 |
|
FibTask(int n) { number = n; } |
89 |
|
public Integer compute() { |
97 |
|
} |
98 |
|
|
99 |
|
// A failing task for testing |
100 |
< |
static final class FailingTask extends ForkJoinTask<Void> { |
100 |
> |
static final class FailingTask extends ForkJoinTask<Void> { |
101 |
|
public final Void getRawResult() { return null; } |
102 |
|
protected final void setRawResult(Void mustBeNull) { } |
103 |
|
protected final boolean exec() { throw new Error(); } |
105 |
|
} |
106 |
|
|
107 |
|
// Fib needlessly using locking to test ManagedBlockers |
108 |
< |
static final class LockingFibTask extends RecursiveTask<Integer> { |
108 |
> |
static final class LockingFibTask extends RecursiveTask<Integer> { |
109 |
|
final int number; |
110 |
|
final ManagedLocker locker; |
111 |
|
final ReentrantLock lock; |
112 |
< |
LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) { |
113 |
< |
number = n; |
112 |
> |
LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) { |
113 |
> |
number = n; |
114 |
|
this.locker = locker; |
115 |
|
this.lock = lock; |
116 |
|
} |
119 |
|
LockingFibTask f1 = null; |
120 |
|
LockingFibTask f2 = null; |
121 |
|
locker.block(); |
122 |
< |
n = number; |
122 |
> |
n = number; |
123 |
|
if (n > 1) { |
124 |
|
f1 = new LockingFibTask(n - 1, locker, lock); |
125 |
|
f2 = new LockingFibTask(n - 2, locker, lock); |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
< |
/** |
137 |
> |
/** |
138 |
|
* Succesfully constructed pool reports default factory, |
139 |
|
* parallelism and async mode policies, no active threads or |
140 |
|
* tasks, and quiescent running state. |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
< |
/** |
164 |
< |
* Constructor throws if size argument is less than zero |
163 |
> |
/** |
164 |
> |
* Constructor throws if size argument is less than zero |
165 |
|
*/ |
166 |
|
public void testConstructor1() { |
167 |
|
try { |
168 |
|
new ForkJoinPool(-1); |
169 |
|
shouldThrow(); |
170 |
|
} |
171 |
< |
catch (IllegalArgumentException success){} |
171 |
> |
catch (IllegalArgumentException success) {} |
172 |
|
} |
173 |
|
|
174 |
< |
/** |
175 |
< |
* Constructor throws if factory argument is null |
174 |
> |
/** |
175 |
> |
* Constructor throws if factory argument is null |
176 |
|
*/ |
177 |
|
public void testConstructor2() { |
178 |
|
try { |
179 |
|
new ForkJoinPool(1, null); |
180 |
|
shouldThrow(); |
181 |
|
} |
182 |
< |
catch (NullPointerException success){} |
182 |
> |
catch (NullPointerException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
< |
/** |
187 |
< |
* getParallelism returns size set in constructor |
186 |
> |
/** |
187 |
> |
* getParallelism returns size set in constructor |
188 |
|
*/ |
189 |
|
public void testGetParallelism() { |
190 |
|
ForkJoinPool p = null; |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
< |
/** |
199 |
> |
/** |
200 |
|
* setParallelism changes reported parallelism level. |
201 |
|
*/ |
202 |
|
public void testSetParallelism() { |
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
< |
/** |
214 |
> |
/** |
215 |
|
* setParallelism with argument <= 0 throws exception |
216 |
|
*/ |
217 |
|
public void testSetParallelism2() { |
221 |
|
assertTrue(p.getParallelism() == 1); |
222 |
|
p.setParallelism(-2); |
223 |
|
shouldThrow(); |
224 |
< |
}catch (IllegalArgumentException success){ |
224 |
> |
} catch (IllegalArgumentException success) { |
225 |
|
} finally { |
226 |
|
joinPool(p); |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
< |
/** |
230 |
> |
/** |
231 |
|
* getPoolSize returns number of started workers. |
232 |
|
*/ |
233 |
|
public void testGetPoolSize() { |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
< |
/** |
246 |
> |
/** |
247 |
|
* setMaximumPoolSize changes size reported by getMaximumPoolSize. |
248 |
|
*/ |
249 |
|
public void testSetMaximumPoolSize() { |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
< |
/** |
260 |
> |
/** |
261 |
|
* setMaximumPoolSize with argument <= 0 throws exception |
262 |
|
*/ |
263 |
|
public void testSetMaximumPoolSize2() { |
266 |
|
p = new ForkJoinPool(1); |
267 |
|
p.setMaximumPoolSize(-2); |
268 |
|
shouldThrow(); |
269 |
< |
}catch (IllegalArgumentException success){ |
269 |
> |
} catch (IllegalArgumentException success) { |
270 |
|
} finally { |
271 |
|
joinPool(p); |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
< |
/** |
275 |
> |
/** |
276 |
|
* setMaintainsParallelism changes policy reported by |
277 |
|
* getMaintainsParallelism. |
278 |
|
*/ |
286 |
|
joinPool(p); |
287 |
|
} |
288 |
|
} |
289 |
< |
|
290 |
< |
/** |
289 |
> |
|
290 |
> |
/** |
291 |
|
* setAsyncMode changes policy reported by |
292 |
|
* getAsyncMode. |
293 |
|
*/ |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
< |
/** |
305 |
> |
/** |
306 |
|
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
307 |
|
* |
308 |
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
318 |
|
p.execute(new FailingTask()); |
319 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
320 |
|
assertTrue(eh.catches > 0); |
321 |
< |
} catch(InterruptedException e){ |
321 |
> |
} catch (InterruptedException e) { |
322 |
|
unexpectedException(); |
323 |
|
} finally { |
324 |
|
joinPool(p); |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
< |
/** |
328 |
> |
/** |
329 |
|
* setUncaughtExceptionHandler of null removes handler |
330 |
|
*/ |
331 |
|
public void testSetUncaughtExceptionHandler2() { |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
< |
/** |
343 |
> |
/** |
344 |
|
* After invoking a single task, isQuiescent is true, |
345 |
|
* queues are empty, threads are not active, and |
346 |
|
* construction parameters continue to hold |
362 |
|
assertFalse(p.isShutdown()); |
363 |
|
assertFalse(p.isTerminating()); |
364 |
|
assertFalse(p.isTerminated()); |
365 |
< |
} catch(InterruptedException e){ |
365 |
> |
} catch (InterruptedException e) { |
366 |
|
unexpectedException(); |
367 |
|
} finally { |
368 |
|
joinPool(p); |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
< |
|
471 |
> |
|
472 |
|
// FJ Versions of AbstractExecutorService tests |
473 |
|
|
474 |
|
/** |
558 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
559 |
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
560 |
|
Policy.setPolicy(policy); |
561 |
< |
} catch(AccessControlException ok) { |
561 |
> |
} catch (AccessControlException ok) { |
562 |
|
return; |
563 |
|
} |
564 |
|
try { |
580 |
|
finally { |
581 |
|
try { |
582 |
|
Policy.setPolicy(savedPolicy); |
583 |
< |
} catch(AccessControlException ok) { |
583 |
> |
} catch (AccessControlException ok) { |
584 |
|
return; |
585 |
|
} |
586 |
|
} |
597 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
598 |
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
599 |
|
Policy.setPolicy(policy); |
600 |
< |
} catch(AccessControlException ok) { |
600 |
> |
} catch (AccessControlException ok) { |
601 |
|
return; |
602 |
|
} |
603 |
|
|
633 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
634 |
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
635 |
|
Policy.setPolicy(policy); |
636 |
< |
} catch(AccessControlException ok) { |
636 |
> |
} catch (AccessControlException ok) { |
637 |
|
return; |
638 |
|
} |
639 |
|
|
708 |
|
try { |
709 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
710 |
|
shouldThrow(); |
711 |
< |
} catch(InterruptedException e){ |
711 |
> |
} catch (InterruptedException e) { |
712 |
|
} |
713 |
|
return null; |
714 |
|
} |
715 |
|
}).get(); |
716 |
< |
} catch(InterruptedException success){ |
717 |
< |
} catch(Exception e) { |
716 |
> |
} catch (InterruptedException success) { |
717 |
> |
} catch (Exception e) { |
718 |
|
unexpectedException(); |
719 |
|
} |
720 |
|
|
724 |
|
t.start(); |
725 |
|
Thread.sleep(SHORT_DELAY_MS); |
726 |
|
t.interrupt(); |
727 |
< |
} catch(Exception e){ |
727 |
> |
} catch (Exception e) { |
728 |
|
unexpectedException(); |
729 |
|
} |
730 |
|
joinPool(p); |
745 |
|
} |
746 |
|
}; |
747 |
|
|
748 |
< |
for(int i =0; i < 5; i++){ |
748 |
> |
for (int i = 0; i < 5; i++) { |
749 |
|
p.submit(c).get(); |
750 |
|
} |
751 |
|
|
752 |
|
shouldThrow(); |
753 |
< |
} |
754 |
< |
catch(ExecutionException success){ |
753 |
> |
} catch (ExecutionException success) { |
754 |
|
} catch (CancellationException success) { |
755 |
< |
} catch(Exception e) { |
755 |
> |
} catch (Exception e) { |
756 |
|
unexpectedException(); |
757 |
|
} |
758 |
|
joinPool(p); |
766 |
|
try { |
767 |
|
e.invokeAny(null); |
768 |
|
} catch (NullPointerException success) { |
769 |
< |
} catch(Exception ex) { |
769 |
> |
} catch (Exception ex) { |
770 |
|
unexpectedException(); |
771 |
|
} finally { |
772 |
|
joinPool(e); |
781 |
|
try { |
782 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
783 |
|
} catch (IllegalArgumentException success) { |
784 |
< |
} catch(Exception ex) { |
784 |
> |
} catch (Exception ex) { |
785 |
|
unexpectedException(); |
786 |
|
} finally { |
787 |
|
joinPool(e); |
799 |
|
l.add(null); |
800 |
|
e.invokeAny(l); |
801 |
|
} catch (NullPointerException success) { |
802 |
< |
} catch(Exception ex) { |
802 |
> |
} catch (Exception ex) { |
803 |
|
ex.printStackTrace(); |
804 |
|
unexpectedException(); |
805 |
|
} finally { |
816 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
817 |
|
l.add(new NPETask()); |
818 |
|
e.invokeAny(l); |
819 |
< |
} catch(ExecutionException success) { |
819 |
> |
} catch (ExecutionException success) { |
820 |
|
} catch (CancellationException success) { |
821 |
< |
} catch(Exception ex) { |
821 |
> |
} catch (Exception ex) { |
822 |
|
unexpectedException(); |
823 |
|
} finally { |
824 |
|
joinPool(e); |
838 |
|
assertSame(TEST_STRING, result); |
839 |
|
} catch (ExecutionException success) { |
840 |
|
} catch (CancellationException success) { |
841 |
< |
} catch(Exception ex) { |
841 |
> |
} catch (Exception ex) { |
842 |
|
unexpectedException(); |
843 |
|
} finally { |
844 |
|
joinPool(e); |
853 |
|
try { |
854 |
|
e.invokeAll(null); |
855 |
|
} catch (NullPointerException success) { |
856 |
< |
} catch(Exception ex) { |
856 |
> |
} catch (Exception ex) { |
857 |
|
unexpectedException(); |
858 |
|
} finally { |
859 |
|
joinPool(e); |
868 |
|
try { |
869 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
870 |
|
assertTrue(r.isEmpty()); |
871 |
< |
} catch(Exception ex) { |
871 |
> |
} catch (Exception ex) { |
872 |
|
unexpectedException(); |
873 |
|
} finally { |
874 |
|
joinPool(e); |
886 |
|
l.add(null); |
887 |
|
e.invokeAll(l); |
888 |
|
} catch (NullPointerException success) { |
889 |
< |
} catch(Exception ex) { |
889 |
> |
} catch (Exception ex) { |
890 |
|
unexpectedException(); |
891 |
|
} finally { |
892 |
|
joinPool(e); |
903 |
|
l.add(new NPETask()); |
904 |
|
List<Future<String>> result = e.invokeAll(l); |
905 |
|
assertEquals(1, result.size()); |
906 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
906 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
907 |
|
it.next().get(); |
908 |
< |
} catch(ExecutionException success) { |
908 |
> |
} catch (ExecutionException success) { |
909 |
|
} catch (CancellationException success) { |
910 |
< |
} catch(Exception ex) { |
910 |
> |
} catch (Exception ex) { |
911 |
|
ex.printStackTrace(); |
912 |
|
unexpectedException(); |
913 |
|
} finally { |
926 |
|
l.add(new StringTask()); |
927 |
|
List<Future<String>> result = e.invokeAll(l); |
928 |
|
assertEquals(2, result.size()); |
929 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
929 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
930 |
|
assertSame(TEST_STRING, it.next().get()); |
931 |
|
} catch (ExecutionException success) { |
932 |
|
} catch (CancellationException success) { |
933 |
< |
} catch(Exception ex) { |
933 |
> |
} catch (Exception ex) { |
934 |
|
ex.printStackTrace(); |
935 |
|
unexpectedException(); |
936 |
|
} finally { |
947 |
|
try { |
948 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
949 |
|
} catch (NullPointerException success) { |
950 |
< |
} catch(Exception ex) { |
950 |
> |
} catch (Exception ex) { |
951 |
|
ex.printStackTrace(); |
952 |
|
unexpectedException(); |
953 |
|
} finally { |
965 |
|
l.add(new StringTask()); |
966 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
967 |
|
} catch (NullPointerException success) { |
968 |
< |
} catch(Exception ex) { |
968 |
> |
} catch (Exception ex) { |
969 |
|
ex.printStackTrace(); |
970 |
|
unexpectedException(); |
971 |
|
} finally { |
981 |
|
try { |
982 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
983 |
|
} catch (IllegalArgumentException success) { |
984 |
< |
} catch(Exception ex) { |
984 |
> |
} catch (Exception ex) { |
985 |
|
ex.printStackTrace(); |
986 |
|
unexpectedException(); |
987 |
|
} finally { |
1000 |
|
l.add(null); |
1001 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1002 |
|
} catch (NullPointerException success) { |
1003 |
< |
} catch(Exception ex) { |
1003 |
> |
} catch (Exception ex) { |
1004 |
|
ex.printStackTrace(); |
1005 |
|
unexpectedException(); |
1006 |
|
} finally { |
1017 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1018 |
|
l.add(new NPETask()); |
1019 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1020 |
< |
} catch(ExecutionException success) { |
1020 |
> |
} catch (ExecutionException success) { |
1021 |
|
} catch (CancellationException success) { |
1022 |
< |
} catch(Exception ex) { |
1022 |
> |
} catch (Exception ex) { |
1023 |
|
ex.printStackTrace(); |
1024 |
|
unexpectedException(); |
1025 |
|
} finally { |
1040 |
|
assertSame(TEST_STRING, result); |
1041 |
|
} catch (ExecutionException success) { |
1042 |
|
} catch (CancellationException success) { |
1043 |
< |
} catch(Exception ex) { |
1043 |
> |
} catch (Exception ex) { |
1044 |
|
ex.printStackTrace(); |
1045 |
|
unexpectedException(); |
1046 |
|
} finally { |
1056 |
|
try { |
1057 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1058 |
|
} catch (NullPointerException success) { |
1059 |
< |
} catch(Exception ex) { |
1059 |
> |
} catch (Exception ex) { |
1060 |
|
ex.printStackTrace(); |
1061 |
|
unexpectedException(); |
1062 |
|
} finally { |
1074 |
|
l.add(new StringTask()); |
1075 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1076 |
|
} catch (NullPointerException success) { |
1077 |
< |
} catch(Exception ex) { |
1077 |
> |
} catch (Exception ex) { |
1078 |
|
ex.printStackTrace(); |
1079 |
|
unexpectedException(); |
1080 |
|
} finally { |
1090 |
|
try { |
1091 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1092 |
|
assertTrue(r.isEmpty()); |
1093 |
< |
} catch(Exception ex) { |
1093 |
> |
} catch (Exception ex) { |
1094 |
|
ex.printStackTrace(); |
1095 |
|
unexpectedException(); |
1096 |
|
} finally { |
1109 |
|
l.add(null); |
1110 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1111 |
|
} catch (NullPointerException success) { |
1112 |
< |
} catch(Exception ex) { |
1112 |
> |
} catch (Exception ex) { |
1113 |
|
ex.printStackTrace(); |
1114 |
|
unexpectedException(); |
1115 |
|
} finally { |
1127 |
|
l.add(new NPETask()); |
1128 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1129 |
|
assertEquals(1, result.size()); |
1130 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1130 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1131 |
|
it.next().get(); |
1132 |
< |
} catch(ExecutionException success) { |
1132 |
> |
} catch (ExecutionException success) { |
1133 |
|
} catch (CancellationException success) { |
1134 |
< |
} catch(Exception ex) { |
1134 |
> |
} catch (Exception ex) { |
1135 |
|
ex.printStackTrace(); |
1136 |
|
unexpectedException(); |
1137 |
|
} finally { |
1150 |
|
l.add(new StringTask()); |
1151 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1152 |
|
assertEquals(2, result.size()); |
1153 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1153 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1154 |
|
assertSame(TEST_STRING, it.next().get()); |
1155 |
|
} catch (ExecutionException success) { |
1156 |
|
} catch (CancellationException success) { |
1157 |
< |
} catch(Exception ex) { |
1157 |
> |
} catch (Exception ex) { |
1158 |
|
ex.printStackTrace(); |
1159 |
|
unexpectedException(); |
1160 |
|
} finally { |