9 |
|
/* |
10 |
|
* @test |
11 |
|
* @summary JSR-166 tck tests |
12 |
+ |
* @modules java.management |
13 |
|
* @build * |
14 |
< |
* @run junit/othervm/timeout=1000 JSR166TestCase |
14 |
> |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase |
15 |
> |
* @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 -Djsr166.testImplementationDetails=true JSR166TestCase |
16 |
> |
* @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 -Djava.util.secureRandomSeed=true JSR166TestCase |
17 |
|
*/ |
18 |
|
|
19 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
41 |
|
import java.security.SecurityPermission; |
42 |
|
import java.util.ArrayList; |
43 |
|
import java.util.Arrays; |
44 |
+ |
import java.util.Collection; |
45 |
+ |
import java.util.Collections; |
46 |
|
import java.util.Date; |
47 |
|
import java.util.Enumeration; |
48 |
|
import java.util.Iterator; |
62 |
|
import java.util.concurrent.RecursiveTask; |
63 |
|
import java.util.concurrent.RejectedExecutionHandler; |
64 |
|
import java.util.concurrent.Semaphore; |
65 |
+ |
import java.util.concurrent.SynchronousQueue; |
66 |
|
import java.util.concurrent.ThreadFactory; |
67 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
68 |
|
import java.util.concurrent.ThreadPoolExecutor; |
69 |
|
import java.util.concurrent.TimeoutException; |
70 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
71 |
|
import java.util.concurrent.atomic.AtomicReference; |
72 |
|
import java.util.regex.Matcher; |
73 |
|
import java.util.regex.Pattern; |
191 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
192 |
|
|
193 |
|
/** |
194 |
< |
* The scaling factor to apply to standard delays used in tests. |
194 |
> |
* Returns the value of the system property, or NaN if not defined. |
195 |
|
*/ |
196 |
< |
private static final int delayFactor = |
197 |
< |
Integer.getInteger("jsr166.delay.factor", 1); |
196 |
> |
private static float systemPropertyValue(String name) { |
197 |
> |
String floatString = System.getProperty(name); |
198 |
> |
if (floatString == null) |
199 |
> |
return Float.NaN; |
200 |
> |
try { |
201 |
> |
return Float.parseFloat(floatString); |
202 |
> |
} catch (NumberFormatException ex) { |
203 |
> |
throw new IllegalArgumentException( |
204 |
> |
String.format("Bad float value in system property %s=%s", |
205 |
> |
name, floatString)); |
206 |
> |
} |
207 |
> |
} |
208 |
> |
|
209 |
> |
/** |
210 |
> |
* The scaling factor to apply to standard delays used in tests. |
211 |
> |
* May be initialized from any of: |
212 |
> |
* - the "jsr166.delay.factor" system property |
213 |
> |
* - the "test.timeout.factor" system property (as used by jtreg) |
214 |
> |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
215 |
> |
* - hard-coded fuzz factor when using a known slowpoke VM |
216 |
> |
*/ |
217 |
> |
private static final float delayFactor = delayFactor(); |
218 |
> |
|
219 |
> |
private static float delayFactor() { |
220 |
> |
float x; |
221 |
> |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
222 |
> |
return x; |
223 |
> |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
224 |
> |
return x; |
225 |
> |
String prop = System.getProperty("java.vm.version"); |
226 |
> |
if (prop != null && prop.matches(".*debug.*")) |
227 |
> |
return 4.0f; // How much slower is fastdebug than product?! |
228 |
> |
return 1.0f; |
229 |
> |
} |
230 |
|
|
231 |
|
public JSR166TestCase() { super(); } |
232 |
|
public JSR166TestCase(String name) { super(name); } |
448 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
449 |
|
ArrayBlockingQueueTest.suite(), |
450 |
|
ArrayDequeTest.suite(), |
451 |
+ |
ArrayListTest.suite(), |
452 |
|
AtomicBooleanTest.suite(), |
453 |
|
AtomicIntegerArrayTest.suite(), |
454 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
471 |
|
CopyOnWriteArrayListTest.suite(), |
472 |
|
CopyOnWriteArraySetTest.suite(), |
473 |
|
CountDownLatchTest.suite(), |
474 |
+ |
CountedCompleterTest.suite(), |
475 |
|
CyclicBarrierTest.suite(), |
476 |
|
DelayQueueTest.suite(), |
477 |
|
EntryTest.suite(), |
500 |
|
TreeMapTest.suite(), |
501 |
|
TreeSetTest.suite(), |
502 |
|
TreeSubMapTest.suite(), |
503 |
< |
TreeSubSetTest.suite()); |
503 |
> |
TreeSubSetTest.suite(), |
504 |
> |
VectorTest.suite()); |
505 |
|
|
506 |
|
// Java8+ test classes |
507 |
|
if (atLeastJava8()) { |
508 |
|
String[] java8TestClassNames = { |
509 |
+ |
"ArrayDeque8Test", |
510 |
|
"Atomic8Test", |
511 |
|
"CompletableFutureTest", |
512 |
|
"ConcurrentHashMap8Test", |
513 |
< |
"CountedCompleterTest", |
513 |
> |
"CountedCompleter8Test", |
514 |
|
"DoubleAccumulatorTest", |
515 |
|
"DoubleAdderTest", |
516 |
|
"ForkJoinPool8Test", |
521 |
|
"StampedLockTest", |
522 |
|
"SubmissionPublisherTest", |
523 |
|
"ThreadLocalRandom8Test", |
524 |
+ |
"TimeUnit8Test", |
525 |
|
}; |
526 |
|
addNamedTestClasses(suite, java8TestClassNames); |
527 |
|
} |
529 |
|
// Java9+ test classes |
530 |
|
if (atLeastJava9()) { |
531 |
|
String[] java9TestClassNames = { |
532 |
< |
// Currently empty, but expecting varhandle tests |
532 |
> |
"AtomicBoolean9Test", |
533 |
> |
"AtomicInteger9Test", |
534 |
> |
"AtomicIntegerArray9Test", |
535 |
> |
"AtomicLong9Test", |
536 |
> |
"AtomicLongArray9Test", |
537 |
> |
"AtomicReference9Test", |
538 |
> |
"AtomicReferenceArray9Test", |
539 |
> |
"ExecutorCompletionService9Test", |
540 |
|
}; |
541 |
|
addNamedTestClasses(suite, java9TestClassNames); |
542 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
|
* Returns the shortest timed delay. This can be scaled up for |
617 |
< |
* slow machines using the jsr166.delay.factor system property. |
617 |
> |
* slow machines using the jsr166.delay.factor system property, |
618 |
> |
* or via jtreg's -timeoutFactor: flag. |
619 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
620 |
|
*/ |
621 |
|
protected long getShortDelay() { |
622 |
< |
return 50 * delayFactor; |
622 |
> |
return (long) (50 * delayFactor); |
623 |
|
} |
624 |
|
|
625 |
|
/** |
932 |
|
}}; |
933 |
|
} |
934 |
|
|
935 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
936 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
940 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
941 |
+ |
} |
942 |
+ |
|
943 |
|
/** |
944 |
|
* Waits out termination of a thread pool or fails doing so. |
945 |
|
*/ |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
966 |
> |
/** |
967 |
> |
* Like Runnable, but with the freedom to throw anything. |
968 |
> |
* junit folks had the same idea: |
969 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
970 |
> |
*/ |
971 |
|
interface Action { public void run() throws Throwable; } |
972 |
|
|
973 |
|
/** |
998 |
|
* Uninteresting threads are filtered out. |
999 |
|
*/ |
1000 |
|
static void dumpTestThreads() { |
1001 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1002 |
+ |
if (sm != null) { |
1003 |
+ |
try { |
1004 |
+ |
System.setSecurityManager(null); |
1005 |
+ |
} catch (SecurityException giveUp) { |
1006 |
+ |
return; |
1007 |
+ |
} |
1008 |
+ |
} |
1009 |
+ |
|
1010 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1011 |
|
System.err.println("------ stacktrace dump start ------"); |
1012 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1013 |
< |
String name = info.getThreadName(); |
1013 |
> |
final String name = info.getThreadName(); |
1014 |
> |
String lockName; |
1015 |
|
if ("Signal Dispatcher".equals(name)) |
1016 |
|
continue; |
1017 |
|
if ("Reference Handler".equals(name) |
1018 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1018 |
> |
&& (lockName = info.getLockName()) != null |
1019 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1020 |
|
continue; |
1021 |
|
if ("Finalizer".equals(name) |
1022 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1022 |
> |
&& (lockName = info.getLockName()) != null |
1023 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1024 |
|
continue; |
1025 |
|
if ("checkForWedgedTest".equals(name)) |
1026 |
|
continue; |
1027 |
|
System.err.print(info); |
1028 |
|
} |
1029 |
|
System.err.println("------ stacktrace dump end ------"); |
1030 |
+ |
|
1031 |
+ |
if (sm != null) System.setSecurityManager(sm); |
1032 |
|
} |
1033 |
|
|
1034 |
|
/** |
1245 |
|
* Sleeps until the given time has elapsed. |
1246 |
|
* Throws AssertionFailedError if interrupted. |
1247 |
|
*/ |
1248 |
< |
void sleep(long millis) { |
1248 |
> |
static void sleep(long millis) { |
1249 |
|
try { |
1250 |
|
delay(millis); |
1251 |
|
} catch (InterruptedException fail) { |
1261 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1262 |
|
*/ |
1263 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1264 |
< |
long startTime = System.nanoTime(); |
1264 |
> |
long startTime = 0L; |
1265 |
|
for (;;) { |
1266 |
|
Thread.State s = thread.getState(); |
1267 |
|
if (s == Thread.State.BLOCKED || |
1270 |
|
return; |
1271 |
|
else if (s == Thread.State.TERMINATED) |
1272 |
|
fail("Unexpected thread termination"); |
1273 |
+ |
else if (startTime == 0L) |
1274 |
+ |
startTime = System.nanoTime(); |
1275 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1276 |
|
threadAssertTrue(thread.isAlive()); |
1277 |
|
return; |
1516 |
|
return new LatchAwaiter(latch); |
1517 |
|
} |
1518 |
|
|
1519 |
< |
public void await(CountDownLatch latch) { |
1519 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1520 |
|
try { |
1521 |
< |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1521 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1522 |
|
fail("timed out waiting for CountDownLatch for " |
1523 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1523 |
> |
+ (timeoutMillis/1000) + " sec"); |
1524 |
|
} catch (Throwable fail) { |
1525 |
|
threadUnexpectedException(fail); |
1526 |
|
} |
1527 |
|
} |
1528 |
|
|
1529 |
+ |
public void await(CountDownLatch latch) { |
1530 |
+ |
await(latch, LONG_DELAY_MS); |
1531 |
+ |
} |
1532 |
+ |
|
1533 |
|
public void await(Semaphore semaphore) { |
1534 |
|
try { |
1535 |
|
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1764 |
|
* A CyclicBarrier that uses timed await and fails with |
1765 |
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1766 |
|
*/ |
1767 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1767 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1768 |
|
public CheckedBarrier(int parties) { super(parties); } |
1769 |
|
|
1770 |
|
public int await() { |
1828 |
|
} |
1829 |
|
} |
1830 |
|
|
1831 |
+ |
void assertImmutable(final Object o) { |
1832 |
+ |
if (o instanceof Collection) { |
1833 |
+ |
assertThrows( |
1834 |
+ |
UnsupportedOperationException.class, |
1835 |
+ |
new Runnable() { public void run() { |
1836 |
+ |
((Collection) o).add(null);}}); |
1837 |
+ |
} |
1838 |
+ |
} |
1839 |
+ |
|
1840 |
|
@SuppressWarnings("unchecked") |
1841 |
|
<T> T serialClone(T o) { |
1842 |
|
try { |
1843 |
|
ObjectInputStream ois = new ObjectInputStream |
1844 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1845 |
|
T clone = (T) ois.readObject(); |
1846 |
+ |
if (o == clone) assertImmutable(o); |
1847 |
|
assertSame(o.getClass(), clone.getClass()); |
1848 |
|
return clone; |
1849 |
|
} catch (Throwable fail) { |
1852 |
|
} |
1853 |
|
} |
1854 |
|
|
1855 |
+ |
/** |
1856 |
+ |
* A version of serialClone that leaves error handling (for |
1857 |
+ |
* e.g. NotSerializableException) up to the caller. |
1858 |
+ |
*/ |
1859 |
+ |
@SuppressWarnings("unchecked") |
1860 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1861 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1862 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1863 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1864 |
+ |
oos.writeObject(o); |
1865 |
+ |
oos.flush(); |
1866 |
+ |
oos.close(); |
1867 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1868 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1869 |
+ |
T clone = (T) ois.readObject(); |
1870 |
+ |
if (o == clone) assertImmutable(o); |
1871 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1872 |
+ |
return clone; |
1873 |
+ |
} |
1874 |
+ |
|
1875 |
+ |
/** |
1876 |
+ |
* If o implements Cloneable and has a public clone method, |
1877 |
+ |
* returns a clone of o, else null. |
1878 |
+ |
*/ |
1879 |
+ |
@SuppressWarnings("unchecked") |
1880 |
+ |
<T> T cloneableClone(T o) { |
1881 |
+ |
if (!(o instanceof Cloneable)) return null; |
1882 |
+ |
final T clone; |
1883 |
+ |
try { |
1884 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1885 |
+ |
} catch (NoSuchMethodException ok) { |
1886 |
+ |
return null; |
1887 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1888 |
+ |
throw new Error(unexpected); |
1889 |
+ |
} |
1890 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1891 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1892 |
+ |
return clone; |
1893 |
+ |
} |
1894 |
+ |
|
1895 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1896 |
|
Runnable... throwingActions) { |
1897 |
|
for (Runnable throwingAction : throwingActions) { |
1920 |
|
} catch (NoSuchElementException success) {} |
1921 |
|
assertFalse(it.hasNext()); |
1922 |
|
} |
1923 |
+ |
|
1924 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1925 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1926 |
+ |
} |
1927 |
+ |
|
1928 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1929 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1930 |
+ |
} |
1931 |
+ |
|
1932 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1933 |
+ |
static final ExecutorService cachedThreadPool = |
1934 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1935 |
+ |
1000L, MILLISECONDS, |
1936 |
+ |
new SynchronousQueue<Runnable>()); |
1937 |
+ |
|
1938 |
+ |
static <T> void shuffle(T[] array) { |
1939 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1940 |
+ |
} |
1941 |
|
} |