1 |
dl |
1.1 |
/* |
2 |
dl |
1.13 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.74 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.27 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.182 |
/* |
10 |
|
|
* @test |
11 |
|
|
* @summary JSR-166 tck tests |
12 |
jsr166 |
1.184 |
* @modules java.management |
13 |
jsr166 |
1.182 |
* @build * |
14 |
jsr166 |
1.183 |
* @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase |
15 |
jsr166 |
1.194 |
* @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 -Djsr166.testImplementationDetails=true JSR166TestCase |
16 |
jsr166 |
1.198 |
* @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 -Djava.util.secureRandomSeed=true JSR166TestCase |
17 |
jsr166 |
1.182 |
*/ |
18 |
|
|
|
19 |
jsr166 |
1.123 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
jsr166 |
1.156 |
import static java.util.concurrent.TimeUnit.MINUTES; |
21 |
jsr166 |
1.123 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
22 |
|
|
|
23 |
jsr166 |
1.79 |
import java.io.ByteArrayInputStream; |
24 |
|
|
import java.io.ByteArrayOutputStream; |
25 |
|
|
import java.io.ObjectInputStream; |
26 |
|
|
import java.io.ObjectOutputStream; |
27 |
jsr166 |
1.96 |
import java.lang.management.ManagementFactory; |
28 |
|
|
import java.lang.management.ThreadInfo; |
29 |
jsr166 |
1.150 |
import java.lang.management.ThreadMXBean; |
30 |
jsr166 |
1.134 |
import java.lang.reflect.Constructor; |
31 |
jsr166 |
1.97 |
import java.lang.reflect.Method; |
32 |
jsr166 |
1.134 |
import java.lang.reflect.Modifier; |
33 |
jsr166 |
1.171 |
import java.nio.file.Files; |
34 |
|
|
import java.nio.file.Paths; |
35 |
jsr166 |
1.123 |
import java.security.CodeSource; |
36 |
|
|
import java.security.Permission; |
37 |
|
|
import java.security.PermissionCollection; |
38 |
|
|
import java.security.Permissions; |
39 |
|
|
import java.security.Policy; |
40 |
|
|
import java.security.ProtectionDomain; |
41 |
|
|
import java.security.SecurityPermission; |
42 |
jsr166 |
1.93 |
import java.util.ArrayList; |
43 |
jsr166 |
1.72 |
import java.util.Arrays; |
44 |
jsr166 |
1.208 |
import java.util.Collection; |
45 |
jsr166 |
1.200 |
import java.util.Collections; |
46 |
jsr166 |
1.81 |
import java.util.Date; |
47 |
jsr166 |
1.93 |
import java.util.Enumeration; |
48 |
jsr166 |
1.126 |
import java.util.Iterator; |
49 |
jsr166 |
1.93 |
import java.util.List; |
50 |
jsr166 |
1.72 |
import java.util.NoSuchElementException; |
51 |
jsr166 |
1.53 |
import java.util.PropertyPermission; |
52 |
jsr166 |
1.123 |
import java.util.concurrent.BlockingQueue; |
53 |
|
|
import java.util.concurrent.Callable; |
54 |
|
|
import java.util.concurrent.CountDownLatch; |
55 |
|
|
import java.util.concurrent.CyclicBarrier; |
56 |
jsr166 |
1.139 |
import java.util.concurrent.ExecutionException; |
57 |
|
|
import java.util.concurrent.Executors; |
58 |
jsr166 |
1.123 |
import java.util.concurrent.ExecutorService; |
59 |
jsr166 |
1.146 |
import java.util.concurrent.ForkJoinPool; |
60 |
jsr166 |
1.123 |
import java.util.concurrent.Future; |
61 |
|
|
import java.util.concurrent.RecursiveAction; |
62 |
|
|
import java.util.concurrent.RecursiveTask; |
63 |
|
|
import java.util.concurrent.RejectedExecutionHandler; |
64 |
|
|
import java.util.concurrent.Semaphore; |
65 |
jsr166 |
1.193 |
import java.util.concurrent.SynchronousQueue; |
66 |
jsr166 |
1.123 |
import java.util.concurrent.ThreadFactory; |
67 |
jsr166 |
1.200 |
import java.util.concurrent.ThreadLocalRandom; |
68 |
jsr166 |
1.123 |
import java.util.concurrent.ThreadPoolExecutor; |
69 |
|
|
import java.util.concurrent.TimeoutException; |
70 |
jsr166 |
1.185 |
import java.util.concurrent.atomic.AtomicBoolean; |
71 |
jsr166 |
1.53 |
import java.util.concurrent.atomic.AtomicReference; |
72 |
jsr166 |
1.171 |
import java.util.regex.Matcher; |
73 |
jsr166 |
1.114 |
import java.util.regex.Pattern; |
74 |
jsr166 |
1.123 |
|
75 |
|
|
import junit.framework.AssertionFailedError; |
76 |
|
|
import junit.framework.Test; |
77 |
|
|
import junit.framework.TestCase; |
78 |
jsr166 |
1.131 |
import junit.framework.TestResult; |
79 |
jsr166 |
1.123 |
import junit.framework.TestSuite; |
80 |
dl |
1.1 |
|
81 |
|
|
/** |
82 |
dl |
1.5 |
* Base class for JSR166 Junit TCK tests. Defines some constants, |
83 |
|
|
* utility methods and classes, as well as a simple framework for |
84 |
|
|
* helping to make sure that assertions failing in generated threads |
85 |
|
|
* cause the associated test that generated them to itself fail (which |
86 |
jsr166 |
1.27 |
* JUnit does not otherwise arrange). The rules for creating such |
87 |
dl |
1.5 |
* tests are: |
88 |
dl |
1.1 |
* |
89 |
|
|
* <ol> |
90 |
|
|
* |
91 |
jsr166 |
1.143 |
* <li>All assertions in code running in generated threads must use |
92 |
jsr166 |
1.27 |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
93 |
dl |
1.18 |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
94 |
jsr166 |
1.50 |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
95 |
dl |
1.1 |
* particularly recommended) for other code to use these forms too. |
96 |
|
|
* Only the most typically used JUnit assertion methods are defined |
97 |
jsr166 |
1.143 |
* this way, but enough to live with. |
98 |
dl |
1.1 |
* |
99 |
jsr166 |
1.143 |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
100 |
jsr166 |
1.50 |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
101 |
dl |
1.1 |
* them. These methods are used to clear and check for thread |
102 |
jsr166 |
1.143 |
* assertion failures. |
103 |
dl |
1.1 |
* |
104 |
jsr166 |
1.51 |
* <li>All delays and timeouts must use one of the constants {@code |
105 |
jsr166 |
1.50 |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
106 |
|
|
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
107 |
dl |
1.5 |
* discriminable from zero time, and always allows enough time for the |
108 |
|
|
* small amounts of computation (creating a thread, calling a few |
109 |
dl |
1.1 |
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
110 |
|
|
* is always discriminable as larger than SHORT and smaller than |
111 |
|
|
* MEDIUM. And so on. These constants are set to conservative values, |
112 |
dl |
1.2 |
* but even so, if there is ever any doubt, they can all be increased |
113 |
jsr166 |
1.143 |
* in one spot to rerun tests on slower platforms. |
114 |
dl |
1.1 |
* |
115 |
jsr166 |
1.143 |
* <li>All threads generated must be joined inside each test case |
116 |
jsr166 |
1.50 |
* method (or {@code fail} to do so) before returning from the |
117 |
|
|
* method. The {@code joinPool} method can be used to do this when |
118 |
jsr166 |
1.143 |
* using Executors. |
119 |
dl |
1.1 |
* |
120 |
|
|
* </ol> |
121 |
dl |
1.6 |
* |
122 |
jsr166 |
1.92 |
* <p><b>Other notes</b> |
123 |
dl |
1.6 |
* <ul> |
124 |
|
|
* |
125 |
jsr166 |
1.143 |
* <li>Usually, there is one testcase method per JSR166 method |
126 |
dl |
1.6 |
* covering "normal" operation, and then as many exception-testing |
127 |
|
|
* methods as there are exceptions the method can throw. Sometimes |
128 |
|
|
* there are multiple tests per JSR166 method when the different |
129 |
|
|
* "normal" behaviors differ significantly. And sometimes testcases |
130 |
jsr166 |
1.180 |
* cover multiple methods when they cannot be tested in isolation. |
131 |
jsr166 |
1.27 |
* |
132 |
jsr166 |
1.143 |
* <li>The documentation style for testcases is to provide as javadoc |
133 |
dl |
1.6 |
* a simple sentence or two describing the property that the testcase |
134 |
|
|
* method purports to test. The javadocs do not say anything about how |
135 |
jsr166 |
1.143 |
* the property is tested. To find out, read the code. |
136 |
dl |
1.6 |
* |
137 |
jsr166 |
1.143 |
* <li>These tests are "conformance tests", and do not attempt to |
138 |
dl |
1.6 |
* test throughput, latency, scalability or other performance factors |
139 |
|
|
* (see the separate "jtreg" tests for a set intended to check these |
140 |
|
|
* for the most central aspects of functionality.) So, most tests use |
141 |
|
|
* the smallest sensible numbers of threads, collection sizes, etc |
142 |
jsr166 |
1.143 |
* needed to check basic conformance. |
143 |
dl |
1.6 |
* |
144 |
|
|
* <li>The test classes currently do not declare inclusion in |
145 |
|
|
* any particular package to simplify things for people integrating |
146 |
jsr166 |
1.143 |
* them in TCK test suites. |
147 |
dl |
1.6 |
* |
148 |
jsr166 |
1.143 |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
149 |
|
|
* runs all JSR166 unit tests. |
150 |
dl |
1.6 |
* |
151 |
|
|
* </ul> |
152 |
dl |
1.1 |
*/ |
153 |
|
|
public class JSR166TestCase extends TestCase { |
154 |
jsr166 |
1.49 |
private static final boolean useSecurityManager = |
155 |
|
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
156 |
|
|
|
157 |
jsr166 |
1.62 |
protected static final boolean expensiveTests = |
158 |
|
|
Boolean.getBoolean("jsr166.expensiveTests"); |
159 |
|
|
|
160 |
jsr166 |
1.119 |
/** |
161 |
|
|
* If true, also run tests that are not part of the official tck |
162 |
|
|
* because they test unspecified implementation details. |
163 |
|
|
*/ |
164 |
jsr166 |
1.118 |
protected static final boolean testImplementationDetails = |
165 |
|
|
Boolean.getBoolean("jsr166.testImplementationDetails"); |
166 |
|
|
|
167 |
dl |
1.6 |
/** |
168 |
jsr166 |
1.61 |
* If true, report on stdout all "slow" tests, that is, ones that |
169 |
|
|
* take more than profileThreshold milliseconds to execute. |
170 |
|
|
*/ |
171 |
|
|
private static final boolean profileTests = |
172 |
|
|
Boolean.getBoolean("jsr166.profileTests"); |
173 |
|
|
|
174 |
|
|
/** |
175 |
|
|
* The number of milliseconds that tests are permitted for |
176 |
|
|
* execution without being reported, when profileTests is set. |
177 |
|
|
*/ |
178 |
|
|
private static final long profileThreshold = |
179 |
|
|
Long.getLong("jsr166.profileThreshold", 100); |
180 |
|
|
|
181 |
jsr166 |
1.107 |
/** |
182 |
|
|
* The number of repetitions per test (for tickling rare bugs). |
183 |
|
|
*/ |
184 |
|
|
private static final int runsPerTest = |
185 |
|
|
Integer.getInteger("jsr166.runsPerTest", 1); |
186 |
|
|
|
187 |
jsr166 |
1.114 |
/** |
188 |
jsr166 |
1.131 |
* The number of repetitions of the test suite (for finding leaks?). |
189 |
|
|
*/ |
190 |
|
|
private static final int suiteRuns = |
191 |
|
|
Integer.getInteger("jsr166.suiteRuns", 1); |
192 |
|
|
|
193 |
jsr166 |
1.189 |
/** |
194 |
|
|
* Returns the value of the system property, or NaN if not defined. |
195 |
|
|
*/ |
196 |
|
|
private static float systemPropertyValue(String name) { |
197 |
jsr166 |
1.185 |
String floatString = System.getProperty(name); |
198 |
|
|
if (floatString == null) |
199 |
jsr166 |
1.189 |
return Float.NaN; |
200 |
jsr166 |
1.185 |
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 |
jsr166 |
1.181 |
/** |
210 |
|
|
* The scaling factor to apply to standard delays used in tests. |
211 |
jsr166 |
1.189 |
* 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 |
jsr166 |
1.181 |
|
231 |
jsr166 |
1.134 |
public JSR166TestCase() { super(); } |
232 |
|
|
public JSR166TestCase(String name) { super(name); } |
233 |
|
|
|
234 |
jsr166 |
1.131 |
/** |
235 |
jsr166 |
1.114 |
* A filter for tests to run, matching strings of the form |
236 |
|
|
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
237 |
|
|
* Usefully combined with jsr166.runsPerTest. |
238 |
|
|
*/ |
239 |
|
|
private static final Pattern methodFilter = methodFilter(); |
240 |
|
|
|
241 |
|
|
private static Pattern methodFilter() { |
242 |
|
|
String regex = System.getProperty("jsr166.methodFilter"); |
243 |
|
|
return (regex == null) ? null : Pattern.compile(regex); |
244 |
|
|
} |
245 |
|
|
|
246 |
jsr166 |
1.170 |
// Instrumentation to debug very rare, but very annoying hung test runs. |
247 |
jsr166 |
1.156 |
static volatile TestCase currentTestCase; |
248 |
jsr166 |
1.173 |
// static volatile int currentRun = 0; |
249 |
jsr166 |
1.156 |
static { |
250 |
|
|
Runnable checkForWedgedTest = new Runnable() { public void run() { |
251 |
jsr166 |
1.174 |
// Avoid spurious reports with enormous runsPerTest. |
252 |
|
|
// A single test case run should never take more than 1 second. |
253 |
|
|
// But let's cap it at the high end too ... |
254 |
|
|
final int timeoutMinutes = |
255 |
|
|
Math.min(15, Math.max(runsPerTest / 60, 1)); |
256 |
jsr166 |
1.156 |
for (TestCase lastTestCase = currentTestCase;;) { |
257 |
jsr166 |
1.157 |
try { MINUTES.sleep(timeoutMinutes); } |
258 |
jsr166 |
1.156 |
catch (InterruptedException unexpected) { break; } |
259 |
|
|
if (lastTestCase == currentTestCase) { |
260 |
jsr166 |
1.170 |
System.err.printf( |
261 |
jsr166 |
1.173 |
"Looks like we're stuck running test: %s%n", |
262 |
|
|
lastTestCase); |
263 |
|
|
// System.err.printf( |
264 |
|
|
// "Looks like we're stuck running test: %s (%d/%d)%n", |
265 |
|
|
// lastTestCase, currentRun, runsPerTest); |
266 |
jsr166 |
1.175 |
// System.err.println("availableProcessors=" + |
267 |
|
|
// Runtime.getRuntime().availableProcessors()); |
268 |
|
|
// System.err.printf("cpu model = %s%n", cpuModel()); |
269 |
jsr166 |
1.156 |
dumpTestThreads(); |
270 |
jsr166 |
1.165 |
// one stack dump is probably enough; more would be spam |
271 |
|
|
break; |
272 |
jsr166 |
1.156 |
} |
273 |
|
|
lastTestCase = currentTestCase; |
274 |
|
|
}}}; |
275 |
|
|
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
276 |
|
|
thread.setDaemon(true); |
277 |
|
|
thread.start(); |
278 |
|
|
} |
279 |
|
|
|
280 |
jsr166 |
1.175 |
// public static String cpuModel() { |
281 |
|
|
// try { |
282 |
|
|
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
283 |
|
|
// .matcher(new String( |
284 |
|
|
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
285 |
|
|
// matcher.find(); |
286 |
|
|
// return matcher.group(1); |
287 |
|
|
// } catch (Exception ex) { return null; } |
288 |
|
|
// } |
289 |
jsr166 |
1.171 |
|
290 |
jsr166 |
1.147 |
public void runBare() throws Throwable { |
291 |
jsr166 |
1.156 |
currentTestCase = this; |
292 |
jsr166 |
1.147 |
if (methodFilter == null |
293 |
|
|
|| methodFilter.matcher(toString()).find()) |
294 |
|
|
super.runBare(); |
295 |
|
|
} |
296 |
|
|
|
297 |
jsr166 |
1.61 |
protected void runTest() throws Throwable { |
298 |
jsr166 |
1.147 |
for (int i = 0; i < runsPerTest; i++) { |
299 |
jsr166 |
1.173 |
// currentRun = i; |
300 |
jsr166 |
1.147 |
if (profileTests) |
301 |
|
|
runTestProfiled(); |
302 |
|
|
else |
303 |
|
|
super.runTest(); |
304 |
jsr166 |
1.107 |
} |
305 |
jsr166 |
1.61 |
} |
306 |
|
|
|
307 |
|
|
protected void runTestProfiled() throws Throwable { |
308 |
jsr166 |
1.145 |
for (int i = 0; i < 2; i++) { |
309 |
|
|
long startTime = System.nanoTime(); |
310 |
jsr166 |
1.61 |
super.runTest(); |
311 |
jsr166 |
1.145 |
long elapsedMillis = millisElapsedSince(startTime); |
312 |
|
|
if (elapsedMillis < profileThreshold) |
313 |
|
|
break; |
314 |
|
|
// Never report first run of any test; treat it as a |
315 |
|
|
// warmup run, notably to trigger all needed classloading, |
316 |
|
|
if (i > 0) |
317 |
jsr166 |
1.61 |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
318 |
|
|
} |
319 |
|
|
} |
320 |
jsr166 |
1.63 |
|
321 |
jsr166 |
1.61 |
/** |
322 |
jsr166 |
1.94 |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
323 |
jsr166 |
1.27 |
*/ |
324 |
jsr166 |
1.41 |
public static void main(String[] args) { |
325 |
jsr166 |
1.131 |
main(suite(), args); |
326 |
|
|
} |
327 |
|
|
|
328 |
jsr166 |
1.178 |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
329 |
|
|
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
330 |
|
|
long runTime; |
331 |
|
|
public void startTest(Test test) {} |
332 |
jsr166 |
1.179 |
protected void printHeader(long runTime) { |
333 |
jsr166 |
1.178 |
this.runTime = runTime; // defer printing for later |
334 |
jsr166 |
1.179 |
} |
335 |
|
|
protected void printFooter(TestResult result) { |
336 |
jsr166 |
1.178 |
if (result.wasSuccessful()) { |
337 |
|
|
getWriter().println("OK (" + result.runCount() + " tests)" |
338 |
|
|
+ " Time: " + elapsedTimeAsString(runTime)); |
339 |
|
|
} else { |
340 |
|
|
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
341 |
|
|
super.printFooter(result); |
342 |
|
|
} |
343 |
|
|
} |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
/** |
347 |
|
|
* Returns a TestRunner that doesn't bother with unnecessary |
348 |
|
|
* fluff, like printing a "." for each test case. |
349 |
|
|
*/ |
350 |
|
|
static junit.textui.TestRunner newPithyTestRunner() { |
351 |
|
|
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
352 |
|
|
runner.setPrinter(new PithyResultPrinter(System.out)); |
353 |
|
|
return runner; |
354 |
|
|
} |
355 |
|
|
|
356 |
jsr166 |
1.131 |
/** |
357 |
|
|
* Runs all unit tests in the given test suite. |
358 |
jsr166 |
1.132 |
* Actual behavior influenced by jsr166.* system properties. |
359 |
jsr166 |
1.131 |
*/ |
360 |
|
|
static void main(Test suite, String[] args) { |
361 |
jsr166 |
1.49 |
if (useSecurityManager) { |
362 |
|
|
System.err.println("Setting a permissive security manager"); |
363 |
|
|
Policy.setPolicy(permissivePolicy()); |
364 |
|
|
System.setSecurityManager(new SecurityManager()); |
365 |
|
|
} |
366 |
jsr166 |
1.131 |
for (int i = 0; i < suiteRuns; i++) { |
367 |
jsr166 |
1.178 |
TestResult result = newPithyTestRunner().doRun(suite); |
368 |
jsr166 |
1.131 |
if (!result.wasSuccessful()) |
369 |
|
|
System.exit(1); |
370 |
dl |
1.22 |
System.gc(); |
371 |
|
|
System.runFinalization(); |
372 |
|
|
} |
373 |
dl |
1.6 |
} |
374 |
|
|
|
375 |
jsr166 |
1.60 |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
376 |
|
|
TestSuite suite = new TestSuite(); |
377 |
|
|
for (Object suiteOrClass : suiteOrClasses) { |
378 |
|
|
if (suiteOrClass instanceof TestSuite) |
379 |
|
|
suite.addTest((TestSuite) suiteOrClass); |
380 |
|
|
else if (suiteOrClass instanceof Class) |
381 |
|
|
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
382 |
|
|
else |
383 |
|
|
throw new ClassCastException("not a test suite or class"); |
384 |
|
|
} |
385 |
|
|
return suite; |
386 |
|
|
} |
387 |
|
|
|
388 |
jsr166 |
1.98 |
public static void addNamedTestClasses(TestSuite suite, |
389 |
|
|
String... testClassNames) { |
390 |
|
|
for (String testClassName : testClassNames) { |
391 |
|
|
try { |
392 |
|
|
Class<?> testClass = Class.forName(testClassName); |
393 |
|
|
Method m = testClass.getDeclaredMethod("suite", |
394 |
|
|
new Class<?>[0]); |
395 |
|
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
396 |
|
|
} catch (Exception e) { |
397 |
|
|
throw new Error("Missing test class", e); |
398 |
|
|
} |
399 |
jsr166 |
1.97 |
} |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
public static final double JAVA_CLASS_VERSION; |
403 |
jsr166 |
1.115 |
public static final String JAVA_SPECIFICATION_VERSION; |
404 |
jsr166 |
1.97 |
static { |
405 |
|
|
try { |
406 |
|
|
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
407 |
|
|
new java.security.PrivilegedAction<Double>() { |
408 |
|
|
public Double run() { |
409 |
|
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
410 |
jsr166 |
1.115 |
JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged( |
411 |
|
|
new java.security.PrivilegedAction<String>() { |
412 |
|
|
public String run() { |
413 |
|
|
return System.getProperty("java.specification.version");}}); |
414 |
jsr166 |
1.97 |
} catch (Throwable t) { |
415 |
|
|
throw new Error(t); |
416 |
|
|
} |
417 |
|
|
} |
418 |
|
|
|
419 |
jsr166 |
1.98 |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
420 |
|
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
421 |
|
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
422 |
jsr166 |
1.136 |
public static boolean atLeastJava9() { |
423 |
jsr166 |
1.137 |
return JAVA_CLASS_VERSION >= 53.0 |
424 |
|
|
// As of 2015-09, java9 still uses 52.0 class file version |
425 |
|
|
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
426 |
jsr166 |
1.136 |
} |
427 |
|
|
public static boolean atLeastJava10() { |
428 |
jsr166 |
1.137 |
return JAVA_CLASS_VERSION >= 54.0 |
429 |
|
|
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
430 |
jsr166 |
1.136 |
} |
431 |
jsr166 |
1.97 |
|
432 |
dl |
1.6 |
/** |
433 |
jsr166 |
1.60 |
* Collects all JSR166 unit tests as one suite. |
434 |
jsr166 |
1.27 |
*/ |
435 |
jsr166 |
1.41 |
public static Test suite() { |
436 |
jsr166 |
1.98 |
// Java7+ test classes |
437 |
jsr166 |
1.97 |
TestSuite suite = newTestSuite( |
438 |
jsr166 |
1.60 |
ForkJoinPoolTest.suite(), |
439 |
|
|
ForkJoinTaskTest.suite(), |
440 |
|
|
RecursiveActionTest.suite(), |
441 |
|
|
RecursiveTaskTest.suite(), |
442 |
|
|
LinkedTransferQueueTest.suite(), |
443 |
|
|
PhaserTest.suite(), |
444 |
|
|
ThreadLocalRandomTest.suite(), |
445 |
|
|
AbstractExecutorServiceTest.suite(), |
446 |
|
|
AbstractQueueTest.suite(), |
447 |
|
|
AbstractQueuedSynchronizerTest.suite(), |
448 |
|
|
AbstractQueuedLongSynchronizerTest.suite(), |
449 |
|
|
ArrayBlockingQueueTest.suite(), |
450 |
|
|
ArrayDequeTest.suite(), |
451 |
jsr166 |
1.205 |
ArrayListTest.suite(), |
452 |
jsr166 |
1.60 |
AtomicBooleanTest.suite(), |
453 |
|
|
AtomicIntegerArrayTest.suite(), |
454 |
|
|
AtomicIntegerFieldUpdaterTest.suite(), |
455 |
|
|
AtomicIntegerTest.suite(), |
456 |
|
|
AtomicLongArrayTest.suite(), |
457 |
|
|
AtomicLongFieldUpdaterTest.suite(), |
458 |
|
|
AtomicLongTest.suite(), |
459 |
|
|
AtomicMarkableReferenceTest.suite(), |
460 |
|
|
AtomicReferenceArrayTest.suite(), |
461 |
|
|
AtomicReferenceFieldUpdaterTest.suite(), |
462 |
|
|
AtomicReferenceTest.suite(), |
463 |
|
|
AtomicStampedReferenceTest.suite(), |
464 |
|
|
ConcurrentHashMapTest.suite(), |
465 |
|
|
ConcurrentLinkedDequeTest.suite(), |
466 |
|
|
ConcurrentLinkedQueueTest.suite(), |
467 |
|
|
ConcurrentSkipListMapTest.suite(), |
468 |
|
|
ConcurrentSkipListSubMapTest.suite(), |
469 |
|
|
ConcurrentSkipListSetTest.suite(), |
470 |
|
|
ConcurrentSkipListSubSetTest.suite(), |
471 |
|
|
CopyOnWriteArrayListTest.suite(), |
472 |
|
|
CopyOnWriteArraySetTest.suite(), |
473 |
|
|
CountDownLatchTest.suite(), |
474 |
jsr166 |
1.204 |
CountedCompleterTest.suite(), |
475 |
jsr166 |
1.60 |
CyclicBarrierTest.suite(), |
476 |
|
|
DelayQueueTest.suite(), |
477 |
|
|
EntryTest.suite(), |
478 |
|
|
ExchangerTest.suite(), |
479 |
|
|
ExecutorsTest.suite(), |
480 |
|
|
ExecutorCompletionServiceTest.suite(), |
481 |
|
|
FutureTaskTest.suite(), |
482 |
|
|
LinkedBlockingDequeTest.suite(), |
483 |
|
|
LinkedBlockingQueueTest.suite(), |
484 |
|
|
LinkedListTest.suite(), |
485 |
|
|
LockSupportTest.suite(), |
486 |
|
|
PriorityBlockingQueueTest.suite(), |
487 |
|
|
PriorityQueueTest.suite(), |
488 |
|
|
ReentrantLockTest.suite(), |
489 |
|
|
ReentrantReadWriteLockTest.suite(), |
490 |
|
|
ScheduledExecutorTest.suite(), |
491 |
|
|
ScheduledExecutorSubclassTest.suite(), |
492 |
|
|
SemaphoreTest.suite(), |
493 |
|
|
SynchronousQueueTest.suite(), |
494 |
|
|
SystemTest.suite(), |
495 |
|
|
ThreadLocalTest.suite(), |
496 |
|
|
ThreadPoolExecutorTest.suite(), |
497 |
|
|
ThreadPoolExecutorSubclassTest.suite(), |
498 |
|
|
ThreadTest.suite(), |
499 |
|
|
TimeUnitTest.suite(), |
500 |
|
|
TreeMapTest.suite(), |
501 |
|
|
TreeSetTest.suite(), |
502 |
|
|
TreeSubMapTest.suite(), |
503 |
jsr166 |
1.207 |
TreeSubSetTest.suite(), |
504 |
|
|
VectorTest.suite()); |
505 |
jsr166 |
1.98 |
|
506 |
|
|
// Java8+ test classes |
507 |
|
|
if (atLeastJava8()) { |
508 |
|
|
String[] java8TestClassNames = { |
509 |
jsr166 |
1.206 |
"ArrayDeque8Test", |
510 |
dl |
1.113 |
"Atomic8Test", |
511 |
dl |
1.104 |
"CompletableFutureTest", |
512 |
dl |
1.105 |
"ConcurrentHashMap8Test", |
513 |
jsr166 |
1.204 |
"CountedCompleter8Test", |
514 |
dl |
1.104 |
"DoubleAccumulatorTest", |
515 |
dl |
1.103 |
"DoubleAdderTest", |
516 |
jsr166 |
1.102 |
"ForkJoinPool8Test", |
517 |
dl |
1.111 |
"ForkJoinTask8Test", |
518 |
dl |
1.104 |
"LongAccumulatorTest", |
519 |
|
|
"LongAdderTest", |
520 |
jsr166 |
1.110 |
"SplittableRandomTest", |
521 |
jsr166 |
1.98 |
"StampedLockTest", |
522 |
dl |
1.140 |
"SubmissionPublisherTest", |
523 |
jsr166 |
1.112 |
"ThreadLocalRandom8Test", |
524 |
jsr166 |
1.190 |
"TimeUnit8Test", |
525 |
jsr166 |
1.98 |
}; |
526 |
|
|
addNamedTestClasses(suite, java8TestClassNames); |
527 |
jsr166 |
1.97 |
} |
528 |
jsr166 |
1.98 |
|
529 |
jsr166 |
1.115 |
// Java9+ test classes |
530 |
|
|
if (atLeastJava9()) { |
531 |
|
|
String[] java9TestClassNames = { |
532 |
jsr166 |
1.196 |
"AtomicBoolean9Test", |
533 |
|
|
"AtomicInteger9Test", |
534 |
|
|
"AtomicIntegerArray9Test", |
535 |
|
|
"AtomicLong9Test", |
536 |
|
|
"AtomicLongArray9Test", |
537 |
|
|
"AtomicReference9Test", |
538 |
|
|
"AtomicReferenceArray9Test", |
539 |
jsr166 |
1.192 |
"ExecutorCompletionService9Test", |
540 |
jsr166 |
1.115 |
}; |
541 |
|
|
addNamedTestClasses(suite, java9TestClassNames); |
542 |
|
|
} |
543 |
|
|
|
544 |
jsr166 |
1.97 |
return suite; |
545 |
dl |
1.6 |
} |
546 |
|
|
|
547 |
jsr166 |
1.134 |
/** Returns list of junit-style test method names in given class. */ |
548 |
|
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
549 |
|
|
Method[] methods = testClass.getDeclaredMethods(); |
550 |
|
|
ArrayList<String> names = new ArrayList<String>(methods.length); |
551 |
|
|
for (Method method : methods) { |
552 |
|
|
if (method.getName().startsWith("test") |
553 |
|
|
&& Modifier.isPublic(method.getModifiers()) |
554 |
|
|
// method.getParameterCount() requires jdk8+ |
555 |
|
|
&& method.getParameterTypes().length == 0) { |
556 |
|
|
names.add(method.getName()); |
557 |
|
|
} |
558 |
|
|
} |
559 |
|
|
return names; |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
/** |
563 |
|
|
* Returns junit-style testSuite for the given test class, but |
564 |
|
|
* parameterized by passing extra data to each test. |
565 |
|
|
*/ |
566 |
|
|
public static <ExtraData> Test parameterizedTestSuite |
567 |
|
|
(Class<? extends JSR166TestCase> testClass, |
568 |
|
|
Class<ExtraData> dataClass, |
569 |
|
|
ExtraData data) { |
570 |
|
|
try { |
571 |
|
|
TestSuite suite = new TestSuite(); |
572 |
|
|
Constructor c = |
573 |
|
|
testClass.getDeclaredConstructor(dataClass, String.class); |
574 |
|
|
for (String methodName : testMethodNames(testClass)) |
575 |
|
|
suite.addTest((Test) c.newInstance(data, methodName)); |
576 |
|
|
return suite; |
577 |
|
|
} catch (Exception e) { |
578 |
|
|
throw new Error(e); |
579 |
|
|
} |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
/** |
583 |
|
|
* Returns junit-style testSuite for the jdk8 extension of the |
584 |
|
|
* given test class, but parameterized by passing extra data to |
585 |
|
|
* each test. Uses reflection to allow compilation in jdk7. |
586 |
|
|
*/ |
587 |
|
|
public static <ExtraData> Test jdk8ParameterizedTestSuite |
588 |
|
|
(Class<? extends JSR166TestCase> testClass, |
589 |
|
|
Class<ExtraData> dataClass, |
590 |
|
|
ExtraData data) { |
591 |
|
|
if (atLeastJava8()) { |
592 |
|
|
String name = testClass.getName(); |
593 |
|
|
String name8 = name.replaceAll("Test$", "8Test"); |
594 |
|
|
if (name.equals(name8)) throw new Error(name); |
595 |
|
|
try { |
596 |
|
|
return (Test) |
597 |
|
|
Class.forName(name8) |
598 |
|
|
.getMethod("testSuite", new Class[] { dataClass }) |
599 |
|
|
.invoke(null, data); |
600 |
|
|
} catch (Exception e) { |
601 |
|
|
throw new Error(e); |
602 |
|
|
} |
603 |
|
|
} else { |
604 |
|
|
return new TestSuite(); |
605 |
|
|
} |
606 |
|
|
} |
607 |
|
|
|
608 |
jsr166 |
1.109 |
// Delays for timing-dependent tests, in milliseconds. |
609 |
dl |
1.1 |
|
610 |
dl |
1.2 |
public static long SHORT_DELAY_MS; |
611 |
|
|
public static long SMALL_DELAY_MS; |
612 |
|
|
public static long MEDIUM_DELAY_MS; |
613 |
|
|
public static long LONG_DELAY_MS; |
614 |
|
|
|
615 |
|
|
/** |
616 |
jsr166 |
1.181 |
* Returns the shortest timed delay. This can be scaled up for |
617 |
jsr166 |
1.185 |
* slow machines using the jsr166.delay.factor system property, |
618 |
jsr166 |
1.187 |
* or via jtreg's -timeoutFactor: flag. |
619 |
jsr166 |
1.185 |
* http://openjdk.java.net/jtreg/command-help.html |
620 |
jsr166 |
1.27 |
*/ |
621 |
dl |
1.2 |
protected long getShortDelay() { |
622 |
jsr166 |
1.189 |
return (long) (50 * delayFactor); |
623 |
dl |
1.2 |
} |
624 |
|
|
|
625 |
|
|
/** |
626 |
jsr166 |
1.27 |
* Sets delays as multiples of SHORT_DELAY. |
627 |
dl |
1.2 |
*/ |
628 |
jsr166 |
1.43 |
protected void setDelays() { |
629 |
dl |
1.2 |
SHORT_DELAY_MS = getShortDelay(); |
630 |
jsr166 |
1.53 |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
631 |
dl |
1.2 |
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
632 |
jsr166 |
1.71 |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
633 |
dl |
1.2 |
} |
634 |
|
|
|
635 |
dl |
1.1 |
/** |
636 |
jsr166 |
1.81 |
* Returns a timeout in milliseconds to be used in tests that |
637 |
|
|
* verify that operations block or time out. |
638 |
|
|
*/ |
639 |
|
|
long timeoutMillis() { |
640 |
|
|
return SHORT_DELAY_MS / 4; |
641 |
|
|
} |
642 |
|
|
|
643 |
|
|
/** |
644 |
jsr166 |
1.135 |
* Returns a new Date instance representing a time at least |
645 |
|
|
* delayMillis milliseconds in the future. |
646 |
jsr166 |
1.81 |
*/ |
647 |
|
|
Date delayedDate(long delayMillis) { |
648 |
jsr166 |
1.135 |
// Add 1 because currentTimeMillis is known to round into the past. |
649 |
|
|
return new Date(System.currentTimeMillis() + delayMillis + 1); |
650 |
jsr166 |
1.81 |
} |
651 |
|
|
|
652 |
|
|
/** |
653 |
jsr166 |
1.53 |
* The first exception encountered if any threadAssertXXX method fails. |
654 |
dl |
1.1 |
*/ |
655 |
jsr166 |
1.53 |
private final AtomicReference<Throwable> threadFailure |
656 |
|
|
= new AtomicReference<Throwable>(null); |
657 |
dl |
1.1 |
|
658 |
|
|
/** |
659 |
jsr166 |
1.53 |
* Records an exception so that it can be rethrown later in the test |
660 |
|
|
* harness thread, triggering a test case failure. Only the first |
661 |
|
|
* failure is recorded; subsequent calls to this method from within |
662 |
|
|
* the same test have no effect. |
663 |
dl |
1.1 |
*/ |
664 |
jsr166 |
1.53 |
public void threadRecordFailure(Throwable t) { |
665 |
jsr166 |
1.158 |
System.err.println(t); |
666 |
jsr166 |
1.156 |
dumpTestThreads(); |
667 |
jsr166 |
1.53 |
threadFailure.compareAndSet(null, t); |
668 |
|
|
} |
669 |
|
|
|
670 |
jsr166 |
1.27 |
public void setUp() { |
671 |
dl |
1.2 |
setDelays(); |
672 |
dl |
1.1 |
} |
673 |
|
|
|
674 |
jsr166 |
1.146 |
void tearDownFail(String format, Object... args) { |
675 |
|
|
String msg = toString() + ": " + String.format(format, args); |
676 |
|
|
System.err.println(msg); |
677 |
jsr166 |
1.156 |
dumpTestThreads(); |
678 |
jsr166 |
1.146 |
throw new AssertionFailedError(msg); |
679 |
|
|
} |
680 |
|
|
|
681 |
dl |
1.1 |
/** |
682 |
jsr166 |
1.85 |
* Extra checks that get done for all test cases. |
683 |
|
|
* |
684 |
jsr166 |
1.53 |
* Triggers test case failure if any thread assertions have failed, |
685 |
|
|
* by rethrowing, in the test harness thread, any exception recorded |
686 |
|
|
* earlier by threadRecordFailure. |
687 |
jsr166 |
1.85 |
* |
688 |
|
|
* Triggers test case failure if interrupt status is set in the main thread. |
689 |
jsr166 |
1.53 |
*/ |
690 |
|
|
public void tearDown() throws Exception { |
691 |
jsr166 |
1.70 |
Throwable t = threadFailure.getAndSet(null); |
692 |
jsr166 |
1.53 |
if (t != null) { |
693 |
|
|
if (t instanceof Error) |
694 |
|
|
throw (Error) t; |
695 |
|
|
else if (t instanceof RuntimeException) |
696 |
|
|
throw (RuntimeException) t; |
697 |
|
|
else if (t instanceof Exception) |
698 |
|
|
throw (Exception) t; |
699 |
jsr166 |
1.57 |
else { |
700 |
|
|
AssertionFailedError afe = |
701 |
|
|
new AssertionFailedError(t.toString()); |
702 |
|
|
afe.initCause(t); |
703 |
|
|
throw afe; |
704 |
|
|
} |
705 |
jsr166 |
1.53 |
} |
706 |
jsr166 |
1.85 |
|
707 |
|
|
if (Thread.interrupted()) |
708 |
jsr166 |
1.146 |
tearDownFail("interrupt status set in main thread"); |
709 |
jsr166 |
1.100 |
|
710 |
|
|
checkForkJoinPoolThreadLeaks(); |
711 |
dl |
1.1 |
} |
712 |
|
|
|
713 |
dl |
1.5 |
/** |
714 |
jsr166 |
1.164 |
* Finds missing PoolCleaners |
715 |
jsr166 |
1.100 |
*/ |
716 |
|
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
717 |
jsr166 |
1.146 |
Thread[] survivors = new Thread[7]; |
718 |
jsr166 |
1.100 |
int count = Thread.enumerate(survivors); |
719 |
|
|
for (int i = 0; i < count; i++) { |
720 |
|
|
Thread thread = survivors[i]; |
721 |
|
|
String name = thread.getName(); |
722 |
|
|
if (name.startsWith("ForkJoinPool-")) { |
723 |
|
|
// give thread some time to terminate |
724 |
|
|
thread.join(LONG_DELAY_MS); |
725 |
jsr166 |
1.146 |
if (thread.isAlive()) |
726 |
|
|
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
727 |
|
|
thread); |
728 |
jsr166 |
1.100 |
} |
729 |
|
|
} |
730 |
jsr166 |
1.146 |
|
731 |
|
|
if (!ForkJoinPool.commonPool() |
732 |
|
|
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
733 |
|
|
tearDownFail("ForkJoin common pool thread stuck"); |
734 |
jsr166 |
1.100 |
} |
735 |
jsr166 |
1.101 |
|
736 |
jsr166 |
1.100 |
/** |
737 |
jsr166 |
1.53 |
* Just like fail(reason), but additionally recording (using |
738 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
739 |
|
|
* the current testcase will fail. |
740 |
jsr166 |
1.27 |
*/ |
741 |
dl |
1.1 |
public void threadFail(String reason) { |
742 |
jsr166 |
1.53 |
try { |
743 |
|
|
fail(reason); |
744 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
745 |
jsr166 |
1.53 |
threadRecordFailure(t); |
746 |
jsr166 |
1.152 |
throw t; |
747 |
jsr166 |
1.53 |
} |
748 |
dl |
1.1 |
} |
749 |
|
|
|
750 |
dl |
1.5 |
/** |
751 |
jsr166 |
1.53 |
* Just like assertTrue(b), but additionally recording (using |
752 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
753 |
|
|
* the current testcase will fail. |
754 |
jsr166 |
1.27 |
*/ |
755 |
dl |
1.1 |
public void threadAssertTrue(boolean b) { |
756 |
jsr166 |
1.53 |
try { |
757 |
dl |
1.1 |
assertTrue(b); |
758 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
759 |
jsr166 |
1.53 |
threadRecordFailure(t); |
760 |
|
|
throw t; |
761 |
dl |
1.1 |
} |
762 |
|
|
} |
763 |
dl |
1.5 |
|
764 |
|
|
/** |
765 |
jsr166 |
1.53 |
* Just like assertFalse(b), but additionally recording (using |
766 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
767 |
|
|
* the current testcase will fail. |
768 |
jsr166 |
1.27 |
*/ |
769 |
dl |
1.1 |
public void threadAssertFalse(boolean b) { |
770 |
jsr166 |
1.53 |
try { |
771 |
dl |
1.1 |
assertFalse(b); |
772 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
773 |
jsr166 |
1.53 |
threadRecordFailure(t); |
774 |
|
|
throw t; |
775 |
dl |
1.1 |
} |
776 |
|
|
} |
777 |
dl |
1.5 |
|
778 |
|
|
/** |
779 |
jsr166 |
1.53 |
* Just like assertNull(x), but additionally recording (using |
780 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
781 |
|
|
* the current testcase will fail. |
782 |
jsr166 |
1.27 |
*/ |
783 |
dl |
1.1 |
public void threadAssertNull(Object x) { |
784 |
jsr166 |
1.53 |
try { |
785 |
dl |
1.1 |
assertNull(x); |
786 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
787 |
jsr166 |
1.53 |
threadRecordFailure(t); |
788 |
|
|
throw t; |
789 |
dl |
1.1 |
} |
790 |
|
|
} |
791 |
dl |
1.5 |
|
792 |
|
|
/** |
793 |
jsr166 |
1.53 |
* Just like assertEquals(x, y), but additionally recording (using |
794 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
795 |
|
|
* the current testcase will fail. |
796 |
jsr166 |
1.27 |
*/ |
797 |
dl |
1.1 |
public void threadAssertEquals(long x, long y) { |
798 |
jsr166 |
1.53 |
try { |
799 |
dl |
1.1 |
assertEquals(x, y); |
800 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
801 |
jsr166 |
1.53 |
threadRecordFailure(t); |
802 |
|
|
throw t; |
803 |
dl |
1.1 |
} |
804 |
|
|
} |
805 |
dl |
1.5 |
|
806 |
|
|
/** |
807 |
jsr166 |
1.53 |
* Just like assertEquals(x, y), but additionally recording (using |
808 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
809 |
|
|
* the current testcase will fail. |
810 |
jsr166 |
1.27 |
*/ |
811 |
dl |
1.1 |
public void threadAssertEquals(Object x, Object y) { |
812 |
jsr166 |
1.53 |
try { |
813 |
dl |
1.1 |
assertEquals(x, y); |
814 |
jsr166 |
1.129 |
} catch (AssertionFailedError fail) { |
815 |
|
|
threadRecordFailure(fail); |
816 |
|
|
throw fail; |
817 |
|
|
} catch (Throwable fail) { |
818 |
|
|
threadUnexpectedException(fail); |
819 |
dl |
1.1 |
} |
820 |
|
|
} |
821 |
|
|
|
822 |
dl |
1.5 |
/** |
823 |
jsr166 |
1.53 |
* Just like assertSame(x, y), but additionally recording (using |
824 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
825 |
|
|
* the current testcase will fail. |
826 |
jsr166 |
1.52 |
*/ |
827 |
|
|
public void threadAssertSame(Object x, Object y) { |
828 |
jsr166 |
1.53 |
try { |
829 |
jsr166 |
1.52 |
assertSame(x, y); |
830 |
jsr166 |
1.129 |
} catch (AssertionFailedError fail) { |
831 |
|
|
threadRecordFailure(fail); |
832 |
|
|
throw fail; |
833 |
jsr166 |
1.52 |
} |
834 |
|
|
} |
835 |
|
|
|
836 |
|
|
/** |
837 |
jsr166 |
1.53 |
* Calls threadFail with message "should throw exception". |
838 |
jsr166 |
1.33 |
*/ |
839 |
dl |
1.3 |
public void threadShouldThrow() { |
840 |
jsr166 |
1.53 |
threadFail("should throw exception"); |
841 |
dl |
1.3 |
} |
842 |
|
|
|
843 |
dl |
1.5 |
/** |
844 |
jsr166 |
1.53 |
* Calls threadFail with message "should throw" + exceptionName. |
845 |
jsr166 |
1.40 |
*/ |
846 |
|
|
public void threadShouldThrow(String exceptionName) { |
847 |
jsr166 |
1.53 |
threadFail("should throw " + exceptionName); |
848 |
dl |
1.3 |
} |
849 |
|
|
|
850 |
dl |
1.31 |
/** |
851 |
jsr166 |
1.57 |
* Records the given exception using {@link #threadRecordFailure}, |
852 |
|
|
* then rethrows the exception, wrapping it in an |
853 |
|
|
* AssertionFailedError if necessary. |
854 |
dl |
1.31 |
*/ |
855 |
jsr166 |
1.53 |
public void threadUnexpectedException(Throwable t) { |
856 |
|
|
threadRecordFailure(t); |
857 |
|
|
t.printStackTrace(); |
858 |
|
|
if (t instanceof RuntimeException) |
859 |
|
|
throw (RuntimeException) t; |
860 |
|
|
else if (t instanceof Error) |
861 |
|
|
throw (Error) t; |
862 |
|
|
else { |
863 |
jsr166 |
1.57 |
AssertionFailedError afe = |
864 |
|
|
new AssertionFailedError("unexpected exception: " + t); |
865 |
jsr166 |
1.82 |
afe.initCause(t); |
866 |
jsr166 |
1.57 |
throw afe; |
867 |
jsr166 |
1.55 |
} |
868 |
dl |
1.31 |
} |
869 |
dl |
1.3 |
|
870 |
dl |
1.1 |
/** |
871 |
jsr166 |
1.81 |
* Delays, via Thread.sleep, for the given millisecond delay, but |
872 |
dl |
1.76 |
* if the sleep is shorter than specified, may re-sleep or yield |
873 |
jsr166 |
1.163 |
* until time elapses. Ensures that the given time, as measured |
874 |
|
|
* by System.nanoTime(), has elapsed. |
875 |
dl |
1.76 |
*/ |
876 |
jsr166 |
1.81 |
static void delay(long millis) throws InterruptedException { |
877 |
jsr166 |
1.163 |
long nanos = millis * (1000 * 1000); |
878 |
|
|
final long wakeupTime = System.nanoTime() + nanos; |
879 |
|
|
do { |
880 |
jsr166 |
1.80 |
if (millis > 0L) |
881 |
|
|
Thread.sleep(millis); |
882 |
dl |
1.76 |
else // too short to sleep |
883 |
|
|
Thread.yield(); |
884 |
jsr166 |
1.163 |
nanos = wakeupTime - System.nanoTime(); |
885 |
|
|
millis = nanos / (1000 * 1000); |
886 |
|
|
} while (nanos >= 0L); |
887 |
dl |
1.76 |
} |
888 |
|
|
|
889 |
|
|
/** |
890 |
jsr166 |
1.149 |
* Allows use of try-with-resources with per-test thread pools. |
891 |
|
|
*/ |
892 |
jsr166 |
1.160 |
class PoolCleaner implements AutoCloseable { |
893 |
|
|
private final ExecutorService pool; |
894 |
|
|
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
895 |
jsr166 |
1.149 |
public void close() { joinPool(pool); } |
896 |
|
|
} |
897 |
|
|
|
898 |
jsr166 |
1.166 |
/** |
899 |
|
|
* An extension of PoolCleaner that has an action to release the pool. |
900 |
|
|
*/ |
901 |
|
|
class PoolCleanerWithReleaser extends PoolCleaner { |
902 |
|
|
private final Runnable releaser; |
903 |
|
|
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
904 |
|
|
super(pool); |
905 |
|
|
this.releaser = releaser; |
906 |
|
|
} |
907 |
|
|
public void close() { |
908 |
|
|
try { |
909 |
|
|
releaser.run(); |
910 |
|
|
} finally { |
911 |
|
|
super.close(); |
912 |
|
|
} |
913 |
|
|
} |
914 |
|
|
} |
915 |
|
|
|
916 |
jsr166 |
1.160 |
PoolCleaner cleaner(ExecutorService pool) { |
917 |
|
|
return new PoolCleaner(pool); |
918 |
jsr166 |
1.159 |
} |
919 |
|
|
|
920 |
jsr166 |
1.166 |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
921 |
|
|
return new PoolCleanerWithReleaser(pool, releaser); |
922 |
|
|
} |
923 |
|
|
|
924 |
|
|
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
925 |
|
|
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
926 |
|
|
} |
927 |
|
|
|
928 |
|
|
Runnable releaser(final CountDownLatch latch) { |
929 |
|
|
return new Runnable() { public void run() { |
930 |
|
|
do { latch.countDown(); } |
931 |
|
|
while (latch.getCount() > 0); |
932 |
|
|
}}; |
933 |
|
|
} |
934 |
|
|
|
935 |
jsr166 |
1.185 |
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 |
jsr166 |
1.149 |
/** |
944 |
jsr166 |
1.53 |
* Waits out termination of a thread pool or fails doing so. |
945 |
dl |
1.1 |
*/ |
946 |
jsr166 |
1.158 |
void joinPool(ExecutorService pool) { |
947 |
dl |
1.1 |
try { |
948 |
jsr166 |
1.139 |
pool.shutdown(); |
949 |
jsr166 |
1.158 |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
950 |
|
|
try { |
951 |
|
|
threadFail("ExecutorService " + pool + |
952 |
|
|
" did not terminate in a timely manner"); |
953 |
|
|
} finally { |
954 |
|
|
// last resort, for the benefit of subsequent tests |
955 |
|
|
pool.shutdownNow(); |
956 |
jsr166 |
1.167 |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
957 |
jsr166 |
1.158 |
} |
958 |
|
|
} |
959 |
jsr166 |
1.33 |
} catch (SecurityException ok) { |
960 |
dl |
1.22 |
// Allowed in case test doesn't have privs |
961 |
jsr166 |
1.128 |
} catch (InterruptedException fail) { |
962 |
jsr166 |
1.158 |
threadFail("Unexpected InterruptedException"); |
963 |
dl |
1.1 |
} |
964 |
|
|
} |
965 |
|
|
|
966 |
jsr166 |
1.197 |
/** |
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 |
jsr166 |
1.141 |
interface Action { public void run() throws Throwable; } |
972 |
jsr166 |
1.139 |
|
973 |
|
|
/** |
974 |
jsr166 |
1.141 |
* Runs all the given actions in parallel, failing if any fail. |
975 |
jsr166 |
1.139 |
* Useful for running multiple variants of tests that are |
976 |
|
|
* necessarily individually slow because they must block. |
977 |
|
|
*/ |
978 |
jsr166 |
1.141 |
void testInParallel(Action ... actions) { |
979 |
jsr166 |
1.160 |
ExecutorService pool = Executors.newCachedThreadPool(); |
980 |
|
|
try (PoolCleaner cleaner = cleaner(pool)) { |
981 |
jsr166 |
1.141 |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
982 |
|
|
for (final Action action : actions) |
983 |
jsr166 |
1.139 |
futures.add(pool.submit(new CheckedRunnable() { |
984 |
jsr166 |
1.141 |
public void realRun() throws Throwable { action.run();}})); |
985 |
jsr166 |
1.139 |
for (Future<?> future : futures) |
986 |
|
|
try { |
987 |
|
|
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
988 |
|
|
} catch (ExecutionException ex) { |
989 |
|
|
threadUnexpectedException(ex.getCause()); |
990 |
|
|
} catch (Exception ex) { |
991 |
|
|
threadUnexpectedException(ex); |
992 |
|
|
} |
993 |
|
|
} |
994 |
|
|
} |
995 |
|
|
|
996 |
jsr166 |
1.78 |
/** |
997 |
jsr166 |
1.156 |
* A debugging tool to print stack traces of most threads, as jstack does. |
998 |
jsr166 |
1.150 |
* Uninteresting threads are filtered out. |
999 |
jsr166 |
1.95 |
*/ |
1000 |
jsr166 |
1.156 |
static void dumpTestThreads() { |
1001 |
jsr166 |
1.195 |
SecurityManager sm = System.getSecurityManager(); |
1002 |
|
|
if (sm != null) { |
1003 |
|
|
try { |
1004 |
|
|
System.setSecurityManager(null); |
1005 |
|
|
} catch (SecurityException giveUp) { |
1006 |
|
|
return; |
1007 |
|
|
} |
1008 |
|
|
} |
1009 |
|
|
|
1010 |
jsr166 |
1.150 |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1011 |
|
|
System.err.println("------ stacktrace dump start ------"); |
1012 |
|
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1013 |
jsr166 |
1.203 |
final String name = info.getThreadName(); |
1014 |
|
|
String lockName; |
1015 |
jsr166 |
1.150 |
if ("Signal Dispatcher".equals(name)) |
1016 |
|
|
continue; |
1017 |
|
|
if ("Reference Handler".equals(name) |
1018 |
jsr166 |
1.203 |
&& (lockName = info.getLockName()) != null |
1019 |
|
|
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1020 |
jsr166 |
1.150 |
continue; |
1021 |
|
|
if ("Finalizer".equals(name) |
1022 |
jsr166 |
1.203 |
&& (lockName = info.getLockName()) != null |
1023 |
|
|
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1024 |
jsr166 |
1.150 |
continue; |
1025 |
jsr166 |
1.156 |
if ("checkForWedgedTest".equals(name)) |
1026 |
|
|
continue; |
1027 |
jsr166 |
1.96 |
System.err.print(info); |
1028 |
jsr166 |
1.150 |
} |
1029 |
|
|
System.err.println("------ stacktrace dump end ------"); |
1030 |
jsr166 |
1.195 |
|
1031 |
|
|
if (sm != null) System.setSecurityManager(sm); |
1032 |
jsr166 |
1.95 |
} |
1033 |
|
|
|
1034 |
|
|
/** |
1035 |
jsr166 |
1.81 |
* Checks that thread does not terminate within the default |
1036 |
|
|
* millisecond delay of {@code timeoutMillis()}. |
1037 |
|
|
*/ |
1038 |
|
|
void assertThreadStaysAlive(Thread thread) { |
1039 |
|
|
assertThreadStaysAlive(thread, timeoutMillis()); |
1040 |
|
|
} |
1041 |
|
|
|
1042 |
|
|
/** |
1043 |
jsr166 |
1.80 |
* Checks that thread does not terminate within the given millisecond delay. |
1044 |
jsr166 |
1.78 |
*/ |
1045 |
jsr166 |
1.81 |
void assertThreadStaysAlive(Thread thread, long millis) { |
1046 |
jsr166 |
1.78 |
try { |
1047 |
jsr166 |
1.80 |
// No need to optimize the failing case via Thread.join. |
1048 |
|
|
delay(millis); |
1049 |
jsr166 |
1.78 |
assertTrue(thread.isAlive()); |
1050 |
jsr166 |
1.128 |
} catch (InterruptedException fail) { |
1051 |
jsr166 |
1.158 |
threadFail("Unexpected InterruptedException"); |
1052 |
jsr166 |
1.78 |
} |
1053 |
|
|
} |
1054 |
dl |
1.5 |
|
1055 |
|
|
/** |
1056 |
jsr166 |
1.90 |
* Checks that the threads do not terminate within the default |
1057 |
|
|
* millisecond delay of {@code timeoutMillis()}. |
1058 |
|
|
*/ |
1059 |
|
|
void assertThreadsStayAlive(Thread... threads) { |
1060 |
|
|
assertThreadsStayAlive(timeoutMillis(), threads); |
1061 |
|
|
} |
1062 |
|
|
|
1063 |
|
|
/** |
1064 |
|
|
* Checks that the threads do not terminate within the given millisecond delay. |
1065 |
|
|
*/ |
1066 |
|
|
void assertThreadsStayAlive(long millis, Thread... threads) { |
1067 |
|
|
try { |
1068 |
|
|
// No need to optimize the failing case via Thread.join. |
1069 |
|
|
delay(millis); |
1070 |
|
|
for (Thread thread : threads) |
1071 |
|
|
assertTrue(thread.isAlive()); |
1072 |
jsr166 |
1.128 |
} catch (InterruptedException fail) { |
1073 |
jsr166 |
1.158 |
threadFail("Unexpected InterruptedException"); |
1074 |
jsr166 |
1.90 |
} |
1075 |
|
|
} |
1076 |
|
|
|
1077 |
|
|
/** |
1078 |
jsr166 |
1.83 |
* Checks that future.get times out, with the default timeout of |
1079 |
|
|
* {@code timeoutMillis()}. |
1080 |
|
|
*/ |
1081 |
|
|
void assertFutureTimesOut(Future future) { |
1082 |
|
|
assertFutureTimesOut(future, timeoutMillis()); |
1083 |
|
|
} |
1084 |
|
|
|
1085 |
|
|
/** |
1086 |
|
|
* Checks that future.get times out, with the given millisecond timeout. |
1087 |
|
|
*/ |
1088 |
|
|
void assertFutureTimesOut(Future future, long timeoutMillis) { |
1089 |
|
|
long startTime = System.nanoTime(); |
1090 |
|
|
try { |
1091 |
|
|
future.get(timeoutMillis, MILLISECONDS); |
1092 |
|
|
shouldThrow(); |
1093 |
|
|
} catch (TimeoutException success) { |
1094 |
jsr166 |
1.128 |
} catch (Exception fail) { |
1095 |
|
|
threadUnexpectedException(fail); |
1096 |
jsr166 |
1.83 |
} finally { future.cancel(true); } |
1097 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1098 |
|
|
} |
1099 |
|
|
|
1100 |
|
|
/** |
1101 |
jsr166 |
1.53 |
* Fails with message "should throw exception". |
1102 |
jsr166 |
1.27 |
*/ |
1103 |
dl |
1.3 |
public void shouldThrow() { |
1104 |
|
|
fail("Should throw exception"); |
1105 |
|
|
} |
1106 |
|
|
|
1107 |
dl |
1.5 |
/** |
1108 |
jsr166 |
1.53 |
* Fails with message "should throw " + exceptionName. |
1109 |
jsr166 |
1.40 |
*/ |
1110 |
|
|
public void shouldThrow(String exceptionName) { |
1111 |
|
|
fail("Should throw " + exceptionName); |
1112 |
|
|
} |
1113 |
|
|
|
1114 |
|
|
/** |
1115 |
dl |
1.1 |
* The number of elements to place in collections, arrays, etc. |
1116 |
|
|
*/ |
1117 |
jsr166 |
1.45 |
public static final int SIZE = 20; |
1118 |
dl |
1.1 |
|
1119 |
|
|
// Some convenient Integer constants |
1120 |
|
|
|
1121 |
jsr166 |
1.47 |
public static final Integer zero = new Integer(0); |
1122 |
|
|
public static final Integer one = new Integer(1); |
1123 |
|
|
public static final Integer two = new Integer(2); |
1124 |
|
|
public static final Integer three = new Integer(3); |
1125 |
jsr166 |
1.45 |
public static final Integer four = new Integer(4); |
1126 |
|
|
public static final Integer five = new Integer(5); |
1127 |
jsr166 |
1.47 |
public static final Integer six = new Integer(6); |
1128 |
jsr166 |
1.45 |
public static final Integer seven = new Integer(7); |
1129 |
|
|
public static final Integer eight = new Integer(8); |
1130 |
jsr166 |
1.47 |
public static final Integer nine = new Integer(9); |
1131 |
jsr166 |
1.45 |
public static final Integer m1 = new Integer(-1); |
1132 |
|
|
public static final Integer m2 = new Integer(-2); |
1133 |
|
|
public static final Integer m3 = new Integer(-3); |
1134 |
jsr166 |
1.47 |
public static final Integer m4 = new Integer(-4); |
1135 |
|
|
public static final Integer m5 = new Integer(-5); |
1136 |
|
|
public static final Integer m6 = new Integer(-6); |
1137 |
jsr166 |
1.45 |
public static final Integer m10 = new Integer(-10); |
1138 |
dl |
1.7 |
|
1139 |
|
|
/** |
1140 |
jsr166 |
1.49 |
* Runs Runnable r with a security policy that permits precisely |
1141 |
|
|
* the specified permissions. If there is no current security |
1142 |
|
|
* manager, the runnable is run twice, both with and without a |
1143 |
|
|
* security manager. We require that any security manager permit |
1144 |
|
|
* getPolicy/setPolicy. |
1145 |
|
|
*/ |
1146 |
|
|
public void runWithPermissions(Runnable r, Permission... permissions) { |
1147 |
|
|
SecurityManager sm = System.getSecurityManager(); |
1148 |
|
|
if (sm == null) { |
1149 |
|
|
r.run(); |
1150 |
jsr166 |
1.93 |
} |
1151 |
|
|
runWithSecurityManagerWithPermissions(r, permissions); |
1152 |
|
|
} |
1153 |
|
|
|
1154 |
|
|
/** |
1155 |
|
|
* Runs Runnable r with a security policy that permits precisely |
1156 |
|
|
* the specified permissions. If there is no current security |
1157 |
|
|
* manager, a temporary one is set for the duration of the |
1158 |
|
|
* Runnable. We require that any security manager permit |
1159 |
|
|
* getPolicy/setPolicy. |
1160 |
|
|
*/ |
1161 |
|
|
public void runWithSecurityManagerWithPermissions(Runnable r, |
1162 |
|
|
Permission... permissions) { |
1163 |
|
|
SecurityManager sm = System.getSecurityManager(); |
1164 |
|
|
if (sm == null) { |
1165 |
jsr166 |
1.49 |
Policy savedPolicy = Policy.getPolicy(); |
1166 |
|
|
try { |
1167 |
|
|
Policy.setPolicy(permissivePolicy()); |
1168 |
|
|
System.setSecurityManager(new SecurityManager()); |
1169 |
jsr166 |
1.93 |
runWithSecurityManagerWithPermissions(r, permissions); |
1170 |
jsr166 |
1.49 |
} finally { |
1171 |
|
|
System.setSecurityManager(null); |
1172 |
|
|
Policy.setPolicy(savedPolicy); |
1173 |
|
|
} |
1174 |
|
|
} else { |
1175 |
|
|
Policy savedPolicy = Policy.getPolicy(); |
1176 |
|
|
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
1177 |
|
|
Policy.setPolicy(policy); |
1178 |
|
|
|
1179 |
|
|
try { |
1180 |
|
|
r.run(); |
1181 |
|
|
} finally { |
1182 |
|
|
policy.addPermission(new SecurityPermission("setPolicy")); |
1183 |
|
|
Policy.setPolicy(savedPolicy); |
1184 |
|
|
} |
1185 |
|
|
} |
1186 |
|
|
} |
1187 |
|
|
|
1188 |
|
|
/** |
1189 |
|
|
* Runs a runnable without any permissions. |
1190 |
|
|
*/ |
1191 |
|
|
public void runWithoutPermissions(Runnable r) { |
1192 |
|
|
runWithPermissions(r); |
1193 |
|
|
} |
1194 |
|
|
|
1195 |
|
|
/** |
1196 |
dl |
1.7 |
* A security policy where new permissions can be dynamically added |
1197 |
|
|
* or all cleared. |
1198 |
|
|
*/ |
1199 |
jsr166 |
1.45 |
public static class AdjustablePolicy extends java.security.Policy { |
1200 |
dl |
1.7 |
Permissions perms = new Permissions(); |
1201 |
jsr166 |
1.49 |
AdjustablePolicy(Permission... permissions) { |
1202 |
|
|
for (Permission permission : permissions) |
1203 |
|
|
perms.add(permission); |
1204 |
|
|
} |
1205 |
dl |
1.7 |
void addPermission(Permission perm) { perms.add(perm); } |
1206 |
|
|
void clearPermissions() { perms = new Permissions(); } |
1207 |
jsr166 |
1.42 |
public PermissionCollection getPermissions(CodeSource cs) { |
1208 |
|
|
return perms; |
1209 |
|
|
} |
1210 |
|
|
public PermissionCollection getPermissions(ProtectionDomain pd) { |
1211 |
|
|
return perms; |
1212 |
|
|
} |
1213 |
|
|
public boolean implies(ProtectionDomain pd, Permission p) { |
1214 |
|
|
return perms.implies(p); |
1215 |
|
|
} |
1216 |
|
|
public void refresh() {} |
1217 |
jsr166 |
1.93 |
public String toString() { |
1218 |
|
|
List<Permission> ps = new ArrayList<Permission>(); |
1219 |
|
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1220 |
|
|
ps.add(e.nextElement()); |
1221 |
|
|
return "AdjustablePolicy with permissions " + ps; |
1222 |
|
|
} |
1223 |
dl |
1.7 |
} |
1224 |
dl |
1.1 |
|
1225 |
jsr166 |
1.38 |
/** |
1226 |
jsr166 |
1.49 |
* Returns a policy containing all the permissions we ever need. |
1227 |
|
|
*/ |
1228 |
|
|
public static Policy permissivePolicy() { |
1229 |
|
|
return new AdjustablePolicy |
1230 |
|
|
// Permissions j.u.c. needs directly |
1231 |
|
|
(new RuntimePermission("modifyThread"), |
1232 |
|
|
new RuntimePermission("getClassLoader"), |
1233 |
|
|
new RuntimePermission("setContextClassLoader"), |
1234 |
|
|
// Permissions needed to change permissions! |
1235 |
|
|
new SecurityPermission("getPolicy"), |
1236 |
|
|
new SecurityPermission("setPolicy"), |
1237 |
|
|
new RuntimePermission("setSecurityManager"), |
1238 |
|
|
// Permissions needed by the junit test harness |
1239 |
|
|
new RuntimePermission("accessDeclaredMembers"), |
1240 |
|
|
new PropertyPermission("*", "read"), |
1241 |
|
|
new java.io.FilePermission("<<ALL FILES>>", "read")); |
1242 |
|
|
} |
1243 |
|
|
|
1244 |
|
|
/** |
1245 |
jsr166 |
1.60 |
* Sleeps until the given time has elapsed. |
1246 |
|
|
* Throws AssertionFailedError if interrupted. |
1247 |
|
|
*/ |
1248 |
jsr166 |
1.201 |
static void sleep(long millis) { |
1249 |
jsr166 |
1.60 |
try { |
1250 |
dl |
1.76 |
delay(millis); |
1251 |
jsr166 |
1.128 |
} catch (InterruptedException fail) { |
1252 |
jsr166 |
1.60 |
AssertionFailedError afe = |
1253 |
|
|
new AssertionFailedError("Unexpected InterruptedException"); |
1254 |
jsr166 |
1.128 |
afe.initCause(fail); |
1255 |
jsr166 |
1.60 |
throw afe; |
1256 |
|
|
} |
1257 |
|
|
} |
1258 |
|
|
|
1259 |
|
|
/** |
1260 |
jsr166 |
1.88 |
* Spin-waits up to the specified number of milliseconds for the given |
1261 |
jsr166 |
1.65 |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1262 |
|
|
*/ |
1263 |
|
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1264 |
jsr166 |
1.199 |
long startTime = 0L; |
1265 |
jsr166 |
1.65 |
for (;;) { |
1266 |
|
|
Thread.State s = thread.getState(); |
1267 |
|
|
if (s == Thread.State.BLOCKED || |
1268 |
|
|
s == Thread.State.WAITING || |
1269 |
jsr166 |
1.67 |
s == Thread.State.TIMED_WAITING) |
1270 |
jsr166 |
1.65 |
return; |
1271 |
jsr166 |
1.67 |
else if (s == Thread.State.TERMINATED) |
1272 |
|
|
fail("Unexpected thread termination"); |
1273 |
jsr166 |
1.199 |
else if (startTime == 0L) |
1274 |
|
|
startTime = System.nanoTime(); |
1275 |
jsr166 |
1.88 |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1276 |
jsr166 |
1.67 |
threadAssertTrue(thread.isAlive()); |
1277 |
|
|
return; |
1278 |
|
|
} |
1279 |
jsr166 |
1.65 |
Thread.yield(); |
1280 |
|
|
} |
1281 |
|
|
} |
1282 |
|
|
|
1283 |
|
|
/** |
1284 |
jsr166 |
1.75 |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1285 |
|
|
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1286 |
|
|
*/ |
1287 |
|
|
void waitForThreadToEnterWaitState(Thread thread) { |
1288 |
|
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1289 |
|
|
} |
1290 |
|
|
|
1291 |
|
|
/** |
1292 |
jsr166 |
1.66 |
* Returns the number of milliseconds since time given by |
1293 |
|
|
* startNanoTime, which must have been previously returned from a |
1294 |
jsr166 |
1.124 |
* call to {@link System#nanoTime()}. |
1295 |
jsr166 |
1.66 |
*/ |
1296 |
jsr166 |
1.117 |
static long millisElapsedSince(long startNanoTime) { |
1297 |
jsr166 |
1.66 |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1298 |
|
|
} |
1299 |
jsr166 |
1.68 |
|
1300 |
jsr166 |
1.120 |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1301 |
|
|
// long startTime = System.nanoTime(); |
1302 |
|
|
// try { |
1303 |
|
|
// r.run(); |
1304 |
|
|
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1305 |
|
|
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1306 |
|
|
// throw new AssertionFailedError("did not return promptly"); |
1307 |
|
|
// } |
1308 |
|
|
|
1309 |
|
|
// void assertTerminatesPromptly(Runnable r) { |
1310 |
|
|
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1311 |
|
|
// } |
1312 |
|
|
|
1313 |
|
|
/** |
1314 |
|
|
* Checks that timed f.get() returns the expected value, and does not |
1315 |
|
|
* wait for the timeout to elapse before returning. |
1316 |
|
|
*/ |
1317 |
|
|
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1318 |
|
|
long startTime = System.nanoTime(); |
1319 |
|
|
try { |
1320 |
|
|
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1321 |
|
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1322 |
|
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1323 |
|
|
throw new AssertionFailedError("timed get did not return promptly"); |
1324 |
|
|
} |
1325 |
|
|
|
1326 |
|
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1327 |
|
|
checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1328 |
|
|
} |
1329 |
|
|
|
1330 |
jsr166 |
1.66 |
/** |
1331 |
jsr166 |
1.58 |
* Returns a new started daemon Thread running the given runnable. |
1332 |
jsr166 |
1.38 |
*/ |
1333 |
|
|
Thread newStartedThread(Runnable runnable) { |
1334 |
|
|
Thread t = new Thread(runnable); |
1335 |
jsr166 |
1.58 |
t.setDaemon(true); |
1336 |
jsr166 |
1.38 |
t.start(); |
1337 |
|
|
return t; |
1338 |
|
|
} |
1339 |
dl |
1.1 |
|
1340 |
jsr166 |
1.59 |
/** |
1341 |
|
|
* Waits for the specified time (in milliseconds) for the thread |
1342 |
|
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1343 |
|
|
* the thread (in the hope that it may terminate later) and fails. |
1344 |
|
|
*/ |
1345 |
|
|
void awaitTermination(Thread t, long timeoutMillis) { |
1346 |
|
|
try { |
1347 |
|
|
t.join(timeoutMillis); |
1348 |
jsr166 |
1.128 |
} catch (InterruptedException fail) { |
1349 |
|
|
threadUnexpectedException(fail); |
1350 |
jsr166 |
1.59 |
} finally { |
1351 |
jsr166 |
1.83 |
if (t.getState() != Thread.State.TERMINATED) { |
1352 |
jsr166 |
1.59 |
t.interrupt(); |
1353 |
jsr166 |
1.177 |
threadFail("timed out waiting for thread to terminate"); |
1354 |
jsr166 |
1.59 |
} |
1355 |
|
|
} |
1356 |
|
|
} |
1357 |
|
|
|
1358 |
jsr166 |
1.75 |
/** |
1359 |
|
|
* Waits for LONG_DELAY_MS milliseconds for the thread to |
1360 |
|
|
* terminate (using {@link Thread#join(long)}), else interrupts |
1361 |
|
|
* the thread (in the hope that it may terminate later) and fails. |
1362 |
|
|
*/ |
1363 |
|
|
void awaitTermination(Thread t) { |
1364 |
|
|
awaitTermination(t, LONG_DELAY_MS); |
1365 |
|
|
} |
1366 |
|
|
|
1367 |
dl |
1.1 |
// Some convenient Runnable classes |
1368 |
|
|
|
1369 |
jsr166 |
1.45 |
public abstract class CheckedRunnable implements Runnable { |
1370 |
|
|
protected abstract void realRun() throws Throwable; |
1371 |
jsr166 |
1.35 |
|
1372 |
|
|
public final void run() { |
1373 |
|
|
try { |
1374 |
|
|
realRun(); |
1375 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1376 |
|
|
threadUnexpectedException(fail); |
1377 |
jsr166 |
1.35 |
} |
1378 |
|
|
} |
1379 |
|
|
} |
1380 |
|
|
|
1381 |
jsr166 |
1.45 |
public abstract class RunnableShouldThrow implements Runnable { |
1382 |
|
|
protected abstract void realRun() throws Throwable; |
1383 |
jsr166 |
1.40 |
|
1384 |
|
|
final Class<?> exceptionClass; |
1385 |
|
|
|
1386 |
|
|
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
1387 |
|
|
this.exceptionClass = exceptionClass; |
1388 |
|
|
} |
1389 |
|
|
|
1390 |
|
|
public final void run() { |
1391 |
|
|
try { |
1392 |
|
|
realRun(); |
1393 |
|
|
threadShouldThrow(exceptionClass.getSimpleName()); |
1394 |
|
|
} catch (Throwable t) { |
1395 |
|
|
if (! exceptionClass.isInstance(t)) |
1396 |
|
|
threadUnexpectedException(t); |
1397 |
|
|
} |
1398 |
|
|
} |
1399 |
|
|
} |
1400 |
|
|
|
1401 |
jsr166 |
1.45 |
public abstract class ThreadShouldThrow extends Thread { |
1402 |
|
|
protected abstract void realRun() throws Throwable; |
1403 |
jsr166 |
1.40 |
|
1404 |
|
|
final Class<?> exceptionClass; |
1405 |
|
|
|
1406 |
|
|
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
1407 |
|
|
this.exceptionClass = exceptionClass; |
1408 |
|
|
} |
1409 |
|
|
|
1410 |
|
|
public final void run() { |
1411 |
|
|
try { |
1412 |
|
|
realRun(); |
1413 |
|
|
threadShouldThrow(exceptionClass.getSimpleName()); |
1414 |
|
|
} catch (Throwable t) { |
1415 |
|
|
if (! exceptionClass.isInstance(t)) |
1416 |
|
|
threadUnexpectedException(t); |
1417 |
|
|
} |
1418 |
|
|
} |
1419 |
|
|
} |
1420 |
|
|
|
1421 |
jsr166 |
1.45 |
public abstract class CheckedInterruptedRunnable implements Runnable { |
1422 |
|
|
protected abstract void realRun() throws Throwable; |
1423 |
jsr166 |
1.35 |
|
1424 |
|
|
public final void run() { |
1425 |
|
|
try { |
1426 |
|
|
realRun(); |
1427 |
jsr166 |
1.40 |
threadShouldThrow("InterruptedException"); |
1428 |
jsr166 |
1.35 |
} catch (InterruptedException success) { |
1429 |
jsr166 |
1.81 |
threadAssertFalse(Thread.interrupted()); |
1430 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1431 |
|
|
threadUnexpectedException(fail); |
1432 |
jsr166 |
1.35 |
} |
1433 |
|
|
} |
1434 |
|
|
} |
1435 |
|
|
|
1436 |
jsr166 |
1.45 |
public abstract class CheckedCallable<T> implements Callable<T> { |
1437 |
|
|
protected abstract T realCall() throws Throwable; |
1438 |
jsr166 |
1.35 |
|
1439 |
|
|
public final T call() { |
1440 |
|
|
try { |
1441 |
|
|
return realCall(); |
1442 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1443 |
|
|
threadUnexpectedException(fail); |
1444 |
jsr166 |
1.53 |
return null; |
1445 |
jsr166 |
1.35 |
} |
1446 |
jsr166 |
1.40 |
} |
1447 |
|
|
} |
1448 |
|
|
|
1449 |
jsr166 |
1.53 |
public abstract class CheckedInterruptedCallable<T> |
1450 |
|
|
implements Callable<T> { |
1451 |
jsr166 |
1.45 |
protected abstract T realCall() throws Throwable; |
1452 |
jsr166 |
1.40 |
|
1453 |
|
|
public final T call() { |
1454 |
|
|
try { |
1455 |
|
|
T result = realCall(); |
1456 |
|
|
threadShouldThrow("InterruptedException"); |
1457 |
|
|
return result; |
1458 |
|
|
} catch (InterruptedException success) { |
1459 |
jsr166 |
1.81 |
threadAssertFalse(Thread.interrupted()); |
1460 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1461 |
|
|
threadUnexpectedException(fail); |
1462 |
jsr166 |
1.40 |
} |
1463 |
|
|
return null; |
1464 |
jsr166 |
1.35 |
} |
1465 |
|
|
} |
1466 |
|
|
|
1467 |
jsr166 |
1.45 |
public static class NoOpRunnable implements Runnable { |
1468 |
dl |
1.1 |
public void run() {} |
1469 |
|
|
} |
1470 |
|
|
|
1471 |
jsr166 |
1.45 |
public static class NoOpCallable implements Callable { |
1472 |
dl |
1.1 |
public Object call() { return Boolean.TRUE; } |
1473 |
dl |
1.10 |
} |
1474 |
|
|
|
1475 |
jsr166 |
1.45 |
public static final String TEST_STRING = "a test string"; |
1476 |
dl |
1.10 |
|
1477 |
jsr166 |
1.45 |
public static class StringTask implements Callable<String> { |
1478 |
jsr166 |
1.144 |
final String value; |
1479 |
|
|
public StringTask() { this(TEST_STRING); } |
1480 |
|
|
public StringTask(String value) { this.value = value; } |
1481 |
|
|
public String call() { return value; } |
1482 |
dl |
1.10 |
} |
1483 |
|
|
|
1484 |
jsr166 |
1.48 |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1485 |
|
|
return new CheckedCallable<String>() { |
1486 |
jsr166 |
1.64 |
protected String realCall() { |
1487 |
jsr166 |
1.48 |
try { |
1488 |
|
|
latch.await(); |
1489 |
|
|
} catch (InterruptedException quittingTime) {} |
1490 |
|
|
return TEST_STRING; |
1491 |
|
|
}}; |
1492 |
|
|
} |
1493 |
|
|
|
1494 |
jsr166 |
1.148 |
public Runnable countDowner(final CountDownLatch latch) { |
1495 |
|
|
return new CheckedRunnable() { |
1496 |
|
|
public void realRun() throws InterruptedException { |
1497 |
|
|
latch.countDown(); |
1498 |
|
|
}}; |
1499 |
|
|
} |
1500 |
|
|
|
1501 |
jsr166 |
1.161 |
class LatchAwaiter extends CheckedRunnable { |
1502 |
jsr166 |
1.162 |
static final int NEW = 0; |
1503 |
|
|
static final int RUNNING = 1; |
1504 |
|
|
static final int DONE = 2; |
1505 |
jsr166 |
1.161 |
final CountDownLatch latch; |
1506 |
|
|
int state = NEW; |
1507 |
|
|
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1508 |
|
|
public void realRun() throws InterruptedException { |
1509 |
|
|
state = 1; |
1510 |
|
|
await(latch); |
1511 |
|
|
state = 2; |
1512 |
|
|
} |
1513 |
|
|
} |
1514 |
jsr166 |
1.162 |
|
1515 |
jsr166 |
1.161 |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1516 |
|
|
return new LatchAwaiter(latch); |
1517 |
jsr166 |
1.73 |
} |
1518 |
|
|
|
1519 |
jsr166 |
1.188 |
public void await(CountDownLatch latch, long timeoutMillis) { |
1520 |
jsr166 |
1.79 |
try { |
1521 |
jsr166 |
1.188 |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1522 |
jsr166 |
1.176 |
fail("timed out waiting for CountDownLatch for " |
1523 |
jsr166 |
1.188 |
+ (timeoutMillis/1000) + " sec"); |
1524 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1525 |
|
|
threadUnexpectedException(fail); |
1526 |
jsr166 |
1.79 |
} |
1527 |
|
|
} |
1528 |
|
|
|
1529 |
jsr166 |
1.188 |
public void await(CountDownLatch latch) { |
1530 |
|
|
await(latch, LONG_DELAY_MS); |
1531 |
|
|
} |
1532 |
|
|
|
1533 |
jsr166 |
1.89 |
public void await(Semaphore semaphore) { |
1534 |
|
|
try { |
1535 |
jsr166 |
1.176 |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1536 |
|
|
fail("timed out waiting for Semaphore for " |
1537 |
|
|
+ (LONG_DELAY_MS/1000) + " sec"); |
1538 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1539 |
|
|
threadUnexpectedException(fail); |
1540 |
jsr166 |
1.89 |
} |
1541 |
|
|
} |
1542 |
|
|
|
1543 |
jsr166 |
1.81 |
// /** |
1544 |
|
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1545 |
|
|
// */ |
1546 |
|
|
// public void await(AtomicBoolean flag) { |
1547 |
|
|
// await(flag, LONG_DELAY_MS); |
1548 |
|
|
// } |
1549 |
|
|
|
1550 |
|
|
// /** |
1551 |
|
|
// * Spin-waits up to the specified timeout until flag becomes true. |
1552 |
|
|
// */ |
1553 |
|
|
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1554 |
|
|
// long startTime = System.nanoTime(); |
1555 |
|
|
// while (!flag.get()) { |
1556 |
|
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1557 |
|
|
// throw new AssertionFailedError("timed out"); |
1558 |
|
|
// Thread.yield(); |
1559 |
|
|
// } |
1560 |
|
|
// } |
1561 |
|
|
|
1562 |
jsr166 |
1.45 |
public static class NPETask implements Callable<String> { |
1563 |
dl |
1.10 |
public String call() { throw new NullPointerException(); } |
1564 |
|
|
} |
1565 |
|
|
|
1566 |
jsr166 |
1.45 |
public static class CallableOne implements Callable<Integer> { |
1567 |
dl |
1.10 |
public Integer call() { return one; } |
1568 |
dl |
1.1 |
} |
1569 |
|
|
|
1570 |
jsr166 |
1.45 |
public class ShortRunnable extends CheckedRunnable { |
1571 |
|
|
protected void realRun() throws Throwable { |
1572 |
dl |
1.76 |
delay(SHORT_DELAY_MS); |
1573 |
dl |
1.1 |
} |
1574 |
|
|
} |
1575 |
|
|
|
1576 |
jsr166 |
1.45 |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1577 |
|
|
protected void realRun() throws InterruptedException { |
1578 |
dl |
1.76 |
delay(SHORT_DELAY_MS); |
1579 |
dl |
1.1 |
} |
1580 |
|
|
} |
1581 |
|
|
|
1582 |
jsr166 |
1.45 |
public class SmallRunnable extends CheckedRunnable { |
1583 |
|
|
protected void realRun() throws Throwable { |
1584 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1585 |
dl |
1.1 |
} |
1586 |
|
|
} |
1587 |
|
|
|
1588 |
jsr166 |
1.45 |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1589 |
|
|
protected void realRun() { |
1590 |
dl |
1.6 |
try { |
1591 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1592 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1593 |
dl |
1.6 |
} |
1594 |
|
|
} |
1595 |
|
|
|
1596 |
jsr166 |
1.45 |
public class SmallCallable extends CheckedCallable { |
1597 |
|
|
protected Object realCall() throws InterruptedException { |
1598 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1599 |
dl |
1.1 |
return Boolean.TRUE; |
1600 |
|
|
} |
1601 |
|
|
} |
1602 |
|
|
|
1603 |
jsr166 |
1.45 |
public class MediumRunnable extends CheckedRunnable { |
1604 |
|
|
protected void realRun() throws Throwable { |
1605 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1606 |
dl |
1.1 |
} |
1607 |
|
|
} |
1608 |
|
|
|
1609 |
jsr166 |
1.45 |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1610 |
|
|
protected void realRun() throws InterruptedException { |
1611 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1612 |
dl |
1.1 |
} |
1613 |
|
|
} |
1614 |
|
|
|
1615 |
jsr166 |
1.63 |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1616 |
|
|
return new CheckedRunnable() { |
1617 |
|
|
protected void realRun() { |
1618 |
|
|
try { |
1619 |
dl |
1.76 |
delay(timeoutMillis); |
1620 |
jsr166 |
1.63 |
} catch (InterruptedException ok) {} |
1621 |
|
|
}}; |
1622 |
|
|
} |
1623 |
|
|
|
1624 |
jsr166 |
1.45 |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1625 |
|
|
protected void realRun() { |
1626 |
dl |
1.1 |
try { |
1627 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1628 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1629 |
dl |
1.1 |
} |
1630 |
|
|
} |
1631 |
dl |
1.5 |
|
1632 |
jsr166 |
1.45 |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1633 |
|
|
protected void realRun() { |
1634 |
dl |
1.12 |
try { |
1635 |
dl |
1.76 |
delay(LONG_DELAY_MS); |
1636 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1637 |
dl |
1.12 |
} |
1638 |
|
|
} |
1639 |
|
|
|
1640 |
dl |
1.5 |
/** |
1641 |
|
|
* For use as ThreadFactory in constructors |
1642 |
|
|
*/ |
1643 |
jsr166 |
1.45 |
public static class SimpleThreadFactory implements ThreadFactory { |
1644 |
jsr166 |
1.33 |
public Thread newThread(Runnable r) { |
1645 |
dl |
1.5 |
return new Thread(r); |
1646 |
jsr166 |
1.27 |
} |
1647 |
dl |
1.5 |
} |
1648 |
|
|
|
1649 |
jsr166 |
1.61 |
public interface TrackedRunnable extends Runnable { |
1650 |
|
|
boolean isDone(); |
1651 |
|
|
} |
1652 |
|
|
|
1653 |
|
|
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1654 |
|
|
return new TrackedRunnable() { |
1655 |
|
|
private volatile boolean done = false; |
1656 |
|
|
public boolean isDone() { return done; } |
1657 |
|
|
public void run() { |
1658 |
|
|
try { |
1659 |
dl |
1.76 |
delay(timeoutMillis); |
1660 |
jsr166 |
1.61 |
done = true; |
1661 |
|
|
} catch (InterruptedException ok) {} |
1662 |
|
|
} |
1663 |
|
|
}; |
1664 |
|
|
} |
1665 |
|
|
|
1666 |
jsr166 |
1.45 |
public static class TrackedShortRunnable implements Runnable { |
1667 |
|
|
public volatile boolean done = false; |
1668 |
dl |
1.5 |
public void run() { |
1669 |
|
|
try { |
1670 |
dl |
1.76 |
delay(SHORT_DELAY_MS); |
1671 |
jsr166 |
1.61 |
done = true; |
1672 |
|
|
} catch (InterruptedException ok) {} |
1673 |
|
|
} |
1674 |
|
|
} |
1675 |
|
|
|
1676 |
|
|
public static class TrackedSmallRunnable implements Runnable { |
1677 |
|
|
public volatile boolean done = false; |
1678 |
|
|
public void run() { |
1679 |
|
|
try { |
1680 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1681 |
dl |
1.5 |
done = true; |
1682 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1683 |
dl |
1.6 |
} |
1684 |
|
|
} |
1685 |
|
|
|
1686 |
jsr166 |
1.45 |
public static class TrackedMediumRunnable implements Runnable { |
1687 |
|
|
public volatile boolean done = false; |
1688 |
dl |
1.6 |
public void run() { |
1689 |
|
|
try { |
1690 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1691 |
dl |
1.6 |
done = true; |
1692 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1693 |
dl |
1.6 |
} |
1694 |
|
|
} |
1695 |
|
|
|
1696 |
jsr166 |
1.45 |
public static class TrackedLongRunnable implements Runnable { |
1697 |
|
|
public volatile boolean done = false; |
1698 |
dl |
1.6 |
public void run() { |
1699 |
|
|
try { |
1700 |
dl |
1.76 |
delay(LONG_DELAY_MS); |
1701 |
dl |
1.6 |
done = true; |
1702 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1703 |
dl |
1.6 |
} |
1704 |
|
|
} |
1705 |
|
|
|
1706 |
jsr166 |
1.45 |
public static class TrackedNoOpRunnable implements Runnable { |
1707 |
|
|
public volatile boolean done = false; |
1708 |
dl |
1.6 |
public void run() { |
1709 |
|
|
done = true; |
1710 |
dl |
1.5 |
} |
1711 |
|
|
} |
1712 |
|
|
|
1713 |
jsr166 |
1.45 |
public static class TrackedCallable implements Callable { |
1714 |
|
|
public volatile boolean done = false; |
1715 |
dl |
1.5 |
public Object call() { |
1716 |
|
|
try { |
1717 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1718 |
dl |
1.5 |
done = true; |
1719 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1720 |
dl |
1.5 |
return Boolean.TRUE; |
1721 |
|
|
} |
1722 |
|
|
} |
1723 |
dl |
1.14 |
|
1724 |
jsr166 |
1.53 |
/** |
1725 |
|
|
* Analog of CheckedRunnable for RecursiveAction |
1726 |
|
|
*/ |
1727 |
|
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1728 |
|
|
protected abstract void realCompute() throws Throwable; |
1729 |
|
|
|
1730 |
jsr166 |
1.108 |
@Override protected final void compute() { |
1731 |
jsr166 |
1.53 |
try { |
1732 |
|
|
realCompute(); |
1733 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1734 |
|
|
threadUnexpectedException(fail); |
1735 |
jsr166 |
1.53 |
} |
1736 |
|
|
} |
1737 |
|
|
} |
1738 |
|
|
|
1739 |
|
|
/** |
1740 |
|
|
* Analog of CheckedCallable for RecursiveTask |
1741 |
|
|
*/ |
1742 |
|
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1743 |
|
|
protected abstract T realCompute() throws Throwable; |
1744 |
|
|
|
1745 |
jsr166 |
1.108 |
@Override protected final T compute() { |
1746 |
jsr166 |
1.53 |
try { |
1747 |
|
|
return realCompute(); |
1748 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1749 |
|
|
threadUnexpectedException(fail); |
1750 |
jsr166 |
1.53 |
return null; |
1751 |
|
|
} |
1752 |
|
|
} |
1753 |
|
|
} |
1754 |
dl |
1.5 |
|
1755 |
|
|
/** |
1756 |
|
|
* For use as RejectedExecutionHandler in constructors |
1757 |
|
|
*/ |
1758 |
jsr166 |
1.45 |
public static class NoOpREHandler implements RejectedExecutionHandler { |
1759 |
jsr166 |
1.35 |
public void rejectedExecution(Runnable r, |
1760 |
|
|
ThreadPoolExecutor executor) {} |
1761 |
dl |
1.5 |
} |
1762 |
jsr166 |
1.27 |
|
1763 |
jsr166 |
1.60 |
/** |
1764 |
jsr166 |
1.86 |
* A CyclicBarrier that uses timed await and fails with |
1765 |
|
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1766 |
jsr166 |
1.60 |
*/ |
1767 |
jsr166 |
1.202 |
public static class CheckedBarrier extends CyclicBarrier { |
1768 |
jsr166 |
1.60 |
public CheckedBarrier(int parties) { super(parties); } |
1769 |
|
|
|
1770 |
|
|
public int await() { |
1771 |
|
|
try { |
1772 |
jsr166 |
1.86 |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1773 |
jsr166 |
1.128 |
} catch (TimeoutException timedOut) { |
1774 |
jsr166 |
1.86 |
throw new AssertionFailedError("timed out"); |
1775 |
jsr166 |
1.128 |
} catch (Exception fail) { |
1776 |
jsr166 |
1.60 |
AssertionFailedError afe = |
1777 |
jsr166 |
1.128 |
new AssertionFailedError("Unexpected exception: " + fail); |
1778 |
|
|
afe.initCause(fail); |
1779 |
jsr166 |
1.60 |
throw afe; |
1780 |
|
|
} |
1781 |
|
|
} |
1782 |
|
|
} |
1783 |
|
|
|
1784 |
jsr166 |
1.81 |
void checkEmpty(BlockingQueue q) { |
1785 |
jsr166 |
1.72 |
try { |
1786 |
|
|
assertTrue(q.isEmpty()); |
1787 |
|
|
assertEquals(0, q.size()); |
1788 |
|
|
assertNull(q.peek()); |
1789 |
|
|
assertNull(q.poll()); |
1790 |
|
|
assertNull(q.poll(0, MILLISECONDS)); |
1791 |
|
|
assertEquals(q.toString(), "[]"); |
1792 |
|
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1793 |
|
|
assertFalse(q.iterator().hasNext()); |
1794 |
|
|
try { |
1795 |
|
|
q.element(); |
1796 |
|
|
shouldThrow(); |
1797 |
|
|
} catch (NoSuchElementException success) {} |
1798 |
|
|
try { |
1799 |
|
|
q.iterator().next(); |
1800 |
|
|
shouldThrow(); |
1801 |
|
|
} catch (NoSuchElementException success) {} |
1802 |
|
|
try { |
1803 |
|
|
q.remove(); |
1804 |
|
|
shouldThrow(); |
1805 |
|
|
} catch (NoSuchElementException success) {} |
1806 |
jsr166 |
1.128 |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
1807 |
jsr166 |
1.72 |
} |
1808 |
|
|
|
1809 |
jsr166 |
1.91 |
void assertSerialEquals(Object x, Object y) { |
1810 |
|
|
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1811 |
|
|
} |
1812 |
|
|
|
1813 |
|
|
void assertNotSerialEquals(Object x, Object y) { |
1814 |
|
|
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1815 |
|
|
} |
1816 |
|
|
|
1817 |
|
|
byte[] serialBytes(Object o) { |
1818 |
jsr166 |
1.79 |
try { |
1819 |
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1820 |
|
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1821 |
|
|
oos.writeObject(o); |
1822 |
|
|
oos.flush(); |
1823 |
|
|
oos.close(); |
1824 |
jsr166 |
1.91 |
return bos.toByteArray(); |
1825 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1826 |
|
|
threadUnexpectedException(fail); |
1827 |
jsr166 |
1.91 |
return new byte[0]; |
1828 |
|
|
} |
1829 |
|
|
} |
1830 |
|
|
|
1831 |
jsr166 |
1.208 |
void assertImmutable(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 |
jsr166 |
1.209 |
|
1840 |
jsr166 |
1.91 |
@SuppressWarnings("unchecked") |
1841 |
|
|
<T> T serialClone(T o) { |
1842 |
|
|
try { |
1843 |
jsr166 |
1.87 |
ObjectInputStream ois = new ObjectInputStream |
1844 |
jsr166 |
1.91 |
(new ByteArrayInputStream(serialBytes(o))); |
1845 |
jsr166 |
1.87 |
T clone = (T) ois.readObject(); |
1846 |
jsr166 |
1.208 |
if (o == clone) assertImmutable(o); |
1847 |
jsr166 |
1.87 |
assertSame(o.getClass(), clone.getClass()); |
1848 |
|
|
return clone; |
1849 |
jsr166 |
1.128 |
} catch (Throwable fail) { |
1850 |
|
|
threadUnexpectedException(fail); |
1851 |
jsr166 |
1.79 |
return null; |
1852 |
|
|
} |
1853 |
|
|
} |
1854 |
jsr166 |
1.106 |
|
1855 |
jsr166 |
1.208 |
/** |
1856 |
|
|
* If o implements Cloneable and has a public clone method, |
1857 |
|
|
* returns a clone of o, else null. |
1858 |
|
|
*/ |
1859 |
|
|
@SuppressWarnings("unchecked") |
1860 |
|
|
<T> T cloneableClone(T o) { |
1861 |
|
|
if (!(o instanceof Cloneable)) return null; |
1862 |
|
|
final T clone; |
1863 |
|
|
try { |
1864 |
|
|
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1865 |
|
|
} catch (NoSuchMethodException ok) { |
1866 |
|
|
return null; |
1867 |
|
|
} catch (ReflectiveOperationException unexpected) { |
1868 |
|
|
throw new Error(unexpected); |
1869 |
|
|
} |
1870 |
|
|
assertNotSame(o, clone); // not 100% guaranteed by spec |
1871 |
|
|
assertSame(o.getClass(), clone.getClass()); |
1872 |
|
|
return clone; |
1873 |
|
|
} |
1874 |
|
|
|
1875 |
jsr166 |
1.106 |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1876 |
|
|
Runnable... throwingActions) { |
1877 |
|
|
for (Runnable throwingAction : throwingActions) { |
1878 |
|
|
boolean threw = false; |
1879 |
|
|
try { throwingAction.run(); } |
1880 |
|
|
catch (Throwable t) { |
1881 |
|
|
threw = true; |
1882 |
|
|
if (!expectedExceptionClass.isInstance(t)) { |
1883 |
|
|
AssertionFailedError afe = |
1884 |
|
|
new AssertionFailedError |
1885 |
|
|
("Expected " + expectedExceptionClass.getName() + |
1886 |
|
|
", got " + t.getClass().getName()); |
1887 |
|
|
afe.initCause(t); |
1888 |
|
|
threadUnexpectedException(afe); |
1889 |
|
|
} |
1890 |
|
|
} |
1891 |
|
|
if (!threw) |
1892 |
|
|
shouldThrow(expectedExceptionClass.getName()); |
1893 |
|
|
} |
1894 |
|
|
} |
1895 |
jsr166 |
1.126 |
|
1896 |
|
|
public void assertIteratorExhausted(Iterator<?> it) { |
1897 |
|
|
try { |
1898 |
|
|
it.next(); |
1899 |
|
|
shouldThrow(); |
1900 |
|
|
} catch (NoSuchElementException success) {} |
1901 |
|
|
assertFalse(it.hasNext()); |
1902 |
jsr166 |
1.127 |
} |
1903 |
jsr166 |
1.191 |
|
1904 |
|
|
public <T> Callable<T> callableThrowing(final Exception ex) { |
1905 |
|
|
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1906 |
|
|
} |
1907 |
|
|
|
1908 |
|
|
public Runnable runnableThrowing(final RuntimeException ex) { |
1909 |
|
|
return new Runnable() { public void run() { throw ex; }}; |
1910 |
|
|
} |
1911 |
jsr166 |
1.193 |
|
1912 |
|
|
/** A reusable thread pool to be shared by tests. */ |
1913 |
|
|
static final ExecutorService cachedThreadPool = |
1914 |
|
|
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1915 |
|
|
1000L, MILLISECONDS, |
1916 |
|
|
new SynchronousQueue<Runnable>()); |
1917 |
|
|
|
1918 |
jsr166 |
1.200 |
static <T> void shuffle(T[] array) { |
1919 |
|
|
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1920 |
|
|
} |
1921 |
dl |
1.1 |
} |