1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
2 |
> |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
> |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
> |
* domain, as explained at |
5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
* Pat Fisher, Mike Judd. |
8 |
|
*/ |
9 |
|
|
10 |
+ |
/* |
11 |
+ |
* @test |
12 |
+ |
* @summary JSR-166 tck tests, in a number of variations. |
13 |
+ |
* The first is the conformance testing variant, |
14 |
+ |
* while others also test implementation details. |
15 |
+ |
* @build * |
16 |
+ |
* @modules java.management |
17 |
+ |
* @run junit/othervm/timeout=1000 JSR166TestCase |
18 |
+ |
* @run junit/othervm/timeout=1000 |
19 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
20 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
21 |
+ |
* -Djsr166.testImplementationDetails=true |
22 |
+ |
* JSR166TestCase |
23 |
+ |
* @run junit/othervm/timeout=1000 |
24 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
25 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
26 |
+ |
* -Djsr166.testImplementationDetails=true |
27 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 |
28 |
+ |
* JSR166TestCase |
29 |
+ |
* @run junit/othervm/timeout=1000 |
30 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
31 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
32 |
+ |
* -Djsr166.testImplementationDetails=true |
33 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 |
34 |
+ |
* -Djava.util.secureRandomSeed=true |
35 |
+ |
* JSR166TestCase |
36 |
+ |
* @run junit/othervm/timeout=1000/policy=tck.policy |
37 |
+ |
* --add-opens java.base/java.util.concurrent=ALL-UNNAMED |
38 |
+ |
* --add-opens java.base/java.lang=ALL-UNNAMED |
39 |
+ |
* -Djsr166.testImplementationDetails=true |
40 |
+ |
* JSR166TestCase |
41 |
+ |
*/ |
42 |
+ |
|
43 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
44 |
|
import static java.util.concurrent.TimeUnit.MINUTES; |
45 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
54 |
|
import java.lang.reflect.Constructor; |
55 |
|
import java.lang.reflect.Method; |
56 |
|
import java.lang.reflect.Modifier; |
23 |
– |
import java.nio.file.Files; |
24 |
– |
import java.nio.file.Paths; |
57 |
|
import java.security.CodeSource; |
58 |
|
import java.security.Permission; |
59 |
|
import java.security.PermissionCollection; |
63 |
|
import java.security.SecurityPermission; |
64 |
|
import java.util.ArrayList; |
65 |
|
import java.util.Arrays; |
66 |
+ |
import java.util.Collection; |
67 |
+ |
import java.util.Collections; |
68 |
|
import java.util.Date; |
69 |
|
import java.util.Enumeration; |
70 |
|
import java.util.Iterator; |
76 |
|
import java.util.concurrent.CountDownLatch; |
77 |
|
import java.util.concurrent.CyclicBarrier; |
78 |
|
import java.util.concurrent.ExecutionException; |
79 |
+ |
import java.util.concurrent.Executor; |
80 |
|
import java.util.concurrent.Executors; |
81 |
|
import java.util.concurrent.ExecutorService; |
82 |
|
import java.util.concurrent.ForkJoinPool; |
83 |
|
import java.util.concurrent.Future; |
84 |
+ |
import java.util.concurrent.FutureTask; |
85 |
|
import java.util.concurrent.RecursiveAction; |
86 |
|
import java.util.concurrent.RecursiveTask; |
87 |
+ |
import java.util.concurrent.RejectedExecutionException; |
88 |
|
import java.util.concurrent.RejectedExecutionHandler; |
89 |
|
import java.util.concurrent.Semaphore; |
90 |
+ |
import java.util.concurrent.ScheduledExecutorService; |
91 |
+ |
import java.util.concurrent.ScheduledFuture; |
92 |
+ |
import java.util.concurrent.SynchronousQueue; |
93 |
|
import java.util.concurrent.ThreadFactory; |
94 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
95 |
|
import java.util.concurrent.ThreadPoolExecutor; |
96 |
+ |
import java.util.concurrent.TimeUnit; |
97 |
|
import java.util.concurrent.TimeoutException; |
98 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
99 |
|
import java.util.concurrent.atomic.AtomicReference; |
57 |
– |
import java.util.regex.Matcher; |
100 |
|
import java.util.regex.Pattern; |
101 |
|
|
60 |
– |
import junit.framework.AssertionFailedError; |
102 |
|
import junit.framework.Test; |
103 |
|
import junit.framework.TestCase; |
104 |
|
import junit.framework.TestResult; |
217 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
218 |
|
|
219 |
|
/** |
220 |
< |
* The scaling factor to apply to standard delays used in tests. |
220 |
> |
* Returns the value of the system property, or NaN if not defined. |
221 |
|
*/ |
222 |
< |
private static final int delayFactor = |
223 |
< |
Integer.getInteger("jsr166.delay.factor", 1); |
222 |
> |
private static float systemPropertyValue(String name) { |
223 |
> |
String floatString = System.getProperty(name); |
224 |
> |
if (floatString == null) |
225 |
> |
return Float.NaN; |
226 |
> |
try { |
227 |
> |
return Float.parseFloat(floatString); |
228 |
> |
} catch (NumberFormatException ex) { |
229 |
> |
throw new IllegalArgumentException( |
230 |
> |
String.format("Bad float value in system property %s=%s", |
231 |
> |
name, floatString)); |
232 |
> |
} |
233 |
> |
} |
234 |
> |
|
235 |
> |
/** |
236 |
> |
* The scaling factor to apply to standard delays used in tests. |
237 |
> |
* May be initialized from any of: |
238 |
> |
* - the "jsr166.delay.factor" system property |
239 |
> |
* - the "test.timeout.factor" system property (as used by jtreg) |
240 |
> |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
241 |
> |
* - hard-coded fuzz factor when using a known slowpoke VM |
242 |
> |
*/ |
243 |
> |
private static final float delayFactor = delayFactor(); |
244 |
> |
|
245 |
> |
private static float delayFactor() { |
246 |
> |
float x; |
247 |
> |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
248 |
> |
return x; |
249 |
> |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
250 |
> |
return x; |
251 |
> |
String prop = System.getProperty("java.vm.version"); |
252 |
> |
if (prop != null && prop.matches(".*debug.*")) |
253 |
> |
return 4.0f; // How much slower is fastdebug than product?! |
254 |
> |
return 1.0f; |
255 |
> |
} |
256 |
|
|
257 |
|
public JSR166TestCase() { super(); } |
258 |
|
public JSR166TestCase(String name) { super(name); } |
305 |
|
|
306 |
|
// public static String cpuModel() { |
307 |
|
// try { |
308 |
< |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
308 |
> |
// java.util.regex.Matcher matcher |
309 |
> |
// = Pattern.compile("model name\\s*: (.*)") |
310 |
|
// .matcher(new String( |
311 |
< |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
311 |
> |
// java.nio.file.Files.readAllBytes( |
312 |
> |
// java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8")); |
313 |
|
// matcher.find(); |
314 |
|
// return matcher.group(1); |
315 |
|
// } catch (Exception ex) { return null; } |
418 |
|
for (String testClassName : testClassNames) { |
419 |
|
try { |
420 |
|
Class<?> testClass = Class.forName(testClassName); |
421 |
< |
Method m = testClass.getDeclaredMethod("suite", |
347 |
< |
new Class<?>[0]); |
421 |
> |
Method m = testClass.getDeclaredMethod("suite"); |
422 |
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
423 |
< |
} catch (Exception e) { |
424 |
< |
throw new Error("Missing test class", e); |
423 |
> |
} catch (ReflectiveOperationException e) { |
424 |
> |
throw new AssertionError("Missing test class", e); |
425 |
|
} |
426 |
|
} |
427 |
|
} |
443 |
|
} |
444 |
|
} |
445 |
|
|
446 |
< |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
447 |
< |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
448 |
< |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
449 |
< |
public static boolean atLeastJava9() { |
450 |
< |
return JAVA_CLASS_VERSION >= 53.0 |
377 |
< |
// As of 2015-09, java9 still uses 52.0 class file version |
378 |
< |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
379 |
< |
} |
380 |
< |
public static boolean atLeastJava10() { |
381 |
< |
return JAVA_CLASS_VERSION >= 54.0 |
382 |
< |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
383 |
< |
} |
446 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
447 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
448 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
449 |
> |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
450 |
> |
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
451 |
|
|
452 |
|
/** |
453 |
|
* Collects all JSR166 unit tests as one suite. |
468 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
469 |
|
ArrayBlockingQueueTest.suite(), |
470 |
|
ArrayDequeTest.suite(), |
471 |
+ |
ArrayListTest.suite(), |
472 |
|
AtomicBooleanTest.suite(), |
473 |
|
AtomicIntegerArrayTest.suite(), |
474 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
491 |
|
CopyOnWriteArrayListTest.suite(), |
492 |
|
CopyOnWriteArraySetTest.suite(), |
493 |
|
CountDownLatchTest.suite(), |
494 |
+ |
CountedCompleterTest.suite(), |
495 |
|
CyclicBarrierTest.suite(), |
496 |
|
DelayQueueTest.suite(), |
497 |
|
EntryTest.suite(), |
520 |
|
TreeMapTest.suite(), |
521 |
|
TreeSetTest.suite(), |
522 |
|
TreeSubMapTest.suite(), |
523 |
< |
TreeSubSetTest.suite()); |
523 |
> |
TreeSubSetTest.suite(), |
524 |
> |
VectorTest.suite()); |
525 |
|
|
526 |
|
// Java8+ test classes |
527 |
|
if (atLeastJava8()) { |
528 |
|
String[] java8TestClassNames = { |
529 |
+ |
"ArrayDeque8Test", |
530 |
|
"Atomic8Test", |
531 |
|
"CompletableFutureTest", |
532 |
|
"ConcurrentHashMap8Test", |
533 |
< |
"CountedCompleterTest", |
533 |
> |
"CountedCompleter8Test", |
534 |
|
"DoubleAccumulatorTest", |
535 |
|
"DoubleAdderTest", |
536 |
|
"ForkJoinPool8Test", |
537 |
|
"ForkJoinTask8Test", |
538 |
+ |
"HashMapTest", |
539 |
+ |
"LinkedBlockingDeque8Test", |
540 |
+ |
"LinkedBlockingQueue8Test", |
541 |
|
"LongAccumulatorTest", |
542 |
|
"LongAdderTest", |
543 |
|
"SplittableRandomTest", |
544 |
|
"StampedLockTest", |
545 |
|
"SubmissionPublisherTest", |
546 |
|
"ThreadLocalRandom8Test", |
547 |
+ |
"TimeUnit8Test", |
548 |
|
}; |
549 |
|
addNamedTestClasses(suite, java8TestClassNames); |
550 |
|
} |
552 |
|
// Java9+ test classes |
553 |
|
if (atLeastJava9()) { |
554 |
|
String[] java9TestClassNames = { |
555 |
< |
// Currently empty, but expecting varhandle tests |
555 |
> |
"AtomicBoolean9Test", |
556 |
> |
"AtomicInteger9Test", |
557 |
> |
"AtomicIntegerArray9Test", |
558 |
> |
"AtomicLong9Test", |
559 |
> |
"AtomicLongArray9Test", |
560 |
> |
"AtomicReference9Test", |
561 |
> |
"AtomicReferenceArray9Test", |
562 |
> |
"ExecutorCompletionService9Test", |
563 |
> |
"ForkJoinPool9Test", |
564 |
|
}; |
565 |
|
addNamedTestClasses(suite, java9TestClassNames); |
566 |
|
} |
571 |
|
/** Returns list of junit-style test method names in given class. */ |
572 |
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
573 |
|
Method[] methods = testClass.getDeclaredMethods(); |
574 |
< |
ArrayList<String> names = new ArrayList<String>(methods.length); |
574 |
> |
ArrayList<String> names = new ArrayList<>(methods.length); |
575 |
|
for (Method method : methods) { |
576 |
|
if (method.getName().startsWith("test") |
577 |
|
&& Modifier.isPublic(method.getModifiers()) |
598 |
|
for (String methodName : testMethodNames(testClass)) |
599 |
|
suite.addTest((Test) c.newInstance(data, methodName)); |
600 |
|
return suite; |
601 |
< |
} catch (Exception e) { |
602 |
< |
throw new Error(e); |
601 |
> |
} catch (ReflectiveOperationException e) { |
602 |
> |
throw new AssertionError(e); |
603 |
|
} |
604 |
|
} |
605 |
|
|
615 |
|
if (atLeastJava8()) { |
616 |
|
String name = testClass.getName(); |
617 |
|
String name8 = name.replaceAll("Test$", "8Test"); |
618 |
< |
if (name.equals(name8)) throw new Error(name); |
618 |
> |
if (name.equals(name8)) throw new AssertionError(name); |
619 |
|
try { |
620 |
|
return (Test) |
621 |
|
Class.forName(name8) |
622 |
< |
.getMethod("testSuite", new Class[] { dataClass }) |
622 |
> |
.getMethod("testSuite", dataClass) |
623 |
|
.invoke(null, data); |
624 |
< |
} catch (Exception e) { |
625 |
< |
throw new Error(e); |
624 |
> |
} catch (ReflectiveOperationException e) { |
625 |
> |
throw new AssertionError(e); |
626 |
|
} |
627 |
|
} else { |
628 |
|
return new TestSuite(); |
636 |
|
public static long MEDIUM_DELAY_MS; |
637 |
|
public static long LONG_DELAY_MS; |
638 |
|
|
639 |
+ |
private static final long RANDOM_TIMEOUT; |
640 |
+ |
private static final long RANDOM_EXPIRED_TIMEOUT; |
641 |
+ |
private static final TimeUnit RANDOM_TIMEUNIT; |
642 |
+ |
static { |
643 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
644 |
+ |
long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE }; |
645 |
+ |
RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)]; |
646 |
+ |
RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)]; |
647 |
+ |
TimeUnit[] timeUnits = TimeUnit.values(); |
648 |
+ |
RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)]; |
649 |
+ |
} |
650 |
+ |
|
651 |
+ |
/** |
652 |
+ |
* Returns a timeout for use when any value at all will do. |
653 |
+ |
*/ |
654 |
+ |
static long randomTimeout() { return RANDOM_TIMEOUT; } |
655 |
+ |
|
656 |
+ |
/** |
657 |
+ |
* Returns a timeout that means "no waiting", i.e. not positive. |
658 |
+ |
*/ |
659 |
+ |
static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; } |
660 |
+ |
|
661 |
+ |
/** |
662 |
+ |
* Returns a random non-null TimeUnit. |
663 |
+ |
*/ |
664 |
+ |
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
665 |
+ |
|
666 |
|
/** |
667 |
|
* Returns the shortest timed delay. This can be scaled up for |
668 |
< |
* slow machines using the jsr166.delay.factor system property. |
668 |
> |
* slow machines using the jsr166.delay.factor system property, |
669 |
> |
* or via jtreg's -timeoutFactor: flag. |
670 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
671 |
|
*/ |
672 |
|
protected long getShortDelay() { |
673 |
< |
return 50 * delayFactor; |
673 |
> |
return (long) (50 * delayFactor); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
683 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
684 |
|
} |
685 |
|
|
686 |
+ |
private static final long TIMEOUT_DELAY_MS |
687 |
+ |
= (long) (12.0 * Math.cbrt(delayFactor)); |
688 |
+ |
|
689 |
|
/** |
690 |
< |
* Returns a timeout in milliseconds to be used in tests that |
691 |
< |
* verify that operations block or time out. |
690 |
> |
* Returns a timeout in milliseconds to be used in tests that verify |
691 |
> |
* that operations block or time out. We want this to be longer |
692 |
> |
* than the OS scheduling quantum, but not too long, so don't scale |
693 |
> |
* linearly with delayFactor; we use "crazy" cube root instead. |
694 |
|
*/ |
695 |
< |
long timeoutMillis() { |
696 |
< |
return SHORT_DELAY_MS / 4; |
695 |
> |
static long timeoutMillis() { |
696 |
> |
return TIMEOUT_DELAY_MS; |
697 |
|
} |
698 |
|
|
699 |
|
/** |
709 |
|
* The first exception encountered if any threadAssertXXX method fails. |
710 |
|
*/ |
711 |
|
private final AtomicReference<Throwable> threadFailure |
712 |
< |
= new AtomicReference<Throwable>(null); |
712 |
> |
= new AtomicReference<>(null); |
713 |
|
|
714 |
|
/** |
715 |
|
* Records an exception so that it can be rethrown later in the test |
731 |
|
String msg = toString() + ": " + String.format(format, args); |
732 |
|
System.err.println(msg); |
733 |
|
dumpTestThreads(); |
734 |
< |
throw new AssertionFailedError(msg); |
734 |
> |
throw new AssertionError(msg); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
752 |
|
throw (RuntimeException) t; |
753 |
|
else if (t instanceof Exception) |
754 |
|
throw (Exception) t; |
755 |
< |
else { |
756 |
< |
AssertionFailedError afe = |
640 |
< |
new AssertionFailedError(t.toString()); |
641 |
< |
afe.initCause(t); |
642 |
< |
throw afe; |
643 |
< |
} |
755 |
> |
else |
756 |
> |
throw new AssertionError(t.toString(), t); |
757 |
|
} |
758 |
|
|
759 |
|
if (Thread.interrupted()) |
787 |
|
|
788 |
|
/** |
789 |
|
* Just like fail(reason), but additionally recording (using |
790 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
791 |
< |
* the current testcase will fail. |
790 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
791 |
> |
* current testcase will fail. |
792 |
|
*/ |
793 |
|
public void threadFail(String reason) { |
794 |
|
try { |
795 |
|
fail(reason); |
796 |
< |
} catch (AssertionFailedError t) { |
797 |
< |
threadRecordFailure(t); |
798 |
< |
throw t; |
796 |
> |
} catch (AssertionError fail) { |
797 |
> |
threadRecordFailure(fail); |
798 |
> |
throw fail; |
799 |
|
} |
800 |
|
} |
801 |
|
|
802 |
|
/** |
803 |
|
* Just like assertTrue(b), but additionally recording (using |
804 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
805 |
< |
* the current testcase will fail. |
804 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
805 |
> |
* current testcase will fail. |
806 |
|
*/ |
807 |
|
public void threadAssertTrue(boolean b) { |
808 |
|
try { |
809 |
|
assertTrue(b); |
810 |
< |
} catch (AssertionFailedError t) { |
811 |
< |
threadRecordFailure(t); |
812 |
< |
throw t; |
810 |
> |
} catch (AssertionError fail) { |
811 |
> |
threadRecordFailure(fail); |
812 |
> |
throw fail; |
813 |
|
} |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
|
* Just like assertFalse(b), but additionally recording (using |
818 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
819 |
< |
* the current testcase will fail. |
818 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
819 |
> |
* current testcase will fail. |
820 |
|
*/ |
821 |
|
public void threadAssertFalse(boolean b) { |
822 |
|
try { |
823 |
|
assertFalse(b); |
824 |
< |
} catch (AssertionFailedError t) { |
825 |
< |
threadRecordFailure(t); |
826 |
< |
throw t; |
824 |
> |
} catch (AssertionError fail) { |
825 |
> |
threadRecordFailure(fail); |
826 |
> |
throw fail; |
827 |
|
} |
828 |
|
} |
829 |
|
|
830 |
|
/** |
831 |
|
* Just like assertNull(x), but additionally recording (using |
832 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
833 |
< |
* the current testcase will fail. |
832 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
833 |
> |
* current testcase will fail. |
834 |
|
*/ |
835 |
|
public void threadAssertNull(Object x) { |
836 |
|
try { |
837 |
|
assertNull(x); |
838 |
< |
} catch (AssertionFailedError t) { |
839 |
< |
threadRecordFailure(t); |
840 |
< |
throw t; |
838 |
> |
} catch (AssertionError fail) { |
839 |
> |
threadRecordFailure(fail); |
840 |
> |
throw fail; |
841 |
|
} |
842 |
|
} |
843 |
|
|
844 |
|
/** |
845 |
|
* Just like assertEquals(x, y), but additionally recording (using |
846 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
847 |
< |
* the current testcase will fail. |
846 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
847 |
> |
* current testcase will fail. |
848 |
|
*/ |
849 |
|
public void threadAssertEquals(long x, long y) { |
850 |
|
try { |
851 |
|
assertEquals(x, y); |
852 |
< |
} catch (AssertionFailedError t) { |
853 |
< |
threadRecordFailure(t); |
854 |
< |
throw t; |
852 |
> |
} catch (AssertionError fail) { |
853 |
> |
threadRecordFailure(fail); |
854 |
> |
throw fail; |
855 |
|
} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
|
* Just like assertEquals(x, y), but additionally recording (using |
860 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
861 |
< |
* the current testcase will fail. |
860 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
861 |
> |
* current testcase will fail. |
862 |
|
*/ |
863 |
|
public void threadAssertEquals(Object x, Object y) { |
864 |
|
try { |
865 |
|
assertEquals(x, y); |
866 |
< |
} catch (AssertionFailedError fail) { |
866 |
> |
} catch (AssertionError fail) { |
867 |
|
threadRecordFailure(fail); |
868 |
|
throw fail; |
869 |
|
} catch (Throwable fail) { |
873 |
|
|
874 |
|
/** |
875 |
|
* Just like assertSame(x, y), but additionally recording (using |
876 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
877 |
< |
* the current testcase will fail. |
876 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
877 |
> |
* current testcase will fail. |
878 |
|
*/ |
879 |
|
public void threadAssertSame(Object x, Object y) { |
880 |
|
try { |
881 |
|
assertSame(x, y); |
882 |
< |
} catch (AssertionFailedError fail) { |
882 |
> |
} catch (AssertionError fail) { |
883 |
|
threadRecordFailure(fail); |
884 |
|
throw fail; |
885 |
|
} |
901 |
|
|
902 |
|
/** |
903 |
|
* Records the given exception using {@link #threadRecordFailure}, |
904 |
< |
* then rethrows the exception, wrapping it in an |
905 |
< |
* AssertionFailedError if necessary. |
904 |
> |
* then rethrows the exception, wrapping it in an AssertionError |
905 |
> |
* if necessary. |
906 |
|
*/ |
907 |
|
public void threadUnexpectedException(Throwable t) { |
908 |
|
threadRecordFailure(t); |
911 |
|
throw (RuntimeException) t; |
912 |
|
else if (t instanceof Error) |
913 |
|
throw (Error) t; |
914 |
< |
else { |
915 |
< |
AssertionFailedError afe = |
803 |
< |
new AssertionFailedError("unexpected exception: " + t); |
804 |
< |
afe.initCause(t); |
805 |
< |
throw afe; |
806 |
< |
} |
914 |
> |
else |
915 |
> |
throw new AssertionError("unexpected exception: " + t, t); |
916 |
|
} |
917 |
|
|
918 |
|
/** |
980 |
|
}}; |
981 |
|
} |
982 |
|
|
983 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
984 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
985 |
+ |
} |
986 |
+ |
|
987 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
988 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
989 |
+ |
} |
990 |
+ |
|
991 |
|
/** |
992 |
|
* Waits out termination of a thread pool or fails doing so. |
993 |
|
*/ |
1011 |
|
} |
1012 |
|
} |
1013 |
|
|
1014 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
1014 |
> |
/** |
1015 |
> |
* Like Runnable, but with the freedom to throw anything. |
1016 |
> |
* junit folks had the same idea: |
1017 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
1018 |
> |
*/ |
1019 |
|
interface Action { public void run() throws Throwable; } |
1020 |
|
|
1021 |
|
/** |
1046 |
|
* Uninteresting threads are filtered out. |
1047 |
|
*/ |
1048 |
|
static void dumpTestThreads() { |
1049 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1050 |
+ |
if (sm != null) { |
1051 |
+ |
try { |
1052 |
+ |
System.setSecurityManager(null); |
1053 |
+ |
} catch (SecurityException giveUp) { |
1054 |
+ |
return; |
1055 |
+ |
} |
1056 |
+ |
} |
1057 |
+ |
|
1058 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1059 |
|
System.err.println("------ stacktrace dump start ------"); |
1060 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1061 |
< |
String name = info.getThreadName(); |
1061 |
> |
final String name = info.getThreadName(); |
1062 |
> |
String lockName; |
1063 |
|
if ("Signal Dispatcher".equals(name)) |
1064 |
|
continue; |
1065 |
|
if ("Reference Handler".equals(name) |
1066 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1066 |
> |
&& (lockName = info.getLockName()) != null |
1067 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1068 |
|
continue; |
1069 |
|
if ("Finalizer".equals(name) |
1070 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1070 |
> |
&& (lockName = info.getLockName()) != null |
1071 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1072 |
|
continue; |
1073 |
|
if ("checkForWedgedTest".equals(name)) |
1074 |
|
continue; |
1075 |
|
System.err.print(info); |
1076 |
|
} |
1077 |
|
System.err.println("------ stacktrace dump end ------"); |
945 |
– |
} |
946 |
– |
|
947 |
– |
/** |
948 |
– |
* Checks that thread does not terminate within the default |
949 |
– |
* millisecond delay of {@code timeoutMillis()}. |
950 |
– |
*/ |
951 |
– |
void assertThreadStaysAlive(Thread thread) { |
952 |
– |
assertThreadStaysAlive(thread, timeoutMillis()); |
953 |
– |
} |
954 |
– |
|
955 |
– |
/** |
956 |
– |
* Checks that thread does not terminate within the given millisecond delay. |
957 |
– |
*/ |
958 |
– |
void assertThreadStaysAlive(Thread thread, long millis) { |
959 |
– |
try { |
960 |
– |
// No need to optimize the failing case via Thread.join. |
961 |
– |
delay(millis); |
962 |
– |
assertTrue(thread.isAlive()); |
963 |
– |
} catch (InterruptedException fail) { |
964 |
– |
threadFail("Unexpected InterruptedException"); |
965 |
– |
} |
966 |
– |
} |
1078 |
|
|
1079 |
< |
/** |
969 |
< |
* Checks that the threads do not terminate within the default |
970 |
< |
* millisecond delay of {@code timeoutMillis()}. |
971 |
< |
*/ |
972 |
< |
void assertThreadsStayAlive(Thread... threads) { |
973 |
< |
assertThreadsStayAlive(timeoutMillis(), threads); |
1079 |
> |
if (sm != null) System.setSecurityManager(sm); |
1080 |
|
} |
1081 |
|
|
1082 |
|
/** |
1083 |
< |
* Checks that the threads do not terminate within the given millisecond delay. |
1083 |
> |
* Checks that thread eventually enters the expected blocked thread state. |
1084 |
|
*/ |
1085 |
< |
void assertThreadsStayAlive(long millis, Thread... threads) { |
1086 |
< |
try { |
1087 |
< |
// No need to optimize the failing case via Thread.join. |
1088 |
< |
delay(millis); |
1089 |
< |
for (Thread thread : threads) |
1090 |
< |
assertTrue(thread.isAlive()); |
1091 |
< |
} catch (InterruptedException fail) { |
1092 |
< |
threadFail("Unexpected InterruptedException"); |
1085 |
> |
void assertThreadBlocks(Thread thread, Thread.State expected) { |
1086 |
> |
// always sleep at least 1 ms, with high probability avoiding |
1087 |
> |
// transitory states |
1088 |
> |
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1089 |
> |
try { delay(1); } |
1090 |
> |
catch (InterruptedException fail) { |
1091 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1092 |
> |
} |
1093 |
> |
Thread.State s = thread.getState(); |
1094 |
> |
if (s == expected) |
1095 |
> |
return; |
1096 |
> |
else if (s == Thread.State.TERMINATED) |
1097 |
> |
fail("Unexpected thread termination"); |
1098 |
|
} |
1099 |
+ |
fail("timed out waiting for thread to enter thread state " + expected); |
1100 |
|
} |
1101 |
|
|
1102 |
|
/** |
1137 |
|
} |
1138 |
|
|
1139 |
|
/** |
1140 |
+ |
* The maximum number of consecutive spurious wakeups we should |
1141 |
+ |
* tolerate (from APIs like LockSupport.park) before failing a test. |
1142 |
+ |
*/ |
1143 |
+ |
static final int MAX_SPURIOUS_WAKEUPS = 10; |
1144 |
+ |
|
1145 |
+ |
/** |
1146 |
|
* The number of elements to place in collections, arrays, etc. |
1147 |
|
*/ |
1148 |
|
public static final int SIZE = 20; |
1246 |
|
} |
1247 |
|
public void refresh() {} |
1248 |
|
public String toString() { |
1249 |
< |
List<Permission> ps = new ArrayList<Permission>(); |
1249 |
> |
List<Permission> ps = new ArrayList<>(); |
1250 |
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1251 |
|
ps.add(e.nextElement()); |
1252 |
|
return "AdjustablePolicy with permissions " + ps; |
1274 |
|
|
1275 |
|
/** |
1276 |
|
* Sleeps until the given time has elapsed. |
1277 |
< |
* Throws AssertionFailedError if interrupted. |
1277 |
> |
* Throws AssertionError if interrupted. |
1278 |
|
*/ |
1279 |
< |
void sleep(long millis) { |
1279 |
> |
static void sleep(long millis) { |
1280 |
|
try { |
1281 |
|
delay(millis); |
1282 |
|
} catch (InterruptedException fail) { |
1283 |
< |
AssertionFailedError afe = |
1166 |
< |
new AssertionFailedError("Unexpected InterruptedException"); |
1167 |
< |
afe.initCause(fail); |
1168 |
< |
throw afe; |
1283 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1284 |
|
} |
1285 |
|
} |
1286 |
|
|
1289 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1290 |
|
*/ |
1291 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1292 |
< |
long startTime = System.nanoTime(); |
1292 |
> |
long startTime = 0L; |
1293 |
|
for (;;) { |
1294 |
|
Thread.State s = thread.getState(); |
1295 |
|
if (s == Thread.State.BLOCKED || |
1298 |
|
return; |
1299 |
|
else if (s == Thread.State.TERMINATED) |
1300 |
|
fail("Unexpected thread termination"); |
1301 |
+ |
else if (startTime == 0L) |
1302 |
+ |
startTime = System.nanoTime(); |
1303 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1304 |
|
threadAssertTrue(thread.isAlive()); |
1305 |
< |
return; |
1305 |
> |
fail("timed out waiting for thread to enter wait state"); |
1306 |
|
} |
1307 |
|
Thread.yield(); |
1308 |
|
} |
1309 |
|
} |
1310 |
|
|
1311 |
|
/** |
1312 |
< |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1313 |
< |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1312 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
1313 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1314 |
> |
* and additionally satisfy the given condition. |
1315 |
> |
*/ |
1316 |
> |
void waitForThreadToEnterWaitState( |
1317 |
> |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1318 |
> |
long startTime = 0L; |
1319 |
> |
for (;;) { |
1320 |
> |
Thread.State s = thread.getState(); |
1321 |
> |
if (s == Thread.State.BLOCKED || |
1322 |
> |
s == Thread.State.WAITING || |
1323 |
> |
s == Thread.State.TIMED_WAITING) { |
1324 |
> |
try { |
1325 |
> |
if (waitingForGodot.call()) |
1326 |
> |
return; |
1327 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1328 |
> |
} |
1329 |
> |
else if (s == Thread.State.TERMINATED) |
1330 |
> |
fail("Unexpected thread termination"); |
1331 |
> |
else if (startTime == 0L) |
1332 |
> |
startTime = System.nanoTime(); |
1333 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1334 |
> |
threadAssertTrue(thread.isAlive()); |
1335 |
> |
fail("timed out waiting for thread to enter wait state"); |
1336 |
> |
} |
1337 |
> |
Thread.yield(); |
1338 |
> |
} |
1339 |
> |
} |
1340 |
> |
|
1341 |
> |
/** |
1342 |
> |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1343 |
> |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1344 |
|
*/ |
1345 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1346 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1347 |
|
} |
1348 |
|
|
1349 |
|
/** |
1350 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1351 |
+ |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1352 |
+ |
* and additionally satisfy the given condition. |
1353 |
+ |
*/ |
1354 |
+ |
void waitForThreadToEnterWaitState( |
1355 |
+ |
Thread thread, Callable<Boolean> waitingForGodot) { |
1356 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1357 |
+ |
} |
1358 |
+ |
|
1359 |
+ |
/** |
1360 |
|
* Returns the number of milliseconds since time given by |
1361 |
|
* startNanoTime, which must have been previously returned from a |
1362 |
|
* call to {@link System#nanoTime()}. |
1371 |
|
// r.run(); |
1372 |
|
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1373 |
|
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1374 |
< |
// throw new AssertionFailedError("did not return promptly"); |
1374 |
> |
// throw new AssertionError("did not return promptly"); |
1375 |
|
// } |
1376 |
|
|
1377 |
|
// void assertTerminatesPromptly(Runnable r) { |
1388 |
|
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1389 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1390 |
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1391 |
< |
throw new AssertionFailedError("timed get did not return promptly"); |
1391 |
> |
throw new AssertionError("timed get did not return promptly"); |
1392 |
|
} |
1393 |
|
|
1394 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1584 |
|
return new LatchAwaiter(latch); |
1585 |
|
} |
1586 |
|
|
1587 |
< |
public void await(CountDownLatch latch) { |
1587 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1588 |
|
try { |
1589 |
< |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1589 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1590 |
|
fail("timed out waiting for CountDownLatch for " |
1591 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1591 |
> |
+ (timeoutMillis/1000) + " sec"); |
1592 |
|
} catch (Throwable fail) { |
1593 |
|
threadUnexpectedException(fail); |
1594 |
|
} |
1595 |
|
} |
1596 |
|
|
1597 |
+ |
public void await(CountDownLatch latch) { |
1598 |
+ |
await(latch, LONG_DELAY_MS); |
1599 |
+ |
} |
1600 |
+ |
|
1601 |
|
public void await(Semaphore semaphore) { |
1602 |
|
try { |
1603 |
|
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1608 |
|
} |
1609 |
|
} |
1610 |
|
|
1611 |
+ |
public void await(CyclicBarrier barrier) { |
1612 |
+ |
try { |
1613 |
+ |
barrier.await(LONG_DELAY_MS, MILLISECONDS); |
1614 |
+ |
} catch (Throwable fail) { |
1615 |
+ |
threadUnexpectedException(fail); |
1616 |
+ |
} |
1617 |
+ |
} |
1618 |
+ |
|
1619 |
|
// /** |
1620 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1621 |
|
// */ |
1630 |
|
// long startTime = System.nanoTime(); |
1631 |
|
// while (!flag.get()) { |
1632 |
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1633 |
< |
// throw new AssertionFailedError("timed out"); |
1633 |
> |
// throw new AssertionError("timed out"); |
1634 |
|
// Thread.yield(); |
1635 |
|
// } |
1636 |
|
// } |
1639 |
|
public String call() { throw new NullPointerException(); } |
1640 |
|
} |
1641 |
|
|
1473 |
– |
public static class CallableOne implements Callable<Integer> { |
1474 |
– |
public Integer call() { return one; } |
1475 |
– |
} |
1476 |
– |
|
1477 |
– |
public class ShortRunnable extends CheckedRunnable { |
1478 |
– |
protected void realRun() throws Throwable { |
1479 |
– |
delay(SHORT_DELAY_MS); |
1480 |
– |
} |
1481 |
– |
} |
1482 |
– |
|
1483 |
– |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1484 |
– |
protected void realRun() throws InterruptedException { |
1485 |
– |
delay(SHORT_DELAY_MS); |
1486 |
– |
} |
1487 |
– |
} |
1488 |
– |
|
1489 |
– |
public class SmallRunnable extends CheckedRunnable { |
1490 |
– |
protected void realRun() throws Throwable { |
1491 |
– |
delay(SMALL_DELAY_MS); |
1492 |
– |
} |
1493 |
– |
} |
1494 |
– |
|
1642 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1643 |
|
protected void realRun() { |
1644 |
|
try { |
1647 |
|
} |
1648 |
|
} |
1649 |
|
|
1503 |
– |
public class SmallCallable extends CheckedCallable { |
1504 |
– |
protected Object realCall() throws InterruptedException { |
1505 |
– |
delay(SMALL_DELAY_MS); |
1506 |
– |
return Boolean.TRUE; |
1507 |
– |
} |
1508 |
– |
} |
1509 |
– |
|
1510 |
– |
public class MediumRunnable extends CheckedRunnable { |
1511 |
– |
protected void realRun() throws Throwable { |
1512 |
– |
delay(MEDIUM_DELAY_MS); |
1513 |
– |
} |
1514 |
– |
} |
1515 |
– |
|
1516 |
– |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1517 |
– |
protected void realRun() throws InterruptedException { |
1518 |
– |
delay(MEDIUM_DELAY_MS); |
1519 |
– |
} |
1520 |
– |
} |
1521 |
– |
|
1650 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1651 |
|
return new CheckedRunnable() { |
1652 |
|
protected void realRun() { |
1656 |
|
}}; |
1657 |
|
} |
1658 |
|
|
1531 |
– |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1532 |
– |
protected void realRun() { |
1533 |
– |
try { |
1534 |
– |
delay(MEDIUM_DELAY_MS); |
1535 |
– |
} catch (InterruptedException ok) {} |
1536 |
– |
} |
1537 |
– |
} |
1538 |
– |
|
1539 |
– |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1540 |
– |
protected void realRun() { |
1541 |
– |
try { |
1542 |
– |
delay(LONG_DELAY_MS); |
1543 |
– |
} catch (InterruptedException ok) {} |
1544 |
– |
} |
1545 |
– |
} |
1546 |
– |
|
1659 |
|
/** |
1660 |
|
* For use as ThreadFactory in constructors |
1661 |
|
*/ |
1669 |
|
boolean isDone(); |
1670 |
|
} |
1671 |
|
|
1560 |
– |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1561 |
– |
return new TrackedRunnable() { |
1562 |
– |
private volatile boolean done = false; |
1563 |
– |
public boolean isDone() { return done; } |
1564 |
– |
public void run() { |
1565 |
– |
try { |
1566 |
– |
delay(timeoutMillis); |
1567 |
– |
done = true; |
1568 |
– |
} catch (InterruptedException ok) {} |
1569 |
– |
} |
1570 |
– |
}; |
1571 |
– |
} |
1572 |
– |
|
1573 |
– |
public static class TrackedShortRunnable implements Runnable { |
1574 |
– |
public volatile boolean done = false; |
1575 |
– |
public void run() { |
1576 |
– |
try { |
1577 |
– |
delay(SHORT_DELAY_MS); |
1578 |
– |
done = true; |
1579 |
– |
} catch (InterruptedException ok) {} |
1580 |
– |
} |
1581 |
– |
} |
1582 |
– |
|
1583 |
– |
public static class TrackedSmallRunnable implements Runnable { |
1584 |
– |
public volatile boolean done = false; |
1585 |
– |
public void run() { |
1586 |
– |
try { |
1587 |
– |
delay(SMALL_DELAY_MS); |
1588 |
– |
done = true; |
1589 |
– |
} catch (InterruptedException ok) {} |
1590 |
– |
} |
1591 |
– |
} |
1592 |
– |
|
1593 |
– |
public static class TrackedMediumRunnable implements Runnable { |
1594 |
– |
public volatile boolean done = false; |
1595 |
– |
public void run() { |
1596 |
– |
try { |
1597 |
– |
delay(MEDIUM_DELAY_MS); |
1598 |
– |
done = true; |
1599 |
– |
} catch (InterruptedException ok) {} |
1600 |
– |
} |
1601 |
– |
} |
1602 |
– |
|
1603 |
– |
public static class TrackedLongRunnable implements Runnable { |
1604 |
– |
public volatile boolean done = false; |
1605 |
– |
public void run() { |
1606 |
– |
try { |
1607 |
– |
delay(LONG_DELAY_MS); |
1608 |
– |
done = true; |
1609 |
– |
} catch (InterruptedException ok) {} |
1610 |
– |
} |
1611 |
– |
} |
1612 |
– |
|
1672 |
|
public static class TrackedNoOpRunnable implements Runnable { |
1673 |
|
public volatile boolean done = false; |
1674 |
|
public void run() { |
1676 |
|
} |
1677 |
|
} |
1678 |
|
|
1620 |
– |
public static class TrackedCallable implements Callable { |
1621 |
– |
public volatile boolean done = false; |
1622 |
– |
public Object call() { |
1623 |
– |
try { |
1624 |
– |
delay(SMALL_DELAY_MS); |
1625 |
– |
done = true; |
1626 |
– |
} catch (InterruptedException ok) {} |
1627 |
– |
return Boolean.TRUE; |
1628 |
– |
} |
1629 |
– |
} |
1630 |
– |
|
1679 |
|
/** |
1680 |
|
* Analog of CheckedRunnable for RecursiveAction |
1681 |
|
*/ |
1717 |
|
|
1718 |
|
/** |
1719 |
|
* A CyclicBarrier that uses timed await and fails with |
1720 |
< |
* AssertionFailedErrors instead of throwing checked exceptions. |
1720 |
> |
* AssertionErrors instead of throwing checked exceptions. |
1721 |
|
*/ |
1722 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1722 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1723 |
|
public CheckedBarrier(int parties) { super(parties); } |
1724 |
|
|
1725 |
|
public int await() { |
1726 |
|
try { |
1727 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1728 |
|
} catch (TimeoutException timedOut) { |
1729 |
< |
throw new AssertionFailedError("timed out"); |
1729 |
> |
throw new AssertionError("timed out"); |
1730 |
|
} catch (Exception fail) { |
1731 |
< |
AssertionFailedError afe = |
1684 |
< |
new AssertionFailedError("Unexpected exception: " + fail); |
1685 |
< |
afe.initCause(fail); |
1686 |
< |
throw afe; |
1731 |
> |
throw new AssertionError("Unexpected exception: " + fail, fail); |
1732 |
|
} |
1733 |
|
} |
1734 |
|
} |
1739 |
|
assertEquals(0, q.size()); |
1740 |
|
assertNull(q.peek()); |
1741 |
|
assertNull(q.poll()); |
1742 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
1742 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
1743 |
|
assertEquals(q.toString(), "[]"); |
1744 |
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1745 |
|
assertFalse(q.iterator().hasNext()); |
1780 |
|
} |
1781 |
|
} |
1782 |
|
|
1783 |
+ |
void assertImmutable(final Object o) { |
1784 |
+ |
if (o instanceof Collection) { |
1785 |
+ |
assertThrows( |
1786 |
+ |
UnsupportedOperationException.class, |
1787 |
+ |
new Runnable() { public void run() { |
1788 |
+ |
((Collection) o).add(null);}}); |
1789 |
+ |
} |
1790 |
+ |
} |
1791 |
+ |
|
1792 |
|
@SuppressWarnings("unchecked") |
1793 |
|
<T> T serialClone(T o) { |
1794 |
|
try { |
1795 |
|
ObjectInputStream ois = new ObjectInputStream |
1796 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1797 |
|
T clone = (T) ois.readObject(); |
1798 |
+ |
if (o == clone) assertImmutable(o); |
1799 |
|
assertSame(o.getClass(), clone.getClass()); |
1800 |
|
return clone; |
1801 |
|
} catch (Throwable fail) { |
1804 |
|
} |
1805 |
|
} |
1806 |
|
|
1807 |
+ |
/** |
1808 |
+ |
* A version of serialClone that leaves error handling (for |
1809 |
+ |
* e.g. NotSerializableException) up to the caller. |
1810 |
+ |
*/ |
1811 |
+ |
@SuppressWarnings("unchecked") |
1812 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1813 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1814 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1815 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1816 |
+ |
oos.writeObject(o); |
1817 |
+ |
oos.flush(); |
1818 |
+ |
oos.close(); |
1819 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1820 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1821 |
+ |
T clone = (T) ois.readObject(); |
1822 |
+ |
if (o == clone) assertImmutable(o); |
1823 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1824 |
+ |
return clone; |
1825 |
+ |
} |
1826 |
+ |
|
1827 |
+ |
/** |
1828 |
+ |
* If o implements Cloneable and has a public clone method, |
1829 |
+ |
* returns a clone of o, else null. |
1830 |
+ |
*/ |
1831 |
+ |
@SuppressWarnings("unchecked") |
1832 |
+ |
<T> T cloneableClone(T o) { |
1833 |
+ |
if (!(o instanceof Cloneable)) return null; |
1834 |
+ |
final T clone; |
1835 |
+ |
try { |
1836 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1837 |
+ |
} catch (NoSuchMethodException ok) { |
1838 |
+ |
return null; |
1839 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1840 |
+ |
throw new Error(unexpected); |
1841 |
+ |
} |
1842 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1843 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1844 |
+ |
return clone; |
1845 |
+ |
} |
1846 |
+ |
|
1847 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1848 |
|
Runnable... throwingActions) { |
1849 |
|
for (Runnable throwingAction : throwingActions) { |
1851 |
|
try { throwingAction.run(); } |
1852 |
|
catch (Throwable t) { |
1853 |
|
threw = true; |
1854 |
< |
if (!expectedExceptionClass.isInstance(t)) { |
1855 |
< |
AssertionFailedError afe = |
1856 |
< |
new AssertionFailedError |
1857 |
< |
("Expected " + expectedExceptionClass.getName() + |
1858 |
< |
", got " + t.getClass().getName()); |
1764 |
< |
afe.initCause(t); |
1765 |
< |
threadUnexpectedException(afe); |
1766 |
< |
} |
1854 |
> |
if (!expectedExceptionClass.isInstance(t)) |
1855 |
> |
throw new AssertionError( |
1856 |
> |
"Expected " + expectedExceptionClass.getName() + |
1857 |
> |
", got " + t.getClass().getName(), |
1858 |
> |
t); |
1859 |
|
} |
1860 |
|
if (!threw) |
1861 |
|
shouldThrow(expectedExceptionClass.getName()); |
1869 |
|
} catch (NoSuchElementException success) {} |
1870 |
|
assertFalse(it.hasNext()); |
1871 |
|
} |
1872 |
+ |
|
1873 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1874 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1875 |
+ |
} |
1876 |
+ |
|
1877 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1878 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1879 |
+ |
} |
1880 |
+ |
|
1881 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1882 |
+ |
static final ExecutorService cachedThreadPool = |
1883 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1884 |
+ |
1000L, MILLISECONDS, |
1885 |
+ |
new SynchronousQueue<Runnable>()); |
1886 |
+ |
|
1887 |
+ |
static <T> void shuffle(T[] array) { |
1888 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1889 |
+ |
} |
1890 |
+ |
|
1891 |
+ |
/** |
1892 |
+ |
* Returns the same String as would be returned by {@link |
1893 |
+ |
* Object#toString}, whether or not the given object's class |
1894 |
+ |
* overrides toString(). |
1895 |
+ |
* |
1896 |
+ |
* @see System#identityHashCode |
1897 |
+ |
*/ |
1898 |
+ |
static String identityString(Object x) { |
1899 |
+ |
return x.getClass().getName() |
1900 |
+ |
+ "@" + Integer.toHexString(System.identityHashCode(x)); |
1901 |
+ |
} |
1902 |
+ |
|
1903 |
+ |
// --- Shared assertions for Executor tests --- |
1904 |
+ |
|
1905 |
+ |
/** |
1906 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1907 |
+ |
* pool (with bounded queue) before saturation (when submission |
1908 |
+ |
* throws RejectedExecutionException). |
1909 |
+ |
*/ |
1910 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1911 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1912 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1913 |
+ |
} |
1914 |
+ |
|
1915 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1916 |
+ |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1917 |
+ |
try { |
1918 |
+ |
e.execute((Runnable) null); |
1919 |
+ |
shouldThrow(); |
1920 |
+ |
} catch (NullPointerException success) {} |
1921 |
+ |
|
1922 |
+ |
if (! (e instanceof ExecutorService)) return; |
1923 |
+ |
ExecutorService es = (ExecutorService) e; |
1924 |
+ |
try { |
1925 |
+ |
es.submit((Runnable) null); |
1926 |
+ |
shouldThrow(); |
1927 |
+ |
} catch (NullPointerException success) {} |
1928 |
+ |
try { |
1929 |
+ |
es.submit((Runnable) null, Boolean.TRUE); |
1930 |
+ |
shouldThrow(); |
1931 |
+ |
} catch (NullPointerException success) {} |
1932 |
+ |
try { |
1933 |
+ |
es.submit((Callable) null); |
1934 |
+ |
shouldThrow(); |
1935 |
+ |
} catch (NullPointerException success) {} |
1936 |
+ |
|
1937 |
+ |
if (! (e instanceof ScheduledExecutorService)) return; |
1938 |
+ |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1939 |
+ |
try { |
1940 |
+ |
ses.schedule((Runnable) null, |
1941 |
+ |
randomTimeout(), randomTimeUnit()); |
1942 |
+ |
shouldThrow(); |
1943 |
+ |
} catch (NullPointerException success) {} |
1944 |
+ |
try { |
1945 |
+ |
ses.schedule((Callable) null, |
1946 |
+ |
randomTimeout(), randomTimeUnit()); |
1947 |
+ |
shouldThrow(); |
1948 |
+ |
} catch (NullPointerException success) {} |
1949 |
+ |
try { |
1950 |
+ |
ses.scheduleAtFixedRate((Runnable) null, |
1951 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1952 |
+ |
shouldThrow(); |
1953 |
+ |
} catch (NullPointerException success) {} |
1954 |
+ |
try { |
1955 |
+ |
ses.scheduleWithFixedDelay((Runnable) null, |
1956 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1957 |
+ |
shouldThrow(); |
1958 |
+ |
} catch (NullPointerException success) {} |
1959 |
+ |
} |
1960 |
+ |
|
1961 |
+ |
void setRejectedExecutionHandler( |
1962 |
+ |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
1963 |
+ |
p.setRejectedExecutionHandler(handler); |
1964 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1965 |
+ |
} |
1966 |
+ |
|
1967 |
+ |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
1968 |
+ |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
1969 |
+ |
final long savedTaskCount = p.getTaskCount(); |
1970 |
+ |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
1971 |
+ |
final int savedQueueSize = p.getQueue().size(); |
1972 |
+ |
final boolean stock = (p.getClass().getClassLoader() == null); |
1973 |
+ |
|
1974 |
+ |
Runnable r = () -> {}; |
1975 |
+ |
Callable<Boolean> c = () -> Boolean.TRUE; |
1976 |
+ |
|
1977 |
+ |
class Recorder implements RejectedExecutionHandler { |
1978 |
+ |
public volatile Runnable r = null; |
1979 |
+ |
public volatile ThreadPoolExecutor p = null; |
1980 |
+ |
public void reset() { r = null; p = null; } |
1981 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
1982 |
+ |
assertNull(this.r); |
1983 |
+ |
assertNull(this.p); |
1984 |
+ |
this.r = r; |
1985 |
+ |
this.p = p; |
1986 |
+ |
} |
1987 |
+ |
} |
1988 |
+ |
|
1989 |
+ |
// check custom handler is invoked exactly once per task |
1990 |
+ |
Recorder recorder = new Recorder(); |
1991 |
+ |
setRejectedExecutionHandler(p, recorder); |
1992 |
+ |
for (int i = 2; i--> 0; ) { |
1993 |
+ |
recorder.reset(); |
1994 |
+ |
p.execute(r); |
1995 |
+ |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
1996 |
+ |
assertSame(r, recorder.r); |
1997 |
+ |
assertSame(p, recorder.p); |
1998 |
+ |
|
1999 |
+ |
recorder.reset(); |
2000 |
+ |
assertFalse(p.submit(r).isDone()); |
2001 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2002 |
+ |
assertSame(p, recorder.p); |
2003 |
+ |
|
2004 |
+ |
recorder.reset(); |
2005 |
+ |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
2006 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2007 |
+ |
assertSame(p, recorder.p); |
2008 |
+ |
|
2009 |
+ |
recorder.reset(); |
2010 |
+ |
assertFalse(p.submit(c).isDone()); |
2011 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2012 |
+ |
assertSame(p, recorder.p); |
2013 |
+ |
|
2014 |
+ |
if (p instanceof ScheduledExecutorService) { |
2015 |
+ |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2016 |
+ |
ScheduledFuture<?> future; |
2017 |
+ |
|
2018 |
+ |
recorder.reset(); |
2019 |
+ |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2020 |
+ |
assertFalse(future.isDone()); |
2021 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2022 |
+ |
assertSame(p, recorder.p); |
2023 |
+ |
|
2024 |
+ |
recorder.reset(); |
2025 |
+ |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2026 |
+ |
assertFalse(future.isDone()); |
2027 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2028 |
+ |
assertSame(p, recorder.p); |
2029 |
+ |
|
2030 |
+ |
recorder.reset(); |
2031 |
+ |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2032 |
+ |
assertFalse(future.isDone()); |
2033 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2034 |
+ |
assertSame(p, recorder.p); |
2035 |
+ |
|
2036 |
+ |
recorder.reset(); |
2037 |
+ |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2038 |
+ |
assertFalse(future.isDone()); |
2039 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2040 |
+ |
assertSame(p, recorder.p); |
2041 |
+ |
} |
2042 |
+ |
} |
2043 |
+ |
|
2044 |
+ |
// Checking our custom handler above should be sufficient, but |
2045 |
+ |
// we add some integration tests of standard handlers. |
2046 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2047 |
+ |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2048 |
+ |
|
2049 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2050 |
+ |
try { |
2051 |
+ |
p.execute(setThread); |
2052 |
+ |
shouldThrow(); |
2053 |
+ |
} catch (RejectedExecutionException success) {} |
2054 |
+ |
assertNull(thread.get()); |
2055 |
+ |
|
2056 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2057 |
+ |
p.execute(setThread); |
2058 |
+ |
assertNull(thread.get()); |
2059 |
+ |
|
2060 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2061 |
+ |
p.execute(setThread); |
2062 |
+ |
if (p.isShutdown()) |
2063 |
+ |
assertNull(thread.get()); |
2064 |
+ |
else |
2065 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2066 |
+ |
|
2067 |
+ |
setRejectedExecutionHandler(p, savedHandler); |
2068 |
+ |
|
2069 |
+ |
// check that pool was not perturbed by handlers |
2070 |
+ |
assertEquals(savedTaskCount, p.getTaskCount()); |
2071 |
+ |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2072 |
+ |
assertEquals(savedQueueSize, p.getQueue().size()); |
2073 |
+ |
} |
2074 |
|
} |