6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
|
12 |
|
import java.io.ByteArrayInputStream; |
13 |
|
import java.io.ByteArrayOutputStream; |
14 |
|
import java.io.ObjectInputStream; |
16 |
|
import java.lang.management.ManagementFactory; |
17 |
|
import java.lang.management.ThreadInfo; |
18 |
|
import java.lang.reflect.Method; |
19 |
+ |
import java.security.CodeSource; |
20 |
+ |
import java.security.Permission; |
21 |
+ |
import java.security.PermissionCollection; |
22 |
+ |
import java.security.Permissions; |
23 |
+ |
import java.security.Policy; |
24 |
+ |
import java.security.ProtectionDomain; |
25 |
+ |
import java.security.SecurityPermission; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.Arrays; |
28 |
|
import java.util.Date; |
29 |
|
import java.util.Enumeration; |
30 |
+ |
import java.util.Iterator; |
31 |
|
import java.util.List; |
32 |
|
import java.util.NoSuchElementException; |
33 |
|
import java.util.PropertyPermission; |
34 |
< |
import java.util.concurrent.*; |
34 |
> |
import java.util.concurrent.BlockingQueue; |
35 |
> |
import java.util.concurrent.Callable; |
36 |
> |
import java.util.concurrent.CountDownLatch; |
37 |
> |
import java.util.concurrent.CyclicBarrier; |
38 |
> |
import java.util.concurrent.ExecutorService; |
39 |
> |
import java.util.concurrent.Future; |
40 |
> |
import java.util.concurrent.RecursiveAction; |
41 |
> |
import java.util.concurrent.RecursiveTask; |
42 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
43 |
> |
import java.util.concurrent.Semaphore; |
44 |
> |
import java.util.concurrent.ThreadFactory; |
45 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
46 |
> |
import java.util.concurrent.TimeoutException; |
47 |
|
import java.util.concurrent.atomic.AtomicReference; |
26 |
– |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
27 |
– |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
48 |
|
import java.util.regex.Pattern; |
49 |
< |
import java.security.CodeSource; |
50 |
< |
import java.security.Permission; |
51 |
< |
import java.security.PermissionCollection; |
52 |
< |
import java.security.Permissions; |
53 |
< |
import java.security.Policy; |
54 |
< |
import java.security.ProtectionDomain; |
35 |
< |
import java.security.SecurityPermission; |
49 |
> |
|
50 |
> |
import junit.framework.AssertionFailedError; |
51 |
> |
import junit.framework.Test; |
52 |
> |
import junit.framework.TestCase; |
53 |
> |
import junit.framework.TestResult; |
54 |
> |
import junit.framework.TestSuite; |
55 |
|
|
56 |
|
/** |
57 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
161 |
|
Integer.getInteger("jsr166.runsPerTest", 1); |
162 |
|
|
163 |
|
/** |
164 |
+ |
* The number of repetitions of the test suite (for finding leaks?). |
165 |
+ |
*/ |
166 |
+ |
private static final int suiteRuns = |
167 |
+ |
Integer.getInteger("jsr166.suiteRuns", 1); |
168 |
+ |
|
169 |
+ |
/** |
170 |
|
* A filter for tests to run, matching strings of the form |
171 |
|
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
172 |
|
* Usefully combined with jsr166.runsPerTest. |
205 |
|
|
206 |
|
/** |
207 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
183 |
– |
* Optional command line arg provides the number of iterations to |
184 |
– |
* repeat running the tests. |
208 |
|
*/ |
209 |
|
public static void main(String[] args) { |
210 |
+ |
main(suite(), args); |
211 |
+ |
} |
212 |
+ |
|
213 |
+ |
/** |
214 |
+ |
* Runs all unit tests in the given test suite. |
215 |
+ |
* Actual behavior influenced by jsr166.* system properties. |
216 |
+ |
*/ |
217 |
+ |
static void main(Test suite, String[] args) { |
218 |
|
if (useSecurityManager) { |
219 |
|
System.err.println("Setting a permissive security manager"); |
220 |
|
Policy.setPolicy(permissivePolicy()); |
221 |
|
System.setSecurityManager(new SecurityManager()); |
222 |
|
} |
223 |
< |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
224 |
< |
|
225 |
< |
Test s = suite(); |
226 |
< |
for (int i = 0; i < iters; ++i) { |
196 |
< |
junit.textui.TestRunner.run(s); |
223 |
> |
for (int i = 0; i < suiteRuns; i++) { |
224 |
> |
TestResult result = junit.textui.TestRunner.run(suite); |
225 |
> |
if (!result.wasSuccessful()) |
226 |
> |
System.exit(1); |
227 |
|
System.gc(); |
228 |
|
System.runFinalization(); |
229 |
|
} |
200 |
– |
System.exit(0); |
230 |
|
} |
231 |
|
|
232 |
|
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
276 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
277 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
278 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
279 |
< |
public static boolean atLeastJava9() { |
251 |
< |
// As of 2014-05, java9 still uses 52.0 class file version |
252 |
< |
return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); |
253 |
< |
} |
279 |
> |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
280 |
|
|
281 |
|
/** |
282 |
|
* Collects all JSR166 unit tests as one suite. |
474 |
|
} |
475 |
|
|
476 |
|
/** |
477 |
< |
* Find missing try { ... } finally { joinPool(e); } |
477 |
> |
* Finds missing try { ... } finally { joinPool(e); } |
478 |
|
*/ |
479 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
480 |
|
Thread[] survivors = new Thread[5]; |
486 |
|
// give thread some time to terminate |
487 |
|
thread.join(LONG_DELAY_MS); |
488 |
|
if (!thread.isAlive()) continue; |
463 |
– |
thread.stop(); |
489 |
|
throw new AssertionFailedError |
490 |
|
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
491 |
|
toString(), name)); |
571 |
|
public void threadAssertEquals(Object x, Object y) { |
572 |
|
try { |
573 |
|
assertEquals(x, y); |
574 |
< |
} catch (AssertionFailedError t) { |
575 |
< |
threadRecordFailure(t); |
576 |
< |
throw t; |
577 |
< |
} catch (Throwable t) { |
578 |
< |
threadUnexpectedException(t); |
574 |
> |
} catch (AssertionFailedError fail) { |
575 |
> |
threadRecordFailure(fail); |
576 |
> |
throw fail; |
577 |
> |
} catch (Throwable fail) { |
578 |
> |
threadUnexpectedException(fail); |
579 |
|
} |
580 |
|
} |
581 |
|
|
587 |
|
public void threadAssertSame(Object x, Object y) { |
588 |
|
try { |
589 |
|
assertSame(x, y); |
590 |
< |
} catch (AssertionFailedError t) { |
591 |
< |
threadRecordFailure(t); |
592 |
< |
throw t; |
590 |
> |
} catch (AssertionFailedError fail) { |
591 |
> |
threadRecordFailure(fail); |
592 |
> |
throw fail; |
593 |
|
} |
594 |
|
} |
595 |
|
|
659 |
|
" did not terminate in a timely manner"); |
660 |
|
} catch (SecurityException ok) { |
661 |
|
// Allowed in case test doesn't have privs |
662 |
< |
} catch (InterruptedException ie) { |
662 |
> |
} catch (InterruptedException fail) { |
663 |
|
fail("Unexpected InterruptedException"); |
664 |
|
} |
665 |
|
} |
690 |
|
// No need to optimize the failing case via Thread.join. |
691 |
|
delay(millis); |
692 |
|
assertTrue(thread.isAlive()); |
693 |
< |
} catch (InterruptedException ie) { |
693 |
> |
} catch (InterruptedException fail) { |
694 |
|
fail("Unexpected InterruptedException"); |
695 |
|
} |
696 |
|
} |
712 |
|
delay(millis); |
713 |
|
for (Thread thread : threads) |
714 |
|
assertTrue(thread.isAlive()); |
715 |
< |
} catch (InterruptedException ie) { |
715 |
> |
} catch (InterruptedException fail) { |
716 |
|
fail("Unexpected InterruptedException"); |
717 |
|
} |
718 |
|
} |
734 |
|
future.get(timeoutMillis, MILLISECONDS); |
735 |
|
shouldThrow(); |
736 |
|
} catch (TimeoutException success) { |
737 |
< |
} catch (Exception e) { |
738 |
< |
threadUnexpectedException(e); |
737 |
> |
} catch (Exception fail) { |
738 |
> |
threadUnexpectedException(fail); |
739 |
|
} finally { future.cancel(true); } |
740 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
741 |
|
} |
891 |
|
void sleep(long millis) { |
892 |
|
try { |
893 |
|
delay(millis); |
894 |
< |
} catch (InterruptedException ie) { |
894 |
> |
} catch (InterruptedException fail) { |
895 |
|
AssertionFailedError afe = |
896 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
897 |
< |
afe.initCause(ie); |
897 |
> |
afe.initCause(fail); |
898 |
|
throw afe; |
899 |
|
} |
900 |
|
} |
932 |
|
/** |
933 |
|
* Returns the number of milliseconds since time given by |
934 |
|
* startNanoTime, which must have been previously returned from a |
935 |
< |
* call to {@link System.nanoTime()}. |
935 |
> |
* call to {@link System#nanoTime()}. |
936 |
|
*/ |
937 |
|
static long millisElapsedSince(long startNanoTime) { |
938 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
986 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
987 |
|
try { |
988 |
|
t.join(timeoutMillis); |
989 |
< |
} catch (InterruptedException ie) { |
990 |
< |
threadUnexpectedException(ie); |
989 |
> |
} catch (InterruptedException fail) { |
990 |
> |
threadUnexpectedException(fail); |
991 |
|
} finally { |
992 |
|
if (t.getState() != Thread.State.TERMINATED) { |
993 |
|
t.interrupt(); |
1013 |
|
public final void run() { |
1014 |
|
try { |
1015 |
|
realRun(); |
1016 |
< |
} catch (Throwable t) { |
1017 |
< |
threadUnexpectedException(t); |
1016 |
> |
} catch (Throwable fail) { |
1017 |
> |
threadUnexpectedException(fail); |
1018 |
|
} |
1019 |
|
} |
1020 |
|
} |
1068 |
|
threadShouldThrow("InterruptedException"); |
1069 |
|
} catch (InterruptedException success) { |
1070 |
|
threadAssertFalse(Thread.interrupted()); |
1071 |
< |
} catch (Throwable t) { |
1072 |
< |
threadUnexpectedException(t); |
1071 |
> |
} catch (Throwable fail) { |
1072 |
> |
threadUnexpectedException(fail); |
1073 |
|
} |
1074 |
|
} |
1075 |
|
} |
1080 |
|
public final T call() { |
1081 |
|
try { |
1082 |
|
return realCall(); |
1083 |
< |
} catch (Throwable t) { |
1084 |
< |
threadUnexpectedException(t); |
1083 |
> |
} catch (Throwable fail) { |
1084 |
> |
threadUnexpectedException(fail); |
1085 |
|
return null; |
1086 |
|
} |
1087 |
|
} |
1098 |
|
return result; |
1099 |
|
} catch (InterruptedException success) { |
1100 |
|
threadAssertFalse(Thread.interrupted()); |
1101 |
< |
} catch (Throwable t) { |
1102 |
< |
threadUnexpectedException(t); |
1101 |
> |
} catch (Throwable fail) { |
1102 |
> |
threadUnexpectedException(fail); |
1103 |
|
} |
1104 |
|
return null; |
1105 |
|
} |
1139 |
|
public void await(CountDownLatch latch) { |
1140 |
|
try { |
1141 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1142 |
< |
} catch (Throwable t) { |
1143 |
< |
threadUnexpectedException(t); |
1142 |
> |
} catch (Throwable fail) { |
1143 |
> |
threadUnexpectedException(fail); |
1144 |
|
} |
1145 |
|
} |
1146 |
|
|
1147 |
|
public void await(Semaphore semaphore) { |
1148 |
|
try { |
1149 |
|
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1150 |
< |
} catch (Throwable t) { |
1151 |
< |
threadUnexpectedException(t); |
1150 |
> |
} catch (Throwable fail) { |
1151 |
> |
threadUnexpectedException(fail); |
1152 |
|
} |
1153 |
|
} |
1154 |
|
|
1342 |
|
@Override protected final void compute() { |
1343 |
|
try { |
1344 |
|
realCompute(); |
1345 |
< |
} catch (Throwable t) { |
1346 |
< |
threadUnexpectedException(t); |
1345 |
> |
} catch (Throwable fail) { |
1346 |
> |
threadUnexpectedException(fail); |
1347 |
|
} |
1348 |
|
} |
1349 |
|
} |
1357 |
|
@Override protected final T compute() { |
1358 |
|
try { |
1359 |
|
return realCompute(); |
1360 |
< |
} catch (Throwable t) { |
1361 |
< |
threadUnexpectedException(t); |
1360 |
> |
} catch (Throwable fail) { |
1361 |
> |
threadUnexpectedException(fail); |
1362 |
|
return null; |
1363 |
|
} |
1364 |
|
} |
1382 |
|
public int await() { |
1383 |
|
try { |
1384 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1385 |
< |
} catch (TimeoutException e) { |
1385 |
> |
} catch (TimeoutException timedOut) { |
1386 |
|
throw new AssertionFailedError("timed out"); |
1387 |
< |
} catch (Exception e) { |
1387 |
> |
} catch (Exception fail) { |
1388 |
|
AssertionFailedError afe = |
1389 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1390 |
< |
afe.initCause(e); |
1389 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1390 |
> |
afe.initCause(fail); |
1391 |
|
throw afe; |
1392 |
|
} |
1393 |
|
} |
1415 |
|
q.remove(); |
1416 |
|
shouldThrow(); |
1417 |
|
} catch (NoSuchElementException success) {} |
1418 |
< |
} catch (InterruptedException ie) { |
1394 |
< |
threadUnexpectedException(ie); |
1395 |
< |
} |
1418 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1419 |
|
} |
1420 |
|
|
1421 |
|
void assertSerialEquals(Object x, Object y) { |
1434 |
|
oos.flush(); |
1435 |
|
oos.close(); |
1436 |
|
return bos.toByteArray(); |
1437 |
< |
} catch (Throwable t) { |
1438 |
< |
threadUnexpectedException(t); |
1437 |
> |
} catch (Throwable fail) { |
1438 |
> |
threadUnexpectedException(fail); |
1439 |
|
return new byte[0]; |
1440 |
|
} |
1441 |
|
} |
1448 |
|
T clone = (T) ois.readObject(); |
1449 |
|
assertSame(o.getClass(), clone.getClass()); |
1450 |
|
return clone; |
1451 |
< |
} catch (Throwable t) { |
1452 |
< |
threadUnexpectedException(t); |
1451 |
> |
} catch (Throwable fail) { |
1452 |
> |
threadUnexpectedException(fail); |
1453 |
|
return null; |
1454 |
|
} |
1455 |
|
} |
1474 |
|
shouldThrow(expectedExceptionClass.getName()); |
1475 |
|
} |
1476 |
|
} |
1477 |
+ |
|
1478 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1479 |
+ |
try { |
1480 |
+ |
it.next(); |
1481 |
+ |
shouldThrow(); |
1482 |
+ |
} catch (NoSuchElementException success) {} |
1483 |
+ |
assertFalse(it.hasNext()); |
1484 |
+ |
} |
1485 |
|
} |