5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
8 |
> |
import java.util.ArrayList; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.List; |
11 |
|
import java.util.concurrent.Executors; |
12 |
|
import java.util.concurrent.ExecutorService; |
13 |
|
import java.util.concurrent.AbstractExecutorService; |
23 |
|
import java.util.concurrent.RecursiveTask; |
24 |
|
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.locks.ReentrantLock; |
26 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
27 |
|
import java.security.AccessControlException; |
28 |
|
import java.security.Policy; |
29 |
|
import java.security.PrivilegedAction; |
162 |
|
public void testDefaultInitialState() { |
163 |
|
ForkJoinPool p = new ForkJoinPool(1); |
164 |
|
try { |
165 |
< |
assertTrue(p.getFactory() == |
166 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
165 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
166 |
> |
p.getFactory()); |
167 |
|
assertTrue(p.isQuiescent()); |
168 |
|
assertFalse(p.getAsyncMode()); |
169 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
170 |
< |
assertTrue(p.getStealCount() == 0); |
171 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
172 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
169 |
> |
assertEquals(0, p.getActiveThreadCount()); |
170 |
> |
assertEquals(0, p.getStealCount()); |
171 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
172 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
173 |
|
assertFalse(p.hasQueuedSubmissions()); |
174 |
|
assertFalse(p.isShutdown()); |
175 |
|
assertFalse(p.isTerminating()); |
206 |
|
public void testGetParallelism() { |
207 |
|
ForkJoinPool p = new ForkJoinPool(1); |
208 |
|
try { |
209 |
< |
assertTrue(p.getParallelism() == 1); |
209 |
> |
assertEquals(1, p.getParallelism()); |
210 |
|
} finally { |
211 |
|
joinPool(p); |
212 |
|
} |
218 |
|
public void testGetPoolSize() { |
219 |
|
ForkJoinPool p = new ForkJoinPool(1); |
220 |
|
try { |
221 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
221 |
> |
assertEquals(0, p.getActiveThreadCount()); |
222 |
|
Future<String> future = p.submit(new StringTask()); |
223 |
< |
assertTrue(p.getPoolSize() == 1); |
223 |
> |
assertEquals(1, p.getPoolSize()); |
224 |
|
} finally { |
225 |
|
joinPool(p); |
226 |
|
} |
233 |
|
* performs its defined action |
234 |
|
*/ |
235 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
236 |
< |
MyHandler eh = new MyHandler(); |
237 |
< |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
238 |
< |
try { |
239 |
< |
assert(eh == p.getUncaughtExceptionHandler()); |
240 |
< |
p.execute(new FailingTask()); |
241 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
242 |
< |
assertTrue(eh.catches > 0); |
236 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
237 |
> |
final Thread.UncaughtExceptionHandler eh = |
238 |
> |
new Thread.UncaughtExceptionHandler() { |
239 |
> |
public void uncaughtException(Thread t, Throwable e) { |
240 |
> |
uehInvoked.countDown(); |
241 |
> |
}}; |
242 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
243 |
> |
eh, false); |
244 |
> |
try { |
245 |
> |
assertSame(eh, p.getUncaughtExceptionHandler()); |
246 |
> |
p.execute(new FibTask(8)); |
247 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
248 |
|
} finally { |
249 |
< |
p.shutdownNow(); |
249 |
> |
p.shutdownNow(); // failure might have prevented processing task |
250 |
|
joinPool(p); |
251 |
|
} |
252 |
|
} |
259 |
|
public void testisQuiescent() throws InterruptedException { |
260 |
|
ForkJoinPool p = new ForkJoinPool(2); |
261 |
|
try { |
262 |
+ |
assertTrue(p.isQuiescent()); |
263 |
|
p.invoke(new FibTask(20)); |
264 |
< |
assertTrue(p.getFactory() == |
265 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
266 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
264 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
265 |
> |
p.getFactory()); |
266 |
> |
Thread.sleep(SMALL_DELAY_MS); |
267 |
|
assertTrue(p.isQuiescent()); |
268 |
|
assertFalse(p.getAsyncMode()); |
269 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
270 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
271 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
269 |
> |
assertEquals(0, p.getActiveThreadCount()); |
270 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
271 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
272 |
|
assertFalse(p.hasQueuedSubmissions()); |
273 |
|
assertFalse(p.isShutdown()); |
274 |
|
assertFalse(p.isTerminating()); |
285 |
|
ForkJoinPool p = new ForkJoinPool(1); |
286 |
|
try { |
287 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
288 |
< |
int r = f.get(); |
280 |
< |
assertTrue(r == 21); |
288 |
> |
assertEquals(21, (int) f.get()); |
289 |
|
} finally { |
290 |
|
joinPool(p); |
291 |
|
} |
299 |
|
try { |
300 |
|
p.shutdown(); |
301 |
|
assertTrue(p.isShutdown()); |
302 |
< |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
303 |
< |
shouldThrow(); |
304 |
< |
} catch (RejectedExecutionException success) { |
302 |
> |
try { |
303 |
> |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
304 |
> |
shouldThrow(); |
305 |
> |
} catch (RejectedExecutionException success) {} |
306 |
|
} finally { |
307 |
|
joinPool(p); |
308 |
|
} |
318 |
|
ManagedLocker locker = new ManagedLocker(lock); |
319 |
|
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
320 |
|
p.execute(f); |
321 |
< |
int r = f.get(); |
313 |
< |
assertTrue(r == 832040); |
321 |
> |
assertEquals(832040, (int) f.get()); |
322 |
|
} finally { |
323 |
|
p.shutdownNow(); // don't wait out shutdown |
324 |
|
} |
531 |
|
*/ |
532 |
|
public void testExecuteNullRunnable() { |
533 |
|
ExecutorService e = new ForkJoinPool(1); |
534 |
+ |
TrackedShortRunnable task = null; |
535 |
|
try { |
527 |
– |
TrackedShortRunnable task = null; |
536 |
|
Future<?> future = e.submit(task); |
537 |
|
shouldThrow(); |
538 |
|
} catch (NullPointerException success) { |
547 |
|
*/ |
548 |
|
public void testSubmitNullCallable() { |
549 |
|
ExecutorService e = new ForkJoinPool(1); |
550 |
+ |
StringTask t = null; |
551 |
|
try { |
543 |
– |
StringTask t = null; |
552 |
|
Future<String> future = e.submit(t); |
553 |
|
shouldThrow(); |
554 |
|
} catch (NullPointerException success) { |
559 |
|
|
560 |
|
|
561 |
|
/** |
562 |
< |
* Blocking on submit(callable) throws InterruptedException if |
555 |
< |
* caller interrupted. |
562 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
563 |
|
*/ |
564 |
|
public void testInterruptedSubmit() throws InterruptedException { |
565 |
< |
final ForkJoinPool p = new ForkJoinPool(1); |
566 |
< |
|
567 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
568 |
< |
public void realRun() throws Throwable { |
569 |
< |
p.submit(new CheckedCallable<Object>() { |
570 |
< |
public Object realCall() throws Throwable { |
571 |
< |
try { |
572 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
573 |
< |
} catch (InterruptedException ok) { |
574 |
< |
} |
575 |
< |
return null; |
576 |
< |
}}).get(); |
577 |
< |
}}); |
578 |
< |
|
579 |
< |
t.start(); |
580 |
< |
Thread.sleep(SHORT_DELAY_MS); |
581 |
< |
t.interrupt(); |
582 |
< |
t.join(); |
583 |
< |
p.shutdownNow(); |
584 |
< |
joinPool(p); |
565 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
566 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
567 |
> |
final ExecutorService p = new ForkJoinPool(1); |
568 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
569 |
> |
public Void realCall() throws InterruptedException { |
570 |
> |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
571 |
> |
return null; |
572 |
> |
}}; |
573 |
> |
try { |
574 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
575 |
> |
public void realRun() throws Exception { |
576 |
> |
Future<Void> future = p.submit(awaiter); |
577 |
> |
submitted.countDown(); |
578 |
> |
future.get(); |
579 |
> |
}}); |
580 |
> |
t.start(); |
581 |
> |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
582 |
> |
t.interrupt(); |
583 |
> |
t.join(); |
584 |
> |
} finally { |
585 |
> |
quittingTime.countDown(); |
586 |
> |
joinPool(p); |
587 |
> |
} |
588 |
|
} |
589 |
|
|
590 |
|
/** |
793 |
|
public void testTimedInvokeAny1() throws Throwable { |
794 |
|
ExecutorService e = new ForkJoinPool(1); |
795 |
|
try { |
796 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
796 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
797 |
|
shouldThrow(); |
798 |
|
} catch (NullPointerException success) { |
799 |
|
} finally { |
824 |
|
ExecutorService e = new ForkJoinPool(1); |
825 |
|
try { |
826 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
827 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
827 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
828 |
|
shouldThrow(); |
829 |
|
} catch (IllegalArgumentException success) { |
830 |
|
} finally { |
842 |
|
l.add(latchAwaitingStringTask(latch)); |
843 |
|
l.add(null); |
844 |
|
try { |
845 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
845 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
846 |
|
shouldThrow(); |
847 |
|
} catch (NullPointerException success) { |
848 |
|
} finally { |
859 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
860 |
|
l.add(new NPETask()); |
861 |
|
try { |
862 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
862 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
863 |
|
shouldThrow(); |
864 |
|
} catch (ExecutionException success) { |
865 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
877 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
878 |
|
l.add(new StringTask()); |
879 |
|
l.add(new StringTask()); |
880 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
880 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
881 |
|
assertSame(TEST_STRING, result); |
882 |
|
} finally { |
883 |
|
joinPool(e); |
890 |
|
public void testTimedInvokeAll1() throws Throwable { |
891 |
|
ExecutorService e = new ForkJoinPool(1); |
892 |
|
try { |
893 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
893 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
894 |
|
shouldThrow(); |
895 |
|
} catch (NullPointerException success) { |
896 |
|
} finally { |
922 |
|
try { |
923 |
|
List<Future<String>> r |
924 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
925 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
925 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
926 |
|
assertTrue(r.isEmpty()); |
927 |
|
} finally { |
928 |
|
joinPool(e); |
938 |
|
l.add(new StringTask()); |
939 |
|
l.add(null); |
940 |
|
try { |
941 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
941 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
942 |
|
shouldThrow(); |
943 |
|
} catch (NullPointerException success) { |
944 |
|
} finally { |
954 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
955 |
|
l.add(new NPETask()); |
956 |
|
List<Future<String>> futures |
957 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
957 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
958 |
|
assertEquals(1, futures.size()); |
959 |
|
try { |
960 |
|
futures.get(0).get(); |
976 |
|
l.add(new StringTask()); |
977 |
|
l.add(new StringTask()); |
978 |
|
List<Future<String>> futures |
979 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
979 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
980 |
|
assertEquals(2, futures.size()); |
981 |
|
for (Future<String> future : futures) |
982 |
|
assertSame(TEST_STRING, future.get()); |