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