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