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