37 |
|
import java.util.concurrent.Callable; |
38 |
|
import java.util.concurrent.CountDownLatch; |
39 |
|
import java.util.concurrent.CyclicBarrier; |
40 |
+ |
import java.util.concurrent.ExecutionException; |
41 |
+ |
import java.util.concurrent.Executors; |
42 |
|
import java.util.concurrent.ExecutorService; |
43 |
|
import java.util.concurrent.Future; |
44 |
|
import java.util.concurrent.RecursiveAction; |
283 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
284 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
285 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
286 |
< |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
286 |
> |
public static boolean atLeastJava9() { |
287 |
> |
return JAVA_CLASS_VERSION >= 53.0 |
288 |
> |
// As of 2015-09, java9 still uses 52.0 class file version |
289 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
290 |
> |
} |
291 |
> |
public static boolean atLeastJava10() { |
292 |
> |
return JAVA_CLASS_VERSION >= 54.0 |
293 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
294 |
> |
} |
295 |
|
|
296 |
|
/** |
297 |
|
* Collects all JSR166 unit tests as one suite. |
387 |
|
// Java9+ test classes |
388 |
|
if (atLeastJava9()) { |
389 |
|
String[] java9TestClassNames = { |
390 |
< |
"ThreadPoolExecutor9Test", |
390 |
> |
// Currently empty |
391 |
|
}; |
392 |
|
addNamedTestClasses(suite, java9TestClassNames); |
393 |
|
} |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* Returns a new Date instance representing a time delayMillis |
495 |
< |
* milliseconds in the future. |
494 |
> |
* Returns a new Date instance representing a time at least |
495 |
> |
* delayMillis milliseconds in the future. |
496 |
|
*/ |
497 |
|
Date delayedDate(long delayMillis) { |
498 |
< |
return new Date(System.currentTimeMillis() + delayMillis); |
498 |
> |
// Add 1 because currentTimeMillis is known to round into the past. |
499 |
> |
return new Date(System.currentTimeMillis() + delayMillis + 1); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
729 |
|
/** |
730 |
|
* Waits out termination of a thread pool or fails doing so. |
731 |
|
*/ |
732 |
< |
void joinPool(ExecutorService exec) { |
732 |
> |
void joinPool(ExecutorService pool) { |
733 |
|
try { |
734 |
< |
exec.shutdown(); |
735 |
< |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
736 |
< |
fail("ExecutorService " + exec + |
734 |
> |
pool.shutdown(); |
735 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
736 |
> |
fail("ExecutorService " + pool + |
737 |
|
" did not terminate in a timely manner"); |
738 |
|
} catch (SecurityException ok) { |
739 |
|
// Allowed in case test doesn't have privs |
742 |
|
} |
743 |
|
} |
744 |
|
|
745 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
746 |
+ |
interface Thunk { public void run() throws Throwable; } |
747 |
+ |
|
748 |
+ |
/** |
749 |
+ |
* Runs all the given tasks in parallel, failing if any fail. |
750 |
+ |
* Useful for running multiple variants of tests that are |
751 |
+ |
* necessarily individually slow because they must block. |
752 |
+ |
*/ |
753 |
+ |
void testInParallel(Thunk ... thunks) { |
754 |
+ |
ExecutorService pool = Executors.newCachedThreadPool(); |
755 |
+ |
try { |
756 |
+ |
ArrayList<Future<?>> futures = new ArrayList<>(thunks.length); |
757 |
+ |
for (final Thunk thunk : thunks) |
758 |
+ |
futures.add(pool.submit(new CheckedRunnable() { |
759 |
+ |
public void realRun() throws Throwable { thunk.run();}})); |
760 |
+ |
for (Future<?> future : futures) |
761 |
+ |
try { |
762 |
+ |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
763 |
+ |
} catch (ExecutionException ex) { |
764 |
+ |
threadUnexpectedException(ex.getCause()); |
765 |
+ |
} catch (Exception ex) { |
766 |
+ |
threadUnexpectedException(ex); |
767 |
+ |
} |
768 |
+ |
} finally { |
769 |
+ |
joinPool(pool); |
770 |
+ |
} |
771 |
+ |
} |
772 |
+ |
|
773 |
|
/** |
774 |
|
* A debugging tool to print all stack traces, as jstack does. |
775 |
|
*/ |