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. |
9 |
|
|
10 |
|
/* |
11 |
|
* @test |
12 |
< |
* @summary JSR-166 tck tests |
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; |
54 |
|
import java.lang.reflect.Constructor; |
55 |
|
import java.lang.reflect.Method; |
56 |
|
import java.lang.reflect.Modifier; |
30 |
– |
import java.nio.file.Files; |
31 |
– |
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.Deque; |
70 |
|
import java.util.Enumeration; |
71 |
+ |
import java.util.HashSet; |
72 |
|
import java.util.Iterator; |
73 |
|
import java.util.List; |
74 |
|
import java.util.NoSuchElementException; |
75 |
|
import java.util.PropertyPermission; |
76 |
+ |
import java.util.Set; |
77 |
|
import java.util.concurrent.BlockingQueue; |
78 |
|
import java.util.concurrent.Callable; |
79 |
|
import java.util.concurrent.CountDownLatch; |
80 |
|
import java.util.concurrent.CyclicBarrier; |
81 |
|
import java.util.concurrent.ExecutionException; |
82 |
+ |
import java.util.concurrent.Executor; |
83 |
|
import java.util.concurrent.Executors; |
84 |
|
import java.util.concurrent.ExecutorService; |
85 |
|
import java.util.concurrent.ForkJoinPool; |
86 |
|
import java.util.concurrent.Future; |
87 |
+ |
import java.util.concurrent.FutureTask; |
88 |
|
import java.util.concurrent.RecursiveAction; |
89 |
|
import java.util.concurrent.RecursiveTask; |
90 |
+ |
import java.util.concurrent.RejectedExecutionException; |
91 |
|
import java.util.concurrent.RejectedExecutionHandler; |
92 |
|
import java.util.concurrent.Semaphore; |
93 |
+ |
import java.util.concurrent.ScheduledExecutorService; |
94 |
+ |
import java.util.concurrent.ScheduledFuture; |
95 |
+ |
import java.util.concurrent.SynchronousQueue; |
96 |
|
import java.util.concurrent.ThreadFactory; |
97 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
98 |
|
import java.util.concurrent.ThreadPoolExecutor; |
99 |
+ |
import java.util.concurrent.TimeUnit; |
100 |
|
import java.util.concurrent.TimeoutException; |
101 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
102 |
|
import java.util.concurrent.atomic.AtomicReference; |
64 |
– |
import java.util.regex.Matcher; |
103 |
|
import java.util.regex.Pattern; |
104 |
|
|
67 |
– |
import junit.framework.AssertionFailedError; |
105 |
|
import junit.framework.Test; |
106 |
|
import junit.framework.TestCase; |
107 |
|
import junit.framework.TestResult; |
220 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
221 |
|
|
222 |
|
/** |
223 |
< |
* The scaling factor to apply to standard delays used in tests. |
223 |
> |
* Returns the value of the system property, or NaN if not defined. |
224 |
|
*/ |
225 |
< |
private static final int delayFactor = |
226 |
< |
Integer.getInteger("jsr166.delay.factor", 1); |
225 |
> |
private static float systemPropertyValue(String name) { |
226 |
> |
String floatString = System.getProperty(name); |
227 |
> |
if (floatString == null) |
228 |
> |
return Float.NaN; |
229 |
> |
try { |
230 |
> |
return Float.parseFloat(floatString); |
231 |
> |
} catch (NumberFormatException ex) { |
232 |
> |
throw new IllegalArgumentException( |
233 |
> |
String.format("Bad float value in system property %s=%s", |
234 |
> |
name, floatString)); |
235 |
> |
} |
236 |
> |
} |
237 |
> |
|
238 |
> |
/** |
239 |
> |
* The scaling factor to apply to standard delays used in tests. |
240 |
> |
* May be initialized from any of: |
241 |
> |
* - the "jsr166.delay.factor" system property |
242 |
> |
* - the "test.timeout.factor" system property (as used by jtreg) |
243 |
> |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
244 |
> |
* - hard-coded fuzz factor when using a known slowpoke VM |
245 |
> |
*/ |
246 |
> |
private static final float delayFactor = delayFactor(); |
247 |
> |
|
248 |
> |
private static float delayFactor() { |
249 |
> |
float x; |
250 |
> |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
251 |
> |
return x; |
252 |
> |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
253 |
> |
return x; |
254 |
> |
String prop = System.getProperty("java.vm.version"); |
255 |
> |
if (prop != null && prop.matches(".*debug.*")) |
256 |
> |
return 4.0f; // How much slower is fastdebug than product?! |
257 |
> |
return 1.0f; |
258 |
> |
} |
259 |
|
|
260 |
|
public JSR166TestCase() { super(); } |
261 |
|
public JSR166TestCase(String name) { super(name); } |
308 |
|
|
309 |
|
// public static String cpuModel() { |
310 |
|
// try { |
311 |
< |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
311 |
> |
// java.util.regex.Matcher matcher |
312 |
> |
// = Pattern.compile("model name\\s*: (.*)") |
313 |
|
// .matcher(new String( |
314 |
< |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
314 |
> |
// java.nio.file.Files.readAllBytes( |
315 |
> |
// java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8")); |
316 |
|
// matcher.find(); |
317 |
|
// return matcher.group(1); |
318 |
|
// } catch (Exception ex) { return null; } |
421 |
|
for (String testClassName : testClassNames) { |
422 |
|
try { |
423 |
|
Class<?> testClass = Class.forName(testClassName); |
424 |
< |
Method m = testClass.getDeclaredMethod("suite", |
354 |
< |
new Class<?>[0]); |
424 |
> |
Method m = testClass.getDeclaredMethod("suite"); |
425 |
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
426 |
< |
} catch (Exception e) { |
427 |
< |
throw new Error("Missing test class", e); |
426 |
> |
} catch (ReflectiveOperationException e) { |
427 |
> |
throw new AssertionError("Missing test class", e); |
428 |
|
} |
429 |
|
} |
430 |
|
} |
446 |
|
} |
447 |
|
} |
448 |
|
|
449 |
< |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
450 |
< |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
451 |
< |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
452 |
< |
public static boolean atLeastJava9() { |
453 |
< |
return JAVA_CLASS_VERSION >= 53.0 |
454 |
< |
// As of 2015-09, java9 still uses 52.0 class file version |
385 |
< |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
386 |
< |
} |
387 |
< |
public static boolean atLeastJava10() { |
388 |
< |
return JAVA_CLASS_VERSION >= 54.0 |
389 |
< |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
390 |
< |
} |
449 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
450 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
451 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
452 |
> |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
453 |
> |
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
454 |
> |
public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; } |
455 |
|
|
456 |
|
/** |
457 |
|
* Collects all JSR166 unit tests as one suite. |
472 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
473 |
|
ArrayBlockingQueueTest.suite(), |
474 |
|
ArrayDequeTest.suite(), |
475 |
+ |
ArrayListTest.suite(), |
476 |
|
AtomicBooleanTest.suite(), |
477 |
|
AtomicIntegerArrayTest.suite(), |
478 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
495 |
|
CopyOnWriteArrayListTest.suite(), |
496 |
|
CopyOnWriteArraySetTest.suite(), |
497 |
|
CountDownLatchTest.suite(), |
498 |
+ |
CountedCompleterTest.suite(), |
499 |
|
CyclicBarrierTest.suite(), |
500 |
|
DelayQueueTest.suite(), |
501 |
|
EntryTest.suite(), |
524 |
|
TreeMapTest.suite(), |
525 |
|
TreeSetTest.suite(), |
526 |
|
TreeSubMapTest.suite(), |
527 |
< |
TreeSubSetTest.suite()); |
527 |
> |
TreeSubSetTest.suite(), |
528 |
> |
VectorTest.suite()); |
529 |
|
|
530 |
|
// Java8+ test classes |
531 |
|
if (atLeastJava8()) { |
532 |
|
String[] java8TestClassNames = { |
533 |
+ |
"ArrayDeque8Test", |
534 |
|
"Atomic8Test", |
535 |
|
"CompletableFutureTest", |
536 |
|
"ConcurrentHashMap8Test", |
537 |
< |
"CountedCompleterTest", |
537 |
> |
"CountedCompleter8Test", |
538 |
|
"DoubleAccumulatorTest", |
539 |
|
"DoubleAdderTest", |
540 |
|
"ForkJoinPool8Test", |
541 |
|
"ForkJoinTask8Test", |
542 |
+ |
"HashMapTest", |
543 |
+ |
"LinkedBlockingDeque8Test", |
544 |
+ |
"LinkedBlockingQueue8Test", |
545 |
|
"LongAccumulatorTest", |
546 |
|
"LongAdderTest", |
547 |
|
"SplittableRandomTest", |
548 |
|
"StampedLockTest", |
549 |
|
"SubmissionPublisherTest", |
550 |
|
"ThreadLocalRandom8Test", |
551 |
+ |
"TimeUnit8Test", |
552 |
|
}; |
553 |
|
addNamedTestClasses(suite, java8TestClassNames); |
554 |
|
} |
556 |
|
// Java9+ test classes |
557 |
|
if (atLeastJava9()) { |
558 |
|
String[] java9TestClassNames = { |
559 |
< |
// Currently empty, but expecting varhandle tests |
559 |
> |
"AtomicBoolean9Test", |
560 |
> |
"AtomicInteger9Test", |
561 |
> |
"AtomicIntegerArray9Test", |
562 |
> |
"AtomicLong9Test", |
563 |
> |
"AtomicLongArray9Test", |
564 |
> |
"AtomicReference9Test", |
565 |
> |
"AtomicReferenceArray9Test", |
566 |
> |
"ExecutorCompletionService9Test", |
567 |
> |
"ForkJoinPool9Test", |
568 |
|
}; |
569 |
|
addNamedTestClasses(suite, java9TestClassNames); |
570 |
|
} |
575 |
|
/** Returns list of junit-style test method names in given class. */ |
576 |
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
577 |
|
Method[] methods = testClass.getDeclaredMethods(); |
578 |
< |
ArrayList<String> names = new ArrayList<String>(methods.length); |
578 |
> |
ArrayList<String> names = new ArrayList<>(methods.length); |
579 |
|
for (Method method : methods) { |
580 |
|
if (method.getName().startsWith("test") |
581 |
|
&& Modifier.isPublic(method.getModifiers()) |
602 |
|
for (String methodName : testMethodNames(testClass)) |
603 |
|
suite.addTest((Test) c.newInstance(data, methodName)); |
604 |
|
return suite; |
605 |
< |
} catch (Exception e) { |
606 |
< |
throw new Error(e); |
605 |
> |
} catch (ReflectiveOperationException e) { |
606 |
> |
throw new AssertionError(e); |
607 |
|
} |
608 |
|
} |
609 |
|
|
619 |
|
if (atLeastJava8()) { |
620 |
|
String name = testClass.getName(); |
621 |
|
String name8 = name.replaceAll("Test$", "8Test"); |
622 |
< |
if (name.equals(name8)) throw new Error(name); |
622 |
> |
if (name.equals(name8)) throw new AssertionError(name); |
623 |
|
try { |
624 |
|
return (Test) |
625 |
|
Class.forName(name8) |
626 |
< |
.getMethod("testSuite", new Class[] { dataClass }) |
626 |
> |
.getMethod("testSuite", dataClass) |
627 |
|
.invoke(null, data); |
628 |
< |
} catch (Exception e) { |
629 |
< |
throw new Error(e); |
628 |
> |
} catch (ReflectiveOperationException e) { |
629 |
> |
throw new AssertionError(e); |
630 |
|
} |
631 |
|
} else { |
632 |
|
return new TestSuite(); |
640 |
|
public static long MEDIUM_DELAY_MS; |
641 |
|
public static long LONG_DELAY_MS; |
642 |
|
|
643 |
+ |
private static final long RANDOM_TIMEOUT; |
644 |
+ |
private static final long RANDOM_EXPIRED_TIMEOUT; |
645 |
+ |
private static final TimeUnit RANDOM_TIMEUNIT; |
646 |
+ |
static { |
647 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
648 |
+ |
long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE }; |
649 |
+ |
RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)]; |
650 |
+ |
RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)]; |
651 |
+ |
TimeUnit[] timeUnits = TimeUnit.values(); |
652 |
+ |
RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)]; |
653 |
+ |
} |
654 |
+ |
|
655 |
+ |
/** |
656 |
+ |
* Returns a timeout for use when any value at all will do. |
657 |
+ |
*/ |
658 |
+ |
static long randomTimeout() { return RANDOM_TIMEOUT; } |
659 |
+ |
|
660 |
+ |
/** |
661 |
+ |
* Returns a timeout that means "no waiting", i.e. not positive. |
662 |
+ |
*/ |
663 |
+ |
static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; } |
664 |
+ |
|
665 |
+ |
/** |
666 |
+ |
* Returns a random non-null TimeUnit. |
667 |
+ |
*/ |
668 |
+ |
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
669 |
+ |
|
670 |
|
/** |
671 |
|
* Returns the shortest timed delay. This can be scaled up for |
672 |
< |
* slow machines using the jsr166.delay.factor system property. |
672 |
> |
* slow machines using the jsr166.delay.factor system property, |
673 |
> |
* or via jtreg's -timeoutFactor: flag. |
674 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
675 |
|
*/ |
676 |
|
protected long getShortDelay() { |
677 |
< |
return 50 * delayFactor; |
677 |
> |
return (long) (50 * delayFactor); |
678 |
|
} |
679 |
|
|
680 |
|
/** |
687 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
688 |
|
} |
689 |
|
|
690 |
+ |
private static final long TIMEOUT_DELAY_MS |
691 |
+ |
= (long) (12.0 * Math.cbrt(delayFactor)); |
692 |
+ |
|
693 |
|
/** |
694 |
< |
* Returns a timeout in milliseconds to be used in tests that |
695 |
< |
* verify that operations block or time out. |
694 |
> |
* Returns a timeout in milliseconds to be used in tests that verify |
695 |
> |
* that operations block or time out. We want this to be longer |
696 |
> |
* than the OS scheduling quantum, but not too long, so don't scale |
697 |
> |
* linearly with delayFactor; we use "crazy" cube root instead. |
698 |
|
*/ |
699 |
< |
long timeoutMillis() { |
700 |
< |
return SHORT_DELAY_MS / 4; |
699 |
> |
static long timeoutMillis() { |
700 |
> |
return TIMEOUT_DELAY_MS; |
701 |
|
} |
702 |
|
|
703 |
|
/** |
713 |
|
* The first exception encountered if any threadAssertXXX method fails. |
714 |
|
*/ |
715 |
|
private final AtomicReference<Throwable> threadFailure |
716 |
< |
= new AtomicReference<Throwable>(null); |
716 |
> |
= new AtomicReference<>(null); |
717 |
|
|
718 |
|
/** |
719 |
|
* Records an exception so that it can be rethrown later in the test |
735 |
|
String msg = toString() + ": " + String.format(format, args); |
736 |
|
System.err.println(msg); |
737 |
|
dumpTestThreads(); |
738 |
< |
throw new AssertionFailedError(msg); |
738 |
> |
throw new AssertionError(msg); |
739 |
|
} |
740 |
|
|
741 |
|
/** |
756 |
|
throw (RuntimeException) t; |
757 |
|
else if (t instanceof Exception) |
758 |
|
throw (Exception) t; |
759 |
< |
else { |
760 |
< |
AssertionFailedError afe = |
647 |
< |
new AssertionFailedError(t.toString()); |
648 |
< |
afe.initCause(t); |
649 |
< |
throw afe; |
650 |
< |
} |
759 |
> |
else |
760 |
> |
throw new AssertionError(t.toString(), t); |
761 |
|
} |
762 |
|
|
763 |
|
if (Thread.interrupted()) |
791 |
|
|
792 |
|
/** |
793 |
|
* Just like fail(reason), but additionally recording (using |
794 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
795 |
< |
* the current testcase will fail. |
794 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
795 |
> |
* current testcase will fail. |
796 |
|
*/ |
797 |
|
public void threadFail(String reason) { |
798 |
|
try { |
799 |
|
fail(reason); |
800 |
< |
} catch (AssertionFailedError t) { |
801 |
< |
threadRecordFailure(t); |
802 |
< |
throw t; |
800 |
> |
} catch (AssertionError fail) { |
801 |
> |
threadRecordFailure(fail); |
802 |
> |
throw fail; |
803 |
|
} |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
|
* Just like assertTrue(b), but additionally recording (using |
808 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
809 |
< |
* the current testcase will fail. |
808 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
809 |
> |
* current testcase will fail. |
810 |
|
*/ |
811 |
|
public void threadAssertTrue(boolean b) { |
812 |
|
try { |
813 |
|
assertTrue(b); |
814 |
< |
} catch (AssertionFailedError t) { |
815 |
< |
threadRecordFailure(t); |
816 |
< |
throw t; |
814 |
> |
} catch (AssertionError fail) { |
815 |
> |
threadRecordFailure(fail); |
816 |
> |
throw fail; |
817 |
|
} |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* Just like assertFalse(b), but additionally recording (using |
822 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
823 |
< |
* the current testcase will fail. |
822 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
823 |
> |
* current testcase will fail. |
824 |
|
*/ |
825 |
|
public void threadAssertFalse(boolean b) { |
826 |
|
try { |
827 |
|
assertFalse(b); |
828 |
< |
} catch (AssertionFailedError t) { |
829 |
< |
threadRecordFailure(t); |
830 |
< |
throw t; |
828 |
> |
} catch (AssertionError fail) { |
829 |
> |
threadRecordFailure(fail); |
830 |
> |
throw fail; |
831 |
|
} |
832 |
|
} |
833 |
|
|
834 |
|
/** |
835 |
|
* Just like assertNull(x), but additionally recording (using |
836 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
837 |
< |
* the current testcase will fail. |
836 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
837 |
> |
* current testcase will fail. |
838 |
|
*/ |
839 |
|
public void threadAssertNull(Object x) { |
840 |
|
try { |
841 |
|
assertNull(x); |
842 |
< |
} catch (AssertionFailedError t) { |
843 |
< |
threadRecordFailure(t); |
844 |
< |
throw t; |
842 |
> |
} catch (AssertionError fail) { |
843 |
> |
threadRecordFailure(fail); |
844 |
> |
throw fail; |
845 |
|
} |
846 |
|
} |
847 |
|
|
848 |
|
/** |
849 |
|
* Just like assertEquals(x, y), but additionally recording (using |
850 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
851 |
< |
* the current testcase will fail. |
850 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
851 |
> |
* current testcase will fail. |
852 |
|
*/ |
853 |
|
public void threadAssertEquals(long x, long y) { |
854 |
|
try { |
855 |
|
assertEquals(x, y); |
856 |
< |
} catch (AssertionFailedError t) { |
857 |
< |
threadRecordFailure(t); |
858 |
< |
throw t; |
856 |
> |
} catch (AssertionError fail) { |
857 |
> |
threadRecordFailure(fail); |
858 |
> |
throw fail; |
859 |
|
} |
860 |
|
} |
861 |
|
|
862 |
|
/** |
863 |
|
* Just like assertEquals(x, y), but additionally recording (using |
864 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
865 |
< |
* the current testcase will fail. |
864 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
865 |
> |
* current testcase will fail. |
866 |
|
*/ |
867 |
|
public void threadAssertEquals(Object x, Object y) { |
868 |
|
try { |
869 |
|
assertEquals(x, y); |
870 |
< |
} catch (AssertionFailedError fail) { |
870 |
> |
} catch (AssertionError fail) { |
871 |
|
threadRecordFailure(fail); |
872 |
|
throw fail; |
873 |
|
} catch (Throwable fail) { |
877 |
|
|
878 |
|
/** |
879 |
|
* Just like assertSame(x, y), but additionally recording (using |
880 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
881 |
< |
* the current testcase will fail. |
880 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
881 |
> |
* current testcase will fail. |
882 |
|
*/ |
883 |
|
public void threadAssertSame(Object x, Object y) { |
884 |
|
try { |
885 |
|
assertSame(x, y); |
886 |
< |
} catch (AssertionFailedError fail) { |
886 |
> |
} catch (AssertionError fail) { |
887 |
|
threadRecordFailure(fail); |
888 |
|
throw fail; |
889 |
|
} |
905 |
|
|
906 |
|
/** |
907 |
|
* Records the given exception using {@link #threadRecordFailure}, |
908 |
< |
* then rethrows the exception, wrapping it in an |
909 |
< |
* AssertionFailedError if necessary. |
908 |
> |
* then rethrows the exception, wrapping it in an AssertionError |
909 |
> |
* if necessary. |
910 |
|
*/ |
911 |
|
public void threadUnexpectedException(Throwable t) { |
912 |
|
threadRecordFailure(t); |
915 |
|
throw (RuntimeException) t; |
916 |
|
else if (t instanceof Error) |
917 |
|
throw (Error) t; |
918 |
< |
else { |
919 |
< |
AssertionFailedError afe = |
810 |
< |
new AssertionFailedError("unexpected exception: " + t); |
811 |
< |
afe.initCause(t); |
812 |
< |
throw afe; |
813 |
< |
} |
918 |
> |
else |
919 |
> |
throw new AssertionError("unexpected exception: " + t, t); |
920 |
|
} |
921 |
|
|
922 |
|
/** |
984 |
|
}}; |
985 |
|
} |
986 |
|
|
987 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
988 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
989 |
+ |
} |
990 |
+ |
|
991 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
992 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
993 |
+ |
} |
994 |
+ |
|
995 |
|
/** |
996 |
|
* Waits out termination of a thread pool or fails doing so. |
997 |
|
*/ |
1015 |
|
} |
1016 |
|
} |
1017 |
|
|
1018 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
1018 |
> |
/** |
1019 |
> |
* Like Runnable, but with the freedom to throw anything. |
1020 |
> |
* junit folks had the same idea: |
1021 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
1022 |
> |
*/ |
1023 |
|
interface Action { public void run() throws Throwable; } |
1024 |
|
|
1025 |
|
/** |
1050 |
|
* Uninteresting threads are filtered out. |
1051 |
|
*/ |
1052 |
|
static void dumpTestThreads() { |
1053 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1054 |
+ |
if (sm != null) { |
1055 |
+ |
try { |
1056 |
+ |
System.setSecurityManager(null); |
1057 |
+ |
} catch (SecurityException giveUp) { |
1058 |
+ |
return; |
1059 |
+ |
} |
1060 |
+ |
} |
1061 |
+ |
|
1062 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1063 |
|
System.err.println("------ stacktrace dump start ------"); |
1064 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1065 |
< |
String name = info.getThreadName(); |
1065 |
> |
final String name = info.getThreadName(); |
1066 |
> |
String lockName; |
1067 |
|
if ("Signal Dispatcher".equals(name)) |
1068 |
|
continue; |
1069 |
|
if ("Reference Handler".equals(name) |
1070 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1070 |
> |
&& (lockName = info.getLockName()) != null |
1071 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1072 |
|
continue; |
1073 |
|
if ("Finalizer".equals(name) |
1074 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1074 |
> |
&& (lockName = info.getLockName()) != null |
1075 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1076 |
|
continue; |
1077 |
|
if ("checkForWedgedTest".equals(name)) |
1078 |
|
continue; |
1079 |
|
System.err.print(info); |
1080 |
|
} |
1081 |
|
System.err.println("------ stacktrace dump end ------"); |
952 |
– |
} |
953 |
– |
|
954 |
– |
/** |
955 |
– |
* Checks that thread does not terminate within the default |
956 |
– |
* millisecond delay of {@code timeoutMillis()}. |
957 |
– |
*/ |
958 |
– |
void assertThreadStaysAlive(Thread thread) { |
959 |
– |
assertThreadStaysAlive(thread, timeoutMillis()); |
960 |
– |
} |
1082 |
|
|
1083 |
< |
/** |
963 |
< |
* Checks that thread does not terminate within the given millisecond delay. |
964 |
< |
*/ |
965 |
< |
void assertThreadStaysAlive(Thread thread, long millis) { |
966 |
< |
try { |
967 |
< |
// No need to optimize the failing case via Thread.join. |
968 |
< |
delay(millis); |
969 |
< |
assertTrue(thread.isAlive()); |
970 |
< |
} catch (InterruptedException fail) { |
971 |
< |
threadFail("Unexpected InterruptedException"); |
972 |
< |
} |
1083 |
> |
if (sm != null) System.setSecurityManager(sm); |
1084 |
|
} |
1085 |
|
|
1086 |
|
/** |
1087 |
< |
* Checks that the threads do not terminate within the default |
977 |
< |
* millisecond delay of {@code timeoutMillis()}. |
1087 |
> |
* Checks that thread eventually enters the expected blocked thread state. |
1088 |
|
*/ |
1089 |
< |
void assertThreadsStayAlive(Thread... threads) { |
1090 |
< |
assertThreadsStayAlive(timeoutMillis(), threads); |
1091 |
< |
} |
1092 |
< |
|
1093 |
< |
/** |
1094 |
< |
* Checks that the threads do not terminate within the given millisecond delay. |
1095 |
< |
*/ |
1096 |
< |
void assertThreadsStayAlive(long millis, Thread... threads) { |
1097 |
< |
try { |
1098 |
< |
// No need to optimize the failing case via Thread.join. |
1099 |
< |
delay(millis); |
1100 |
< |
for (Thread thread : threads) |
1101 |
< |
assertTrue(thread.isAlive()); |
992 |
< |
} catch (InterruptedException fail) { |
993 |
< |
threadFail("Unexpected InterruptedException"); |
1089 |
> |
void assertThreadBlocks(Thread thread, Thread.State expected) { |
1090 |
> |
// always sleep at least 1 ms, with high probability avoiding |
1091 |
> |
// transitory states |
1092 |
> |
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1093 |
> |
try { delay(1); } |
1094 |
> |
catch (InterruptedException fail) { |
1095 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1096 |
> |
} |
1097 |
> |
Thread.State s = thread.getState(); |
1098 |
> |
if (s == expected) |
1099 |
> |
return; |
1100 |
> |
else if (s == Thread.State.TERMINATED) |
1101 |
> |
fail("Unexpected thread termination"); |
1102 |
|
} |
1103 |
+ |
fail("timed out waiting for thread to enter thread state " + expected); |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
1141 |
|
} |
1142 |
|
|
1143 |
|
/** |
1144 |
+ |
* The maximum number of consecutive spurious wakeups we should |
1145 |
+ |
* tolerate (from APIs like LockSupport.park) before failing a test. |
1146 |
+ |
*/ |
1147 |
+ |
static final int MAX_SPURIOUS_WAKEUPS = 10; |
1148 |
+ |
|
1149 |
+ |
/** |
1150 |
|
* The number of elements to place in collections, arrays, etc. |
1151 |
|
*/ |
1152 |
|
public static final int SIZE = 20; |
1250 |
|
} |
1251 |
|
public void refresh() {} |
1252 |
|
public String toString() { |
1253 |
< |
List<Permission> ps = new ArrayList<Permission>(); |
1253 |
> |
List<Permission> ps = new ArrayList<>(); |
1254 |
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1255 |
|
ps.add(e.nextElement()); |
1256 |
|
return "AdjustablePolicy with permissions " + ps; |
1278 |
|
|
1279 |
|
/** |
1280 |
|
* Sleeps until the given time has elapsed. |
1281 |
< |
* Throws AssertionFailedError if interrupted. |
1281 |
> |
* Throws AssertionError if interrupted. |
1282 |
|
*/ |
1283 |
< |
void sleep(long millis) { |
1283 |
> |
static void sleep(long millis) { |
1284 |
|
try { |
1285 |
|
delay(millis); |
1286 |
|
} catch (InterruptedException fail) { |
1287 |
< |
AssertionFailedError afe = |
1173 |
< |
new AssertionFailedError("Unexpected InterruptedException"); |
1174 |
< |
afe.initCause(fail); |
1175 |
< |
throw afe; |
1287 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1288 |
|
} |
1289 |
|
} |
1290 |
|
|
1293 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1294 |
|
*/ |
1295 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1296 |
< |
long startTime = System.nanoTime(); |
1296 |
> |
long startTime = 0L; |
1297 |
|
for (;;) { |
1298 |
|
Thread.State s = thread.getState(); |
1299 |
|
if (s == Thread.State.BLOCKED || |
1302 |
|
return; |
1303 |
|
else if (s == Thread.State.TERMINATED) |
1304 |
|
fail("Unexpected thread termination"); |
1305 |
+ |
else if (startTime == 0L) |
1306 |
+ |
startTime = System.nanoTime(); |
1307 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1308 |
|
threadAssertTrue(thread.isAlive()); |
1309 |
< |
return; |
1309 |
> |
fail("timed out waiting for thread to enter wait state"); |
1310 |
|
} |
1311 |
|
Thread.yield(); |
1312 |
|
} |
1313 |
|
} |
1314 |
|
|
1315 |
|
/** |
1316 |
< |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1317 |
< |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1316 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
1317 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1318 |
> |
* and additionally satisfy the given condition. |
1319 |
> |
*/ |
1320 |
> |
void waitForThreadToEnterWaitState( |
1321 |
> |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1322 |
> |
long startTime = 0L; |
1323 |
> |
for (;;) { |
1324 |
> |
Thread.State s = thread.getState(); |
1325 |
> |
if (s == Thread.State.BLOCKED || |
1326 |
> |
s == Thread.State.WAITING || |
1327 |
> |
s == Thread.State.TIMED_WAITING) { |
1328 |
> |
try { |
1329 |
> |
if (waitingForGodot.call()) |
1330 |
> |
return; |
1331 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1332 |
> |
} |
1333 |
> |
else if (s == Thread.State.TERMINATED) |
1334 |
> |
fail("Unexpected thread termination"); |
1335 |
> |
else if (startTime == 0L) |
1336 |
> |
startTime = System.nanoTime(); |
1337 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1338 |
> |
threadAssertTrue(thread.isAlive()); |
1339 |
> |
fail("timed out waiting for thread to enter wait state"); |
1340 |
> |
} |
1341 |
> |
Thread.yield(); |
1342 |
> |
} |
1343 |
> |
} |
1344 |
> |
|
1345 |
> |
/** |
1346 |
> |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1347 |
> |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1348 |
|
*/ |
1349 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1350 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1351 |
|
} |
1352 |
|
|
1353 |
|
/** |
1354 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1355 |
+ |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1356 |
+ |
* and additionally satisfy the given condition. |
1357 |
+ |
*/ |
1358 |
+ |
void waitForThreadToEnterWaitState( |
1359 |
+ |
Thread thread, Callable<Boolean> waitingForGodot) { |
1360 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1361 |
+ |
} |
1362 |
+ |
|
1363 |
+ |
/** |
1364 |
|
* Returns the number of milliseconds since time given by |
1365 |
|
* startNanoTime, which must have been previously returned from a |
1366 |
|
* call to {@link System#nanoTime()}. |
1375 |
|
// r.run(); |
1376 |
|
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1377 |
|
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1378 |
< |
// throw new AssertionFailedError("did not return promptly"); |
1378 |
> |
// throw new AssertionError("did not return promptly"); |
1379 |
|
// } |
1380 |
|
|
1381 |
|
// void assertTerminatesPromptly(Runnable r) { |
1388 |
|
*/ |
1389 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1390 |
|
long startTime = System.nanoTime(); |
1391 |
+ |
T actual = null; |
1392 |
|
try { |
1393 |
< |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1393 |
> |
actual = f.get(timeoutMillis, MILLISECONDS); |
1394 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1395 |
+ |
assertEquals(expectedValue, actual); |
1396 |
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1397 |
< |
throw new AssertionFailedError("timed get did not return promptly"); |
1397 |
> |
throw new AssertionError("timed get did not return promptly"); |
1398 |
|
} |
1399 |
|
|
1400 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1452 |
|
} |
1453 |
|
} |
1454 |
|
|
1299 |
– |
public abstract class RunnableShouldThrow implements Runnable { |
1300 |
– |
protected abstract void realRun() throws Throwable; |
1301 |
– |
|
1302 |
– |
final Class<?> exceptionClass; |
1303 |
– |
|
1304 |
– |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
1305 |
– |
this.exceptionClass = exceptionClass; |
1306 |
– |
} |
1307 |
– |
|
1308 |
– |
public final void run() { |
1309 |
– |
try { |
1310 |
– |
realRun(); |
1311 |
– |
threadShouldThrow(exceptionClass.getSimpleName()); |
1312 |
– |
} catch (Throwable t) { |
1313 |
– |
if (! exceptionClass.isInstance(t)) |
1314 |
– |
threadUnexpectedException(t); |
1315 |
– |
} |
1316 |
– |
} |
1317 |
– |
} |
1318 |
– |
|
1455 |
|
public abstract class ThreadShouldThrow extends Thread { |
1456 |
|
protected abstract void realRun() throws Throwable; |
1457 |
|
|
1570 |
|
return new LatchAwaiter(latch); |
1571 |
|
} |
1572 |
|
|
1573 |
< |
public void await(CountDownLatch latch) { |
1573 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1574 |
|
try { |
1575 |
< |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1575 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1576 |
|
fail("timed out waiting for CountDownLatch for " |
1577 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1577 |
> |
+ (timeoutMillis/1000) + " sec"); |
1578 |
|
} catch (Throwable fail) { |
1579 |
|
threadUnexpectedException(fail); |
1580 |
|
} |
1581 |
|
} |
1582 |
|
|
1583 |
+ |
public void await(CountDownLatch latch) { |
1584 |
+ |
await(latch, LONG_DELAY_MS); |
1585 |
+ |
} |
1586 |
+ |
|
1587 |
|
public void await(Semaphore semaphore) { |
1588 |
|
try { |
1589 |
|
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1594 |
|
} |
1595 |
|
} |
1596 |
|
|
1597 |
+ |
public void await(CyclicBarrier barrier) { |
1598 |
+ |
try { |
1599 |
+ |
barrier.await(LONG_DELAY_MS, MILLISECONDS); |
1600 |
+ |
} catch (Throwable fail) { |
1601 |
+ |
threadUnexpectedException(fail); |
1602 |
+ |
} |
1603 |
+ |
} |
1604 |
+ |
|
1605 |
|
// /** |
1606 |
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1607 |
|
// */ |
1616 |
|
// long startTime = System.nanoTime(); |
1617 |
|
// while (!flag.get()) { |
1618 |
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1619 |
< |
// throw new AssertionFailedError("timed out"); |
1619 |
> |
// throw new AssertionError("timed out"); |
1620 |
|
// Thread.yield(); |
1621 |
|
// } |
1622 |
|
// } |
1625 |
|
public String call() { throw new NullPointerException(); } |
1626 |
|
} |
1627 |
|
|
1480 |
– |
public static class CallableOne implements Callable<Integer> { |
1481 |
– |
public Integer call() { return one; } |
1482 |
– |
} |
1483 |
– |
|
1484 |
– |
public class ShortRunnable extends CheckedRunnable { |
1485 |
– |
protected void realRun() throws Throwable { |
1486 |
– |
delay(SHORT_DELAY_MS); |
1487 |
– |
} |
1488 |
– |
} |
1489 |
– |
|
1490 |
– |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1491 |
– |
protected void realRun() throws InterruptedException { |
1492 |
– |
delay(SHORT_DELAY_MS); |
1493 |
– |
} |
1494 |
– |
} |
1495 |
– |
|
1496 |
– |
public class SmallRunnable extends CheckedRunnable { |
1497 |
– |
protected void realRun() throws Throwable { |
1498 |
– |
delay(SMALL_DELAY_MS); |
1499 |
– |
} |
1500 |
– |
} |
1501 |
– |
|
1628 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1629 |
|
protected void realRun() { |
1630 |
|
try { |
1633 |
|
} |
1634 |
|
} |
1635 |
|
|
1510 |
– |
public class SmallCallable extends CheckedCallable { |
1511 |
– |
protected Object realCall() throws InterruptedException { |
1512 |
– |
delay(SMALL_DELAY_MS); |
1513 |
– |
return Boolean.TRUE; |
1514 |
– |
} |
1515 |
– |
} |
1516 |
– |
|
1517 |
– |
public class MediumRunnable extends CheckedRunnable { |
1518 |
– |
protected void realRun() throws Throwable { |
1519 |
– |
delay(MEDIUM_DELAY_MS); |
1520 |
– |
} |
1521 |
– |
} |
1522 |
– |
|
1523 |
– |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1524 |
– |
protected void realRun() throws InterruptedException { |
1525 |
– |
delay(MEDIUM_DELAY_MS); |
1526 |
– |
} |
1527 |
– |
} |
1528 |
– |
|
1636 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1637 |
|
return new CheckedRunnable() { |
1638 |
|
protected void realRun() { |
1642 |
|
}}; |
1643 |
|
} |
1644 |
|
|
1538 |
– |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1539 |
– |
protected void realRun() { |
1540 |
– |
try { |
1541 |
– |
delay(MEDIUM_DELAY_MS); |
1542 |
– |
} catch (InterruptedException ok) {} |
1543 |
– |
} |
1544 |
– |
} |
1545 |
– |
|
1546 |
– |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1547 |
– |
protected void realRun() { |
1548 |
– |
try { |
1549 |
– |
delay(LONG_DELAY_MS); |
1550 |
– |
} catch (InterruptedException ok) {} |
1551 |
– |
} |
1552 |
– |
} |
1553 |
– |
|
1645 |
|
/** |
1646 |
|
* For use as ThreadFactory in constructors |
1647 |
|
*/ |
1655 |
|
boolean isDone(); |
1656 |
|
} |
1657 |
|
|
1567 |
– |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1568 |
– |
return new TrackedRunnable() { |
1569 |
– |
private volatile boolean done = false; |
1570 |
– |
public boolean isDone() { return done; } |
1571 |
– |
public void run() { |
1572 |
– |
try { |
1573 |
– |
delay(timeoutMillis); |
1574 |
– |
done = true; |
1575 |
– |
} catch (InterruptedException ok) {} |
1576 |
– |
} |
1577 |
– |
}; |
1578 |
– |
} |
1579 |
– |
|
1580 |
– |
public static class TrackedShortRunnable implements Runnable { |
1581 |
– |
public volatile boolean done = false; |
1582 |
– |
public void run() { |
1583 |
– |
try { |
1584 |
– |
delay(SHORT_DELAY_MS); |
1585 |
– |
done = true; |
1586 |
– |
} catch (InterruptedException ok) {} |
1587 |
– |
} |
1588 |
– |
} |
1589 |
– |
|
1590 |
– |
public static class TrackedSmallRunnable implements Runnable { |
1591 |
– |
public volatile boolean done = false; |
1592 |
– |
public void run() { |
1593 |
– |
try { |
1594 |
– |
delay(SMALL_DELAY_MS); |
1595 |
– |
done = true; |
1596 |
– |
} catch (InterruptedException ok) {} |
1597 |
– |
} |
1598 |
– |
} |
1599 |
– |
|
1600 |
– |
public static class TrackedMediumRunnable implements Runnable { |
1601 |
– |
public volatile boolean done = false; |
1602 |
– |
public void run() { |
1603 |
– |
try { |
1604 |
– |
delay(MEDIUM_DELAY_MS); |
1605 |
– |
done = true; |
1606 |
– |
} catch (InterruptedException ok) {} |
1607 |
– |
} |
1608 |
– |
} |
1609 |
– |
|
1610 |
– |
public static class TrackedLongRunnable implements Runnable { |
1611 |
– |
public volatile boolean done = false; |
1612 |
– |
public void run() { |
1613 |
– |
try { |
1614 |
– |
delay(LONG_DELAY_MS); |
1615 |
– |
done = true; |
1616 |
– |
} catch (InterruptedException ok) {} |
1617 |
– |
} |
1618 |
– |
} |
1619 |
– |
|
1658 |
|
public static class TrackedNoOpRunnable implements Runnable { |
1659 |
|
public volatile boolean done = false; |
1660 |
|
public void run() { |
1662 |
|
} |
1663 |
|
} |
1664 |
|
|
1627 |
– |
public static class TrackedCallable implements Callable { |
1628 |
– |
public volatile boolean done = false; |
1629 |
– |
public Object call() { |
1630 |
– |
try { |
1631 |
– |
delay(SMALL_DELAY_MS); |
1632 |
– |
done = true; |
1633 |
– |
} catch (InterruptedException ok) {} |
1634 |
– |
return Boolean.TRUE; |
1635 |
– |
} |
1636 |
– |
} |
1637 |
– |
|
1665 |
|
/** |
1666 |
|
* Analog of CheckedRunnable for RecursiveAction |
1667 |
|
*/ |
1703 |
|
|
1704 |
|
/** |
1705 |
|
* A CyclicBarrier that uses timed await and fails with |
1706 |
< |
* AssertionFailedErrors instead of throwing checked exceptions. |
1706 |
> |
* AssertionErrors instead of throwing checked exceptions. |
1707 |
|
*/ |
1708 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1708 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1709 |
|
public CheckedBarrier(int parties) { super(parties); } |
1710 |
|
|
1711 |
|
public int await() { |
1712 |
|
try { |
1713 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1714 |
|
} catch (TimeoutException timedOut) { |
1715 |
< |
throw new AssertionFailedError("timed out"); |
1715 |
> |
throw new AssertionError("timed out"); |
1716 |
|
} catch (Exception fail) { |
1717 |
< |
AssertionFailedError afe = |
1691 |
< |
new AssertionFailedError("Unexpected exception: " + fail); |
1692 |
< |
afe.initCause(fail); |
1693 |
< |
throw afe; |
1717 |
> |
throw new AssertionError("Unexpected exception: " + fail, fail); |
1718 |
|
} |
1719 |
|
} |
1720 |
|
} |
1725 |
|
assertEquals(0, q.size()); |
1726 |
|
assertNull(q.peek()); |
1727 |
|
assertNull(q.poll()); |
1728 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
1728 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
1729 |
|
assertEquals(q.toString(), "[]"); |
1730 |
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1731 |
|
assertFalse(q.iterator().hasNext()); |
1766 |
|
} |
1767 |
|
} |
1768 |
|
|
1769 |
+ |
void assertImmutable(final Object o) { |
1770 |
+ |
if (o instanceof Collection) { |
1771 |
+ |
assertThrows( |
1772 |
+ |
UnsupportedOperationException.class, |
1773 |
+ |
new Runnable() { public void run() { |
1774 |
+ |
((Collection) o).add(null);}}); |
1775 |
+ |
} |
1776 |
+ |
} |
1777 |
+ |
|
1778 |
|
@SuppressWarnings("unchecked") |
1779 |
|
<T> T serialClone(T o) { |
1780 |
|
try { |
1781 |
|
ObjectInputStream ois = new ObjectInputStream |
1782 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1783 |
|
T clone = (T) ois.readObject(); |
1784 |
+ |
if (o == clone) assertImmutable(o); |
1785 |
|
assertSame(o.getClass(), clone.getClass()); |
1786 |
|
return clone; |
1787 |
|
} catch (Throwable fail) { |
1790 |
|
} |
1791 |
|
} |
1792 |
|
|
1793 |
+ |
/** |
1794 |
+ |
* A version of serialClone that leaves error handling (for |
1795 |
+ |
* e.g. NotSerializableException) up to the caller. |
1796 |
+ |
*/ |
1797 |
+ |
@SuppressWarnings("unchecked") |
1798 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1799 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1800 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1801 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1802 |
+ |
oos.writeObject(o); |
1803 |
+ |
oos.flush(); |
1804 |
+ |
oos.close(); |
1805 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1806 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1807 |
+ |
T clone = (T) ois.readObject(); |
1808 |
+ |
if (o == clone) assertImmutable(o); |
1809 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1810 |
+ |
return clone; |
1811 |
+ |
} |
1812 |
+ |
|
1813 |
+ |
/** |
1814 |
+ |
* If o implements Cloneable and has a public clone method, |
1815 |
+ |
* returns a clone of o, else null. |
1816 |
+ |
*/ |
1817 |
+ |
@SuppressWarnings("unchecked") |
1818 |
+ |
<T> T cloneableClone(T o) { |
1819 |
+ |
if (!(o instanceof Cloneable)) return null; |
1820 |
+ |
final T clone; |
1821 |
+ |
try { |
1822 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1823 |
+ |
} catch (NoSuchMethodException ok) { |
1824 |
+ |
return null; |
1825 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1826 |
+ |
throw new Error(unexpected); |
1827 |
+ |
} |
1828 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1829 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1830 |
+ |
return clone; |
1831 |
+ |
} |
1832 |
+ |
|
1833 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1834 |
|
Runnable... throwingActions) { |
1835 |
|
for (Runnable throwingAction : throwingActions) { |
1837 |
|
try { throwingAction.run(); } |
1838 |
|
catch (Throwable t) { |
1839 |
|
threw = true; |
1840 |
< |
if (!expectedExceptionClass.isInstance(t)) { |
1841 |
< |
AssertionFailedError afe = |
1842 |
< |
new AssertionFailedError |
1843 |
< |
("Expected " + expectedExceptionClass.getName() + |
1844 |
< |
", got " + t.getClass().getName()); |
1771 |
< |
afe.initCause(t); |
1772 |
< |
threadUnexpectedException(afe); |
1773 |
< |
} |
1840 |
> |
if (!expectedExceptionClass.isInstance(t)) |
1841 |
> |
throw new AssertionError( |
1842 |
> |
"Expected " + expectedExceptionClass.getName() + |
1843 |
> |
", got " + t.getClass().getName(), |
1844 |
> |
t); |
1845 |
|
} |
1846 |
|
if (!threw) |
1847 |
|
shouldThrow(expectedExceptionClass.getName()); |
1855 |
|
} catch (NoSuchElementException success) {} |
1856 |
|
assertFalse(it.hasNext()); |
1857 |
|
} |
1858 |
+ |
|
1859 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1860 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1861 |
+ |
} |
1862 |
+ |
|
1863 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1864 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1865 |
+ |
} |
1866 |
+ |
|
1867 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1868 |
+ |
static final ExecutorService cachedThreadPool = |
1869 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1870 |
+ |
1000L, MILLISECONDS, |
1871 |
+ |
new SynchronousQueue<Runnable>()); |
1872 |
+ |
|
1873 |
+ |
static <T> void shuffle(T[] array) { |
1874 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1875 |
+ |
} |
1876 |
+ |
|
1877 |
+ |
/** |
1878 |
+ |
* Returns the same String as would be returned by {@link |
1879 |
+ |
* Object#toString}, whether or not the given object's class |
1880 |
+ |
* overrides toString(). |
1881 |
+ |
* |
1882 |
+ |
* @see System#identityHashCode |
1883 |
+ |
*/ |
1884 |
+ |
static String identityString(Object x) { |
1885 |
+ |
return x.getClass().getName() |
1886 |
+ |
+ "@" + Integer.toHexString(System.identityHashCode(x)); |
1887 |
+ |
} |
1888 |
+ |
|
1889 |
+ |
// --- Shared assertions for Executor tests --- |
1890 |
+ |
|
1891 |
+ |
/** |
1892 |
+ |
* Returns maximum number of tasks that can be submitted to given |
1893 |
+ |
* pool (with bounded queue) before saturation (when submission |
1894 |
+ |
* throws RejectedExecutionException). |
1895 |
+ |
*/ |
1896 |
+ |
static final int saturatedSize(ThreadPoolExecutor pool) { |
1897 |
+ |
BlockingQueue<Runnable> q = pool.getQueue(); |
1898 |
+ |
return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity(); |
1899 |
+ |
} |
1900 |
+ |
|
1901 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1902 |
+ |
void assertNullTaskSubmissionThrowsNullPointerException(Executor e) { |
1903 |
+ |
try { |
1904 |
+ |
e.execute((Runnable) null); |
1905 |
+ |
shouldThrow(); |
1906 |
+ |
} catch (NullPointerException success) {} |
1907 |
+ |
|
1908 |
+ |
if (! (e instanceof ExecutorService)) return; |
1909 |
+ |
ExecutorService es = (ExecutorService) e; |
1910 |
+ |
try { |
1911 |
+ |
es.submit((Runnable) null); |
1912 |
+ |
shouldThrow(); |
1913 |
+ |
} catch (NullPointerException success) {} |
1914 |
+ |
try { |
1915 |
+ |
es.submit((Runnable) null, Boolean.TRUE); |
1916 |
+ |
shouldThrow(); |
1917 |
+ |
} catch (NullPointerException success) {} |
1918 |
+ |
try { |
1919 |
+ |
es.submit((Callable) null); |
1920 |
+ |
shouldThrow(); |
1921 |
+ |
} catch (NullPointerException success) {} |
1922 |
+ |
|
1923 |
+ |
if (! (e instanceof ScheduledExecutorService)) return; |
1924 |
+ |
ScheduledExecutorService ses = (ScheduledExecutorService) e; |
1925 |
+ |
try { |
1926 |
+ |
ses.schedule((Runnable) null, |
1927 |
+ |
randomTimeout(), randomTimeUnit()); |
1928 |
+ |
shouldThrow(); |
1929 |
+ |
} catch (NullPointerException success) {} |
1930 |
+ |
try { |
1931 |
+ |
ses.schedule((Callable) null, |
1932 |
+ |
randomTimeout(), randomTimeUnit()); |
1933 |
+ |
shouldThrow(); |
1934 |
+ |
} catch (NullPointerException success) {} |
1935 |
+ |
try { |
1936 |
+ |
ses.scheduleAtFixedRate((Runnable) null, |
1937 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1938 |
+ |
shouldThrow(); |
1939 |
+ |
} catch (NullPointerException success) {} |
1940 |
+ |
try { |
1941 |
+ |
ses.scheduleWithFixedDelay((Runnable) null, |
1942 |
+ |
randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
1943 |
+ |
shouldThrow(); |
1944 |
+ |
} catch (NullPointerException success) {} |
1945 |
+ |
} |
1946 |
+ |
|
1947 |
+ |
void setRejectedExecutionHandler( |
1948 |
+ |
ThreadPoolExecutor p, RejectedExecutionHandler handler) { |
1949 |
+ |
p.setRejectedExecutionHandler(handler); |
1950 |
+ |
assertSame(handler, p.getRejectedExecutionHandler()); |
1951 |
+ |
} |
1952 |
+ |
|
1953 |
+ |
void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) { |
1954 |
+ |
final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler(); |
1955 |
+ |
final long savedTaskCount = p.getTaskCount(); |
1956 |
+ |
final long savedCompletedTaskCount = p.getCompletedTaskCount(); |
1957 |
+ |
final int savedQueueSize = p.getQueue().size(); |
1958 |
+ |
final boolean stock = (p.getClass().getClassLoader() == null); |
1959 |
+ |
|
1960 |
+ |
Runnable r = () -> {}; |
1961 |
+ |
Callable<Boolean> c = () -> Boolean.TRUE; |
1962 |
+ |
|
1963 |
+ |
class Recorder implements RejectedExecutionHandler { |
1964 |
+ |
public volatile Runnable r = null; |
1965 |
+ |
public volatile ThreadPoolExecutor p = null; |
1966 |
+ |
public void reset() { r = null; p = null; } |
1967 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor p) { |
1968 |
+ |
assertNull(this.r); |
1969 |
+ |
assertNull(this.p); |
1970 |
+ |
this.r = r; |
1971 |
+ |
this.p = p; |
1972 |
+ |
} |
1973 |
+ |
} |
1974 |
+ |
|
1975 |
+ |
// check custom handler is invoked exactly once per task |
1976 |
+ |
Recorder recorder = new Recorder(); |
1977 |
+ |
setRejectedExecutionHandler(p, recorder); |
1978 |
+ |
for (int i = 2; i--> 0; ) { |
1979 |
+ |
recorder.reset(); |
1980 |
+ |
p.execute(r); |
1981 |
+ |
if (stock && p.getClass() == ThreadPoolExecutor.class) |
1982 |
+ |
assertSame(r, recorder.r); |
1983 |
+ |
assertSame(p, recorder.p); |
1984 |
+ |
|
1985 |
+ |
recorder.reset(); |
1986 |
+ |
assertFalse(p.submit(r).isDone()); |
1987 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
1988 |
+ |
assertSame(p, recorder.p); |
1989 |
+ |
|
1990 |
+ |
recorder.reset(); |
1991 |
+ |
assertFalse(p.submit(r, Boolean.TRUE).isDone()); |
1992 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
1993 |
+ |
assertSame(p, recorder.p); |
1994 |
+ |
|
1995 |
+ |
recorder.reset(); |
1996 |
+ |
assertFalse(p.submit(c).isDone()); |
1997 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
1998 |
+ |
assertSame(p, recorder.p); |
1999 |
+ |
|
2000 |
+ |
if (p instanceof ScheduledExecutorService) { |
2001 |
+ |
ScheduledExecutorService s = (ScheduledExecutorService) p; |
2002 |
+ |
ScheduledFuture<?> future; |
2003 |
+ |
|
2004 |
+ |
recorder.reset(); |
2005 |
+ |
future = s.schedule(r, randomTimeout(), randomTimeUnit()); |
2006 |
+ |
assertFalse(future.isDone()); |
2007 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2008 |
+ |
assertSame(p, recorder.p); |
2009 |
+ |
|
2010 |
+ |
recorder.reset(); |
2011 |
+ |
future = s.schedule(c, randomTimeout(), randomTimeUnit()); |
2012 |
+ |
assertFalse(future.isDone()); |
2013 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2014 |
+ |
assertSame(p, recorder.p); |
2015 |
+ |
|
2016 |
+ |
recorder.reset(); |
2017 |
+ |
future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2018 |
+ |
assertFalse(future.isDone()); |
2019 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2020 |
+ |
assertSame(p, recorder.p); |
2021 |
+ |
|
2022 |
+ |
recorder.reset(); |
2023 |
+ |
future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS); |
2024 |
+ |
assertFalse(future.isDone()); |
2025 |
+ |
if (stock) assertTrue(!((FutureTask) recorder.r).isDone()); |
2026 |
+ |
assertSame(p, recorder.p); |
2027 |
+ |
} |
2028 |
+ |
} |
2029 |
+ |
|
2030 |
+ |
// Checking our custom handler above should be sufficient, but |
2031 |
+ |
// we add some integration tests of standard handlers. |
2032 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
2033 |
+ |
final Runnable setThread = () -> thread.set(Thread.currentThread()); |
2034 |
+ |
|
2035 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy()); |
2036 |
+ |
try { |
2037 |
+ |
p.execute(setThread); |
2038 |
+ |
shouldThrow(); |
2039 |
+ |
} catch (RejectedExecutionException success) {} |
2040 |
+ |
assertNull(thread.get()); |
2041 |
+ |
|
2042 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy()); |
2043 |
+ |
p.execute(setThread); |
2044 |
+ |
assertNull(thread.get()); |
2045 |
+ |
|
2046 |
+ |
setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy()); |
2047 |
+ |
p.execute(setThread); |
2048 |
+ |
if (p.isShutdown()) |
2049 |
+ |
assertNull(thread.get()); |
2050 |
+ |
else |
2051 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
2052 |
+ |
|
2053 |
+ |
setRejectedExecutionHandler(p, savedHandler); |
2054 |
+ |
|
2055 |
+ |
// check that pool was not perturbed by handlers |
2056 |
+ |
assertEquals(savedTaskCount, p.getTaskCount()); |
2057 |
+ |
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2058 |
+ |
assertEquals(savedQueueSize, p.getQueue().size()); |
2059 |
+ |
} |
2060 |
+ |
|
2061 |
+ |
void assertCollectionsEquals(Collection<?> x, Collection<?> y) { |
2062 |
+ |
assertEquals(x, y); |
2063 |
+ |
assertEquals(y, x); |
2064 |
+ |
assertEquals(x.isEmpty(), y.isEmpty()); |
2065 |
+ |
assertEquals(x.size(), y.size()); |
2066 |
+ |
if (x instanceof List) { |
2067 |
+ |
assertEquals(x.toString(), y.toString()); |
2068 |
+ |
} |
2069 |
+ |
if (x instanceof List || x instanceof Set) { |
2070 |
+ |
assertEquals(x.hashCode(), y.hashCode()); |
2071 |
+ |
} |
2072 |
+ |
if (x instanceof List || x instanceof Deque) { |
2073 |
+ |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2074 |
+ |
assertTrue(Arrays.equals(x.toArray(new Object[0]), |
2075 |
+ |
y.toArray(new Object[0]))); |
2076 |
+ |
} |
2077 |
+ |
} |
2078 |
+ |
|
2079 |
+ |
/** |
2080 |
+ |
* A weaker form of assertCollectionsEquals which does not insist |
2081 |
+ |
* that the two collections satisfy Object#equals(Object), since |
2082 |
+ |
* they may use identity semantics as Deques do. |
2083 |
+ |
*/ |
2084 |
+ |
void assertCollectionsEquivalent(Collection<?> x, Collection<?> y) { |
2085 |
+ |
if (x instanceof List || x instanceof Set) |
2086 |
+ |
assertCollectionsEquals(x, y); |
2087 |
+ |
else { |
2088 |
+ |
assertEquals(x.isEmpty(), y.isEmpty()); |
2089 |
+ |
assertEquals(x.size(), y.size()); |
2090 |
+ |
assertEquals(new HashSet(x), new HashSet(y)); |
2091 |
+ |
if (x instanceof Deque) { |
2092 |
+ |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2093 |
+ |
assertTrue(Arrays.equals(x.toArray(new Object[0]), |
2094 |
+ |
y.toArray(new Object[0]))); |
2095 |
+ |
} |
2096 |
+ |
} |
2097 |
+ |
} |
2098 |
|
} |