6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
/* |
10 |
+ |
* @test |
11 |
+ |
* @summary JSR-166 tck tests (conformance testing mode) |
12 |
+ |
* @build * |
13 |
+ |
* @modules java.management |
14 |
+ |
* @run junit/othervm/timeout=1000 JSR166TestCase |
15 |
+ |
*/ |
16 |
+ |
|
17 |
+ |
/* |
18 |
+ |
* @test |
19 |
+ |
* @summary JSR-166 tck tests (whitebox tests allowed) |
20 |
+ |
* @build * |
21 |
+ |
* @modules java.base/java.util.concurrent:open |
22 |
+ |
* java.management |
23 |
+ |
* @run junit/othervm/timeout=1000 |
24 |
+ |
* -Djsr166.testImplementationDetails=true |
25 |
+ |
* JSR166TestCase |
26 |
+ |
* @run junit/othervm/timeout=1000 |
27 |
+ |
* -Djsr166.testImplementationDetails=true |
28 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 |
29 |
+ |
* JSR166TestCase |
30 |
+ |
* @run junit/othervm/timeout=1000 |
31 |
+ |
* -Djsr166.testImplementationDetails=true |
32 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 |
33 |
+ |
* -Djava.util.secureRandomSeed=true |
34 |
+ |
* JSR166TestCase |
35 |
+ |
*/ |
36 |
+ |
|
37 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
38 |
|
import static java.util.concurrent.TimeUnit.MINUTES; |
39 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
59 |
|
import java.security.SecurityPermission; |
60 |
|
import java.util.ArrayList; |
61 |
|
import java.util.Arrays; |
62 |
+ |
import java.util.Collection; |
63 |
+ |
import java.util.Collections; |
64 |
|
import java.util.Date; |
65 |
|
import java.util.Enumeration; |
66 |
|
import java.util.Iterator; |
80 |
|
import java.util.concurrent.RecursiveTask; |
81 |
|
import java.util.concurrent.RejectedExecutionHandler; |
82 |
|
import java.util.concurrent.Semaphore; |
83 |
+ |
import java.util.concurrent.SynchronousQueue; |
84 |
|
import java.util.concurrent.ThreadFactory; |
85 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
86 |
|
import java.util.concurrent.ThreadPoolExecutor; |
87 |
|
import java.util.concurrent.TimeoutException; |
88 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
89 |
|
import java.util.concurrent.atomic.AtomicReference; |
90 |
|
import java.util.regex.Matcher; |
91 |
|
import java.util.regex.Pattern; |
209 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
210 |
|
|
211 |
|
/** |
212 |
< |
* The scaling factor to apply to standard delays used in tests. |
212 |
> |
* Returns the value of the system property, or NaN if not defined. |
213 |
|
*/ |
214 |
< |
private static final int delayFactor = |
215 |
< |
Integer.getInteger("jsr166.delay.factor", 1); |
214 |
> |
private static float systemPropertyValue(String name) { |
215 |
> |
String floatString = System.getProperty(name); |
216 |
> |
if (floatString == null) |
217 |
> |
return Float.NaN; |
218 |
> |
try { |
219 |
> |
return Float.parseFloat(floatString); |
220 |
> |
} catch (NumberFormatException ex) { |
221 |
> |
throw new IllegalArgumentException( |
222 |
> |
String.format("Bad float value in system property %s=%s", |
223 |
> |
name, floatString)); |
224 |
> |
} |
225 |
> |
} |
226 |
> |
|
227 |
> |
/** |
228 |
> |
* The scaling factor to apply to standard delays used in tests. |
229 |
> |
* May be initialized from any of: |
230 |
> |
* - the "jsr166.delay.factor" system property |
231 |
> |
* - the "test.timeout.factor" system property (as used by jtreg) |
232 |
> |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
233 |
> |
* - hard-coded fuzz factor when using a known slowpoke VM |
234 |
> |
*/ |
235 |
> |
private static final float delayFactor = delayFactor(); |
236 |
> |
|
237 |
> |
private static float delayFactor() { |
238 |
> |
float x; |
239 |
> |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
240 |
> |
return x; |
241 |
> |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
242 |
> |
return x; |
243 |
> |
String prop = System.getProperty("java.vm.version"); |
244 |
> |
if (prop != null && prop.matches(".*debug.*")) |
245 |
> |
return 4.0f; // How much slower is fastdebug than product?! |
246 |
> |
return 1.0f; |
247 |
> |
} |
248 |
|
|
249 |
|
public JSR166TestCase() { super(); } |
250 |
|
public JSR166TestCase(String name) { super(name); } |
466 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
467 |
|
ArrayBlockingQueueTest.suite(), |
468 |
|
ArrayDequeTest.suite(), |
469 |
+ |
ArrayListTest.suite(), |
470 |
|
AtomicBooleanTest.suite(), |
471 |
|
AtomicIntegerArrayTest.suite(), |
472 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
489 |
|
CopyOnWriteArrayListTest.suite(), |
490 |
|
CopyOnWriteArraySetTest.suite(), |
491 |
|
CountDownLatchTest.suite(), |
492 |
+ |
CountedCompleterTest.suite(), |
493 |
|
CyclicBarrierTest.suite(), |
494 |
|
DelayQueueTest.suite(), |
495 |
|
EntryTest.suite(), |
518 |
|
TreeMapTest.suite(), |
519 |
|
TreeSetTest.suite(), |
520 |
|
TreeSubMapTest.suite(), |
521 |
< |
TreeSubSetTest.suite()); |
521 |
> |
TreeSubSetTest.suite(), |
522 |
> |
VectorTest.suite()); |
523 |
|
|
524 |
|
// Java8+ test classes |
525 |
|
if (atLeastJava8()) { |
526 |
|
String[] java8TestClassNames = { |
527 |
+ |
"ArrayDeque8Test", |
528 |
|
"Atomic8Test", |
529 |
|
"CompletableFutureTest", |
530 |
|
"ConcurrentHashMap8Test", |
531 |
< |
"CountedCompleterTest", |
531 |
> |
"CountedCompleter8Test", |
532 |
|
"DoubleAccumulatorTest", |
533 |
|
"DoubleAdderTest", |
534 |
|
"ForkJoinPool8Test", |
535 |
|
"ForkJoinTask8Test", |
536 |
+ |
"LinkedBlockingDeque8Test", |
537 |
+ |
"LinkedBlockingQueue8Test", |
538 |
|
"LongAccumulatorTest", |
539 |
|
"LongAdderTest", |
540 |
|
"SplittableRandomTest", |
541 |
|
"StampedLockTest", |
542 |
|
"SubmissionPublisherTest", |
543 |
|
"ThreadLocalRandom8Test", |
544 |
+ |
"TimeUnit8Test", |
545 |
|
}; |
546 |
|
addNamedTestClasses(suite, java8TestClassNames); |
547 |
|
} |
549 |
|
// Java9+ test classes |
550 |
|
if (atLeastJava9()) { |
551 |
|
String[] java9TestClassNames = { |
552 |
< |
// Currently empty, but expecting varhandle tests |
552 |
> |
"AtomicBoolean9Test", |
553 |
> |
"AtomicInteger9Test", |
554 |
> |
"AtomicIntegerArray9Test", |
555 |
> |
"AtomicLong9Test", |
556 |
> |
"AtomicLongArray9Test", |
557 |
> |
"AtomicReference9Test", |
558 |
> |
"AtomicReferenceArray9Test", |
559 |
> |
"ExecutorCompletionService9Test", |
560 |
|
}; |
561 |
|
addNamedTestClasses(suite, java9TestClassNames); |
562 |
|
} |
567 |
|
/** Returns list of junit-style test method names in given class. */ |
568 |
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
569 |
|
Method[] methods = testClass.getDeclaredMethods(); |
570 |
< |
ArrayList<String> names = new ArrayList<String>(methods.length); |
570 |
> |
ArrayList<String> names = new ArrayList<>(methods.length); |
571 |
|
for (Method method : methods) { |
572 |
|
if (method.getName().startsWith("test") |
573 |
|
&& Modifier.isPublic(method.getModifiers()) |
634 |
|
|
635 |
|
/** |
636 |
|
* Returns the shortest timed delay. This can be scaled up for |
637 |
< |
* slow machines using the jsr166.delay.factor system property. |
637 |
> |
* slow machines using the jsr166.delay.factor system property, |
638 |
> |
* or via jtreg's -timeoutFactor: flag. |
639 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
640 |
|
*/ |
641 |
|
protected long getShortDelay() { |
642 |
< |
return 50 * delayFactor; |
642 |
> |
return (long) (50 * delayFactor); |
643 |
|
} |
644 |
|
|
645 |
|
/** |
673 |
|
* The first exception encountered if any threadAssertXXX method fails. |
674 |
|
*/ |
675 |
|
private final AtomicReference<Throwable> threadFailure |
676 |
< |
= new AtomicReference<Throwable>(null); |
676 |
> |
= new AtomicReference<>(null); |
677 |
|
|
678 |
|
/** |
679 |
|
* Records an exception so that it can be rethrown later in the test |
952 |
|
}}; |
953 |
|
} |
954 |
|
|
955 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
956 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
957 |
+ |
} |
958 |
+ |
|
959 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
960 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
961 |
+ |
} |
962 |
+ |
|
963 |
|
/** |
964 |
|
* Waits out termination of a thread pool or fails doing so. |
965 |
|
*/ |
983 |
|
} |
984 |
|
} |
985 |
|
|
986 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
986 |
> |
/** |
987 |
> |
* Like Runnable, but with the freedom to throw anything. |
988 |
> |
* junit folks had the same idea: |
989 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
990 |
> |
*/ |
991 |
|
interface Action { public void run() throws Throwable; } |
992 |
|
|
993 |
|
/** |
1018 |
|
* Uninteresting threads are filtered out. |
1019 |
|
*/ |
1020 |
|
static void dumpTestThreads() { |
1021 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1022 |
+ |
if (sm != null) { |
1023 |
+ |
try { |
1024 |
+ |
System.setSecurityManager(null); |
1025 |
+ |
} catch (SecurityException giveUp) { |
1026 |
+ |
return; |
1027 |
+ |
} |
1028 |
+ |
} |
1029 |
+ |
|
1030 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1031 |
|
System.err.println("------ stacktrace dump start ------"); |
1032 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1033 |
< |
String name = info.getThreadName(); |
1033 |
> |
final String name = info.getThreadName(); |
1034 |
> |
String lockName; |
1035 |
|
if ("Signal Dispatcher".equals(name)) |
1036 |
|
continue; |
1037 |
|
if ("Reference Handler".equals(name) |
1038 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1038 |
> |
&& (lockName = info.getLockName()) != null |
1039 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1040 |
|
continue; |
1041 |
|
if ("Finalizer".equals(name) |
1042 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1042 |
> |
&& (lockName = info.getLockName()) != null |
1043 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1044 |
|
continue; |
1045 |
|
if ("checkForWedgedTest".equals(name)) |
1046 |
|
continue; |
1047 |
|
System.err.print(info); |
1048 |
|
} |
1049 |
|
System.err.println("------ stacktrace dump end ------"); |
1050 |
+ |
|
1051 |
+ |
if (sm != null) System.setSecurityManager(sm); |
1052 |
|
} |
1053 |
|
|
1054 |
|
/** |
1235 |
|
} |
1236 |
|
public void refresh() {} |
1237 |
|
public String toString() { |
1238 |
< |
List<Permission> ps = new ArrayList<Permission>(); |
1238 |
> |
List<Permission> ps = new ArrayList<>(); |
1239 |
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1240 |
|
ps.add(e.nextElement()); |
1241 |
|
return "AdjustablePolicy with permissions " + ps; |
1265 |
|
* Sleeps until the given time has elapsed. |
1266 |
|
* Throws AssertionFailedError if interrupted. |
1267 |
|
*/ |
1268 |
< |
void sleep(long millis) { |
1268 |
> |
static void sleep(long millis) { |
1269 |
|
try { |
1270 |
|
delay(millis); |
1271 |
|
} catch (InterruptedException fail) { |
1281 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1282 |
|
*/ |
1283 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1284 |
< |
long startTime = System.nanoTime(); |
1284 |
> |
long startTime = 0L; |
1285 |
|
for (;;) { |
1286 |
|
Thread.State s = thread.getState(); |
1287 |
|
if (s == Thread.State.BLOCKED || |
1290 |
|
return; |
1291 |
|
else if (s == Thread.State.TERMINATED) |
1292 |
|
fail("Unexpected thread termination"); |
1293 |
+ |
else if (startTime == 0L) |
1294 |
+ |
startTime = System.nanoTime(); |
1295 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1296 |
|
threadAssertTrue(thread.isAlive()); |
1297 |
|
return; |
1536 |
|
return new LatchAwaiter(latch); |
1537 |
|
} |
1538 |
|
|
1539 |
< |
public void await(CountDownLatch latch) { |
1539 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1540 |
|
try { |
1541 |
< |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1541 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1542 |
|
fail("timed out waiting for CountDownLatch for " |
1543 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1543 |
> |
+ (timeoutMillis/1000) + " sec"); |
1544 |
|
} catch (Throwable fail) { |
1545 |
|
threadUnexpectedException(fail); |
1546 |
|
} |
1547 |
|
} |
1548 |
|
|
1549 |
+ |
public void await(CountDownLatch latch) { |
1550 |
+ |
await(latch, LONG_DELAY_MS); |
1551 |
+ |
} |
1552 |
+ |
|
1553 |
|
public void await(Semaphore semaphore) { |
1554 |
|
try { |
1555 |
|
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1784 |
|
* A CyclicBarrier that uses timed await and fails with |
1785 |
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1786 |
|
*/ |
1787 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1787 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1788 |
|
public CheckedBarrier(int parties) { super(parties); } |
1789 |
|
|
1790 |
|
public int await() { |
1848 |
|
} |
1849 |
|
} |
1850 |
|
|
1851 |
+ |
void assertImmutable(final Object o) { |
1852 |
+ |
if (o instanceof Collection) { |
1853 |
+ |
assertThrows( |
1854 |
+ |
UnsupportedOperationException.class, |
1855 |
+ |
new Runnable() { public void run() { |
1856 |
+ |
((Collection) o).add(null);}}); |
1857 |
+ |
} |
1858 |
+ |
} |
1859 |
+ |
|
1860 |
|
@SuppressWarnings("unchecked") |
1861 |
|
<T> T serialClone(T o) { |
1862 |
|
try { |
1863 |
|
ObjectInputStream ois = new ObjectInputStream |
1864 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1865 |
|
T clone = (T) ois.readObject(); |
1866 |
+ |
if (o == clone) assertImmutable(o); |
1867 |
|
assertSame(o.getClass(), clone.getClass()); |
1868 |
|
return clone; |
1869 |
|
} catch (Throwable fail) { |
1872 |
|
} |
1873 |
|
} |
1874 |
|
|
1875 |
+ |
/** |
1876 |
+ |
* A version of serialClone that leaves error handling (for |
1877 |
+ |
* e.g. NotSerializableException) up to the caller. |
1878 |
+ |
*/ |
1879 |
+ |
@SuppressWarnings("unchecked") |
1880 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1881 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1882 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1883 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1884 |
+ |
oos.writeObject(o); |
1885 |
+ |
oos.flush(); |
1886 |
+ |
oos.close(); |
1887 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1888 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1889 |
+ |
T clone = (T) ois.readObject(); |
1890 |
+ |
if (o == clone) assertImmutable(o); |
1891 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1892 |
+ |
return clone; |
1893 |
+ |
} |
1894 |
+ |
|
1895 |
+ |
/** |
1896 |
+ |
* If o implements Cloneable and has a public clone method, |
1897 |
+ |
* returns a clone of o, else null. |
1898 |
+ |
*/ |
1899 |
+ |
@SuppressWarnings("unchecked") |
1900 |
+ |
<T> T cloneableClone(T o) { |
1901 |
+ |
if (!(o instanceof Cloneable)) return null; |
1902 |
+ |
final T clone; |
1903 |
+ |
try { |
1904 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1905 |
+ |
} catch (NoSuchMethodException ok) { |
1906 |
+ |
return null; |
1907 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1908 |
+ |
throw new Error(unexpected); |
1909 |
+ |
} |
1910 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1911 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1912 |
+ |
return clone; |
1913 |
+ |
} |
1914 |
+ |
|
1915 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1916 |
|
Runnable... throwingActions) { |
1917 |
|
for (Runnable throwingAction : throwingActions) { |
1940 |
|
} catch (NoSuchElementException success) {} |
1941 |
|
assertFalse(it.hasNext()); |
1942 |
|
} |
1943 |
+ |
|
1944 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1945 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1946 |
+ |
} |
1947 |
+ |
|
1948 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1949 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1950 |
+ |
} |
1951 |
+ |
|
1952 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1953 |
+ |
static final ExecutorService cachedThreadPool = |
1954 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1955 |
+ |
1000L, MILLISECONDS, |
1956 |
+ |
new SynchronousQueue<Runnable>()); |
1957 |
+ |
|
1958 |
+ |
static <T> void shuffle(T[] array) { |
1959 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1960 |
+ |
} |
1961 |
|
} |