64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
+ |
static class MyError extends Error {} |
68 |
+ |
|
69 |
|
// to test handlers |
70 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
71 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
72 |
< |
protected void onStart() { super.onStart(); throw new Error(); } |
72 |
> |
protected void onStart() { super.onStart(); throw new MyError(); } |
73 |
|
} |
74 |
|
|
75 |
|
static class FailingThreadFactory |
213 |
|
* getPoolSize returns number of started workers. |
214 |
|
*/ |
215 |
|
public void testGetPoolSize() { |
216 |
< |
ForkJoinPool p = new ForkJoinPool(1); |
216 |
> |
final CountDownLatch taskStarted = new CountDownLatch(1); |
217 |
> |
final CountDownLatch done = new CountDownLatch(1); |
218 |
> |
final ForkJoinPool p = new ForkJoinPool(1); |
219 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
220 |
|
assertEquals(0, p.getActiveThreadCount()); |
221 |
< |
Future<String> future = p.submit(new StringTask()); |
221 |
> |
final Runnable task = new CheckedRunnable() { |
222 |
> |
public void realRun() throws InterruptedException { |
223 |
> |
taskStarted.countDown(); |
224 |
> |
assertEquals(1, p.getPoolSize()); |
225 |
> |
assertEquals(1, p.getActiveThreadCount()); |
226 |
> |
done.await(); |
227 |
> |
}}; |
228 |
> |
Future<?> future = p.submit(task); |
229 |
> |
await(taskStarted); |
230 |
|
assertEquals(1, p.getPoolSize()); |
231 |
+ |
assertEquals(1, p.getActiveThreadCount()); |
232 |
+ |
done.countDown(); |
233 |
|
} |
234 |
+ |
assertEquals(0, p.getPoolSize()); |
235 |
+ |
assertEquals(0, p.getActiveThreadCount()); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
275 |
|
final Thread.UncaughtExceptionHandler ueh = |
276 |
|
new Thread.UncaughtExceptionHandler() { |
277 |
|
public void uncaughtException(Thread t, Throwable e) { |
278 |
+ |
threadAssertTrue(e instanceof MyError); |
279 |
+ |
threadAssertTrue(t instanceof FailingFJWSubclass); |
280 |
|
uehInvoked.countDown(); |
281 |
|
}}; |
282 |
|
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
284 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
285 |
|
assertSame(ueh, p.getUncaughtExceptionHandler()); |
286 |
|
try { |
287 |
< |
try { |
288 |
< |
p.execute(new FibTask(8)); |
271 |
< |
await(uehInvoked); |
272 |
< |
} catch (RejectedExecutionException ok) {} |
287 |
> |
p.execute(new FibTask(8)); |
288 |
> |
await(uehInvoked); |
289 |
|
} finally { |
290 |
|
p.shutdownNow(); // failure might have prevented processing task |
291 |
|
} |
835 |
|
public void testTimedInvokeAny4() throws Throwable { |
836 |
|
ExecutorService e = new ForkJoinPool(1); |
837 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
838 |
+ |
long startTime = System.nanoTime(); |
839 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
840 |
|
l.add(new NPETask()); |
841 |
|
try { |
842 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
842 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
843 |
|
shouldThrow(); |
844 |
|
} catch (ExecutionException success) { |
845 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
846 |
|
} |
847 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
848 |
|
} |
849 |
|
} |
850 |
|
|
854 |
|
public void testTimedInvokeAny5() throws Throwable { |
855 |
|
ExecutorService e = new ForkJoinPool(1); |
856 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
857 |
+ |
long startTime = System.nanoTime(); |
858 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
859 |
|
l.add(new StringTask()); |
860 |
|
l.add(new StringTask()); |
861 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
861 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
862 |
|
assertSame(TEST_STRING, result); |
863 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
864 |
|
} |
865 |
|
} |
866 |
|
|