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.*; |
35 |
< |
import java.util.concurrent.atomic.AtomicBoolean; |
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; |
27 |
– |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
– |
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; |
35 |
< |
import java.security.ProtectionDomain; |
36 |
< |
import java.security.SecurityPermission; |
49 |
> |
|
50 |
> |
import junit.framework.AssertionFailedError; |
51 |
> |
import junit.framework.Test; |
52 |
> |
import junit.framework.TestCase; |
53 |
> |
import junit.framework.TestSuite; |
54 |
|
|
55 |
|
/** |
56 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
133 |
|
Boolean.getBoolean("jsr166.expensiveTests"); |
134 |
|
|
135 |
|
/** |
136 |
+ |
* If true, also run tests that are not part of the official tck |
137 |
+ |
* because they test unspecified implementation details. |
138 |
+ |
*/ |
139 |
+ |
protected static final boolean testImplementationDetails = |
140 |
+ |
Boolean.getBoolean("jsr166.testImplementationDetails"); |
141 |
+ |
|
142 |
+ |
/** |
143 |
|
* If true, report on stdout all "slow" tests, that is, ones that |
144 |
|
* take more than profileThreshold milliseconds to execute. |
145 |
|
*/ |
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
< |
* Find missing try { ... } finally { joinPool(e); } |
469 |
> |
* Finds missing try { ... } finally { joinPool(e); } |
470 |
|
*/ |
471 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
472 |
|
Thread[] survivors = new Thread[5]; |
564 |
|
public void threadAssertEquals(Object x, Object y) { |
565 |
|
try { |
566 |
|
assertEquals(x, y); |
567 |
< |
} catch (AssertionFailedError t) { |
568 |
< |
threadRecordFailure(t); |
569 |
< |
throw t; |
570 |
< |
} catch (Throwable t) { |
571 |
< |
threadUnexpectedException(t); |
567 |
> |
} catch (AssertionFailedError fail) { |
568 |
> |
threadRecordFailure(fail); |
569 |
> |
throw fail; |
570 |
> |
} catch (Throwable fail) { |
571 |
> |
threadUnexpectedException(fail); |
572 |
|
} |
573 |
|
} |
574 |
|
|
580 |
|
public void threadAssertSame(Object x, Object y) { |
581 |
|
try { |
582 |
|
assertSame(x, y); |
583 |
< |
} catch (AssertionFailedError t) { |
584 |
< |
threadRecordFailure(t); |
585 |
< |
throw t; |
583 |
> |
} catch (AssertionFailedError fail) { |
584 |
> |
threadRecordFailure(fail); |
585 |
> |
throw fail; |
586 |
|
} |
587 |
|
} |
588 |
|
|
647 |
|
void joinPool(ExecutorService exec) { |
648 |
|
try { |
649 |
|
exec.shutdown(); |
650 |
< |
assertTrue("ExecutorService did not terminate in a timely manner", |
651 |
< |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
650 |
> |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
651 |
> |
fail("ExecutorService " + exec + |
652 |
> |
" did not terminate in a timely manner"); |
653 |
|
} catch (SecurityException ok) { |
654 |
|
// Allowed in case test doesn't have privs |
655 |
< |
} catch (InterruptedException ie) { |
655 |
> |
} catch (InterruptedException fail) { |
656 |
|
fail("Unexpected InterruptedException"); |
657 |
|
} |
658 |
|
} |
683 |
|
// No need to optimize the failing case via Thread.join. |
684 |
|
delay(millis); |
685 |
|
assertTrue(thread.isAlive()); |
686 |
< |
} catch (InterruptedException ie) { |
686 |
> |
} catch (InterruptedException fail) { |
687 |
|
fail("Unexpected InterruptedException"); |
688 |
|
} |
689 |
|
} |
705 |
|
delay(millis); |
706 |
|
for (Thread thread : threads) |
707 |
|
assertTrue(thread.isAlive()); |
708 |
< |
} catch (InterruptedException ie) { |
708 |
> |
} catch (InterruptedException fail) { |
709 |
|
fail("Unexpected InterruptedException"); |
710 |
|
} |
711 |
|
} |
727 |
|
future.get(timeoutMillis, MILLISECONDS); |
728 |
|
shouldThrow(); |
729 |
|
} catch (TimeoutException success) { |
730 |
< |
} catch (Exception e) { |
731 |
< |
threadUnexpectedException(e); |
730 |
> |
} catch (Exception fail) { |
731 |
> |
threadUnexpectedException(fail); |
732 |
|
} finally { future.cancel(true); } |
733 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
734 |
|
} |
884 |
|
void sleep(long millis) { |
885 |
|
try { |
886 |
|
delay(millis); |
887 |
< |
} catch (InterruptedException ie) { |
887 |
> |
} catch (InterruptedException fail) { |
888 |
|
AssertionFailedError afe = |
889 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
890 |
< |
afe.initCause(ie); |
890 |
> |
afe.initCause(fail); |
891 |
|
throw afe; |
892 |
|
} |
893 |
|
} |
925 |
|
/** |
926 |
|
* Returns the number of milliseconds since time given by |
927 |
|
* startNanoTime, which must have been previously returned from a |
928 |
< |
* call to {@link System.nanoTime()}. |
928 |
> |
* call to {@link System#nanoTime()}. |
929 |
|
*/ |
930 |
|
static long millisElapsedSince(long startNanoTime) { |
931 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
932 |
|
} |
933 |
|
|
934 |
+ |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
935 |
+ |
// long startTime = System.nanoTime(); |
936 |
+ |
// try { |
937 |
+ |
// r.run(); |
938 |
+ |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
939 |
+ |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
940 |
+ |
// throw new AssertionFailedError("did not return promptly"); |
941 |
+ |
// } |
942 |
+ |
|
943 |
+ |
// void assertTerminatesPromptly(Runnable r) { |
944 |
+ |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
945 |
+ |
// } |
946 |
+ |
|
947 |
+ |
/** |
948 |
+ |
* Checks that timed f.get() returns the expected value, and does not |
949 |
+ |
* wait for the timeout to elapse before returning. |
950 |
+ |
*/ |
951 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
952 |
+ |
long startTime = System.nanoTime(); |
953 |
+ |
try { |
954 |
+ |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
955 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
956 |
+ |
if (millisElapsedSince(startTime) > timeoutMillis/2) |
957 |
+ |
throw new AssertionFailedError("timed get did not return promptly"); |
958 |
+ |
} |
959 |
+ |
|
960 |
+ |
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
961 |
+ |
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
962 |
+ |
} |
963 |
+ |
|
964 |
|
/** |
965 |
|
* Returns a new started daemon Thread running the given runnable. |
966 |
|
*/ |
979 |
|
void awaitTermination(Thread t, long timeoutMillis) { |
980 |
|
try { |
981 |
|
t.join(timeoutMillis); |
982 |
< |
} catch (InterruptedException ie) { |
983 |
< |
threadUnexpectedException(ie); |
982 |
> |
} catch (InterruptedException fail) { |
983 |
> |
threadUnexpectedException(fail); |
984 |
|
} finally { |
985 |
|
if (t.getState() != Thread.State.TERMINATED) { |
986 |
|
t.interrupt(); |
1006 |
|
public final void run() { |
1007 |
|
try { |
1008 |
|
realRun(); |
1009 |
< |
} catch (Throwable t) { |
1010 |
< |
threadUnexpectedException(t); |
1009 |
> |
} catch (Throwable fail) { |
1010 |
> |
threadUnexpectedException(fail); |
1011 |
|
} |
1012 |
|
} |
1013 |
|
} |
1061 |
|
threadShouldThrow("InterruptedException"); |
1062 |
|
} catch (InterruptedException success) { |
1063 |
|
threadAssertFalse(Thread.interrupted()); |
1064 |
< |
} catch (Throwable t) { |
1065 |
< |
threadUnexpectedException(t); |
1064 |
> |
} catch (Throwable fail) { |
1065 |
> |
threadUnexpectedException(fail); |
1066 |
|
} |
1067 |
|
} |
1068 |
|
} |
1073 |
|
public final T call() { |
1074 |
|
try { |
1075 |
|
return realCall(); |
1076 |
< |
} catch (Throwable t) { |
1077 |
< |
threadUnexpectedException(t); |
1076 |
> |
} catch (Throwable fail) { |
1077 |
> |
threadUnexpectedException(fail); |
1078 |
|
return null; |
1079 |
|
} |
1080 |
|
} |
1091 |
|
return result; |
1092 |
|
} catch (InterruptedException success) { |
1093 |
|
threadAssertFalse(Thread.interrupted()); |
1094 |
< |
} catch (Throwable t) { |
1095 |
< |
threadUnexpectedException(t); |
1094 |
> |
} catch (Throwable fail) { |
1095 |
> |
threadUnexpectedException(fail); |
1096 |
|
} |
1097 |
|
return null; |
1098 |
|
} |
1132 |
|
public void await(CountDownLatch latch) { |
1133 |
|
try { |
1134 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1135 |
< |
} catch (Throwable t) { |
1136 |
< |
threadUnexpectedException(t); |
1135 |
> |
} catch (Throwable fail) { |
1136 |
> |
threadUnexpectedException(fail); |
1137 |
|
} |
1138 |
|
} |
1139 |
|
|
1140 |
|
public void await(Semaphore semaphore) { |
1141 |
|
try { |
1142 |
|
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1143 |
< |
} catch (Throwable t) { |
1144 |
< |
threadUnexpectedException(t); |
1143 |
> |
} catch (Throwable fail) { |
1144 |
> |
threadUnexpectedException(fail); |
1145 |
|
} |
1146 |
|
} |
1147 |
|
|
1335 |
|
@Override protected final void compute() { |
1336 |
|
try { |
1337 |
|
realCompute(); |
1338 |
< |
} catch (Throwable t) { |
1339 |
< |
threadUnexpectedException(t); |
1338 |
> |
} catch (Throwable fail) { |
1339 |
> |
threadUnexpectedException(fail); |
1340 |
|
} |
1341 |
|
} |
1342 |
|
} |
1350 |
|
@Override protected final T compute() { |
1351 |
|
try { |
1352 |
|
return realCompute(); |
1353 |
< |
} catch (Throwable t) { |
1354 |
< |
threadUnexpectedException(t); |
1353 |
> |
} catch (Throwable fail) { |
1354 |
> |
threadUnexpectedException(fail); |
1355 |
|
return null; |
1356 |
|
} |
1357 |
|
} |
1375 |
|
public int await() { |
1376 |
|
try { |
1377 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1378 |
< |
} catch (TimeoutException e) { |
1378 |
> |
} catch (TimeoutException timedOut) { |
1379 |
|
throw new AssertionFailedError("timed out"); |
1380 |
< |
} catch (Exception e) { |
1380 |
> |
} catch (Exception fail) { |
1381 |
|
AssertionFailedError afe = |
1382 |
< |
new AssertionFailedError("Unexpected exception: " + e); |
1383 |
< |
afe.initCause(e); |
1382 |
> |
new AssertionFailedError("Unexpected exception: " + fail); |
1383 |
> |
afe.initCause(fail); |
1384 |
|
throw afe; |
1385 |
|
} |
1386 |
|
} |
1408 |
|
q.remove(); |
1409 |
|
shouldThrow(); |
1410 |
|
} catch (NoSuchElementException success) {} |
1411 |
< |
} catch (InterruptedException ie) { |
1357 |
< |
threadUnexpectedException(ie); |
1358 |
< |
} |
1411 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1412 |
|
} |
1413 |
|
|
1414 |
|
void assertSerialEquals(Object x, Object y) { |
1427 |
|
oos.flush(); |
1428 |
|
oos.close(); |
1429 |
|
return bos.toByteArray(); |
1430 |
< |
} catch (Throwable t) { |
1431 |
< |
threadUnexpectedException(t); |
1430 |
> |
} catch (Throwable fail) { |
1431 |
> |
threadUnexpectedException(fail); |
1432 |
|
return new byte[0]; |
1433 |
|
} |
1434 |
|
} |
1441 |
|
T clone = (T) ois.readObject(); |
1442 |
|
assertSame(o.getClass(), clone.getClass()); |
1443 |
|
return clone; |
1444 |
< |
} catch (Throwable t) { |
1445 |
< |
threadUnexpectedException(t); |
1444 |
> |
} catch (Throwable fail) { |
1445 |
> |
threadUnexpectedException(fail); |
1446 |
|
return null; |
1447 |
|
} |
1448 |
|
} |
1467 |
|
shouldThrow(expectedExceptionClass.getName()); |
1468 |
|
} |
1469 |
|
} |
1470 |
+ |
|
1471 |
+ |
public void assertIteratorExhausted(Iterator<?> it) { |
1472 |
+ |
try { |
1473 |
+ |
it.next(); |
1474 |
+ |
shouldThrow(); |
1475 |
+ |
} catch (NoSuchElementException success) {} |
1476 |
+ |
assertFalse(it.hasNext()); |
1477 |
+ |
} |
1478 |
|
} |