11 |
|
import java.io.ByteArrayOutputStream; |
12 |
|
import java.io.ObjectInputStream; |
13 |
|
import java.io.ObjectOutputStream; |
14 |
+ |
import java.lang.management.ManagementFactory; |
15 |
+ |
import java.lang.management.ThreadInfo; |
16 |
+ |
import java.lang.reflect.Method; |
17 |
|
import java.util.ArrayList; |
18 |
|
import java.util.Arrays; |
19 |
|
import java.util.Date; |
182 |
|
return suite; |
183 |
|
} |
184 |
|
|
185 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
186 |
+ |
String... testClassNames) { |
187 |
+ |
for (String testClassName : testClassNames) { |
188 |
+ |
try { |
189 |
+ |
Class<?> testClass = Class.forName(testClassName); |
190 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
191 |
+ |
new Class<?>[0]); |
192 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
+ |
} catch (Exception e) { |
194 |
+ |
throw new Error("Missing test class", e); |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
public static final double JAVA_CLASS_VERSION; |
200 |
+ |
static { |
201 |
+ |
try { |
202 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
+ |
new java.security.PrivilegedAction<Double>() { |
204 |
+ |
public Double run() { |
205 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
+ |
} catch (Throwable t) { |
207 |
+ |
throw new Error(t); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
+ |
|
215 |
|
/** |
216 |
|
* Collects all JSR166 unit tests as one suite. |
217 |
|
*/ |
218 |
|
public static Test suite() { |
219 |
< |
return newTestSuite( |
219 |
> |
// Java7+ test classes |
220 |
> |
TestSuite suite = newTestSuite( |
221 |
|
ForkJoinPoolTest.suite(), |
222 |
|
ForkJoinTaskTest.suite(), |
223 |
|
RecursiveActionTest.suite(), |
282 |
|
TreeSetTest.suite(), |
283 |
|
TreeSubMapTest.suite(), |
284 |
|
TreeSubSetTest.suite()); |
285 |
+ |
|
286 |
+ |
// Java8+ test classes |
287 |
+ |
if (atLeastJava8()) { |
288 |
+ |
String[] java8TestClassNames = { |
289 |
+ |
"LongAdderTest", |
290 |
+ |
"LongAccumulatorTest", |
291 |
+ |
"DoubleAdderTest", |
292 |
+ |
"DoubleAccumulatorTest", |
293 |
+ |
"CompletableFutureTest", |
294 |
+ |
"ForkJoinPool8Test", |
295 |
+ |
"StampedLockTest", |
296 |
+ |
}; |
297 |
+ |
addNamedTestClasses(suite, java8TestClassNames); |
298 |
+ |
} |
299 |
+ |
|
300 |
+ |
return suite; |
301 |
|
} |
302 |
|
|
303 |
|
|
389 |
|
|
390 |
|
if (Thread.interrupted()) |
391 |
|
throw new AssertionFailedError("interrupt status set in main thread"); |
392 |
+ |
|
393 |
+ |
checkForkJoinPoolThreadLeaks(); |
394 |
+ |
} |
395 |
+ |
|
396 |
+ |
/** |
397 |
+ |
* Find missing try { ... } finally { joinPool(e); } |
398 |
+ |
*/ |
399 |
+ |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
400 |
+ |
Thread[] survivors = new Thread[5]; |
401 |
+ |
int count = Thread.enumerate(survivors); |
402 |
+ |
for (int i = 0; i < count; i++) { |
403 |
+ |
Thread thread = survivors[i]; |
404 |
+ |
String name = thread.getName(); |
405 |
+ |
if (name.startsWith("ForkJoinPool-")) { |
406 |
+ |
// give thread some time to terminate |
407 |
+ |
thread.join(LONG_DELAY_MS); |
408 |
+ |
if (!thread.isAlive()) continue; |
409 |
+ |
thread.stop(); |
410 |
+ |
throw new AssertionFailedError |
411 |
+ |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
412 |
+ |
toString(), name)); |
413 |
+ |
} |
414 |
+ |
} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
589 |
+ |
*/ |
590 |
+ |
static void printAllStackTraces() { |
591 |
+ |
for (ThreadInfo info : |
592 |
+ |
ManagementFactory.getThreadMXBean() |
593 |
+ |
.dumpAllThreads(true, true)) |
594 |
+ |
System.err.print(info); |
595 |
+ |
} |
596 |
+ |
|
597 |
+ |
/** |
598 |
|
* Checks that thread does not terminate within the default |
599 |
|
* millisecond delay of {@code timeoutMillis()}. |
600 |
|
*/ |