1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.io.ByteArrayInputStream; |
11 |
> |
import java.io.ByteArrayOutputStream; |
12 |
> |
import java.io.ObjectInputStream; |
13 |
> |
import java.io.ObjectOutputStream; |
14 |
> |
import java.lang.management.ManagementFactory; |
15 |
> |
import java.lang.management.ThreadInfo; |
16 |
> |
import java.lang.reflect.Method; |
17 |
> |
import java.util.ArrayList; |
18 |
> |
import java.util.Arrays; |
19 |
> |
import java.util.Date; |
20 |
> |
import java.util.Enumeration; |
21 |
> |
import java.util.List; |
22 |
> |
import java.util.NoSuchElementException; |
23 |
> |
import java.util.PropertyPermission; |
24 |
|
import java.util.concurrent.*; |
25 |
< |
import java.io.*; |
26 |
< |
import java.security.*; |
25 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
> |
import java.util.concurrent.atomic.AtomicReference; |
27 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
> |
import java.security.CodeSource; |
30 |
> |
import java.security.Permission; |
31 |
> |
import java.security.PermissionCollection; |
32 |
> |
import java.security.Permissions; |
33 |
> |
import java.security.Policy; |
34 |
> |
import java.security.ProtectionDomain; |
35 |
> |
import java.security.SecurityPermission; |
36 |
|
|
37 |
|
/** |
38 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
39 |
|
* utility methods and classes, as well as a simple framework for |
40 |
|
* helping to make sure that assertions failing in generated threads |
41 |
|
* cause the associated test that generated them to itself fail (which |
42 |
< |
* JUnit doe not otherwise arrange). The rules for creating such |
42 |
> |
* JUnit does not otherwise arrange). The rules for creating such |
43 |
|
* tests are: |
44 |
|
* |
45 |
|
* <ol> |
46 |
|
* |
47 |
|
* <li> All assertions in code running in generated threads must use |
48 |
< |
* the forms {@link threadFail} , {@link threadAssertTrue} {@link |
49 |
< |
* threadAssertEquals}, or {@link threadAssertNull}, (not |
50 |
< |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
48 |
> |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
49 |
> |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
50 |
> |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
51 |
|
* particularly recommended) for other code to use these forms too. |
52 |
|
* Only the most typically used JUnit assertion methods are defined |
53 |
|
* this way, but enough to live with.</li> |
54 |
|
* |
55 |
< |
* <li> If you override {@link setUp} or {@link tearDown}, make sure |
56 |
< |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
55 |
> |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
56 |
> |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
57 |
|
* them. These methods are used to clear and check for thread |
58 |
|
* assertion failures.</li> |
59 |
|
* |
60 |
< |
* <li>All delays and timeouts must use one of the constants <tt> |
61 |
< |
* SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>, |
62 |
< |
* <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always |
60 |
> |
* <li>All delays and timeouts must use one of the constants {@code |
61 |
> |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
62 |
> |
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
63 |
|
* discriminable from zero time, and always allows enough time for the |
64 |
|
* small amounts of computation (creating a thread, calling a few |
65 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
66 |
|
* is always discriminable as larger than SHORT and smaller than |
67 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
68 |
|
* but even so, if there is ever any doubt, they can all be increased |
69 |
< |
* in one spot to rerun tests on slower platforms</li> |
69 |
> |
* in one spot to rerun tests on slower platforms.</li> |
70 |
|
* |
71 |
|
* <li> All threads generated must be joined inside each test case |
72 |
< |
* method (or <tt>fail</tt> to do so) before returning from the |
73 |
< |
* method. The <tt> joinPool</tt> method can be used to do this when |
72 |
> |
* method (or {@code fail} to do so) before returning from the |
73 |
> |
* method. The {@code joinPool} method can be used to do this when |
74 |
|
* using Executors.</li> |
75 |
|
* |
76 |
|
* </ol> |
77 |
|
* |
78 |
< |
* <p> <b>Other notes</b> |
78 |
> |
* <p><b>Other notes</b> |
79 |
|
* <ul> |
80 |
|
* |
81 |
|
* <li> Usually, there is one testcase method per JSR166 method |
85 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
86 |
|
* cover multiple methods when they cannot be tested in |
87 |
|
* isolation.</li> |
88 |
< |
* |
88 |
> |
* |
89 |
|
* <li> The documentation style for testcases is to provide as javadoc |
90 |
|
* a simple sentence or two describing the property that the testcase |
91 |
|
* method purports to test. The javadocs do not say anything about how |
102 |
|
* any particular package to simplify things for people integrating |
103 |
|
* them in TCK test suites.</li> |
104 |
|
* |
105 |
< |
* <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase) |
105 |
> |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
106 |
|
* runs all JSR166 unit tests.</li> |
107 |
|
* |
108 |
|
* </ul> |
109 |
|
*/ |
110 |
|
public class JSR166TestCase extends TestCase { |
111 |
+ |
private static final boolean useSecurityManager = |
112 |
+ |
Boolean.getBoolean("jsr166.useSecurityManager"); |
113 |
+ |
|
114 |
+ |
protected static final boolean expensiveTests = |
115 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
116 |
+ |
|
117 |
+ |
/** |
118 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
119 |
+ |
* take more than profileThreshold milliseconds to execute. |
120 |
+ |
*/ |
121 |
+ |
private static final boolean profileTests = |
122 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
123 |
+ |
|
124 |
+ |
/** |
125 |
+ |
* The number of milliseconds that tests are permitted for |
126 |
+ |
* execution without being reported, when profileTests is set. |
127 |
+ |
*/ |
128 |
+ |
private static final long profileThreshold = |
129 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
130 |
+ |
|
131 |
+ |
protected void runTest() throws Throwable { |
132 |
+ |
if (profileTests) |
133 |
+ |
runTestProfiled(); |
134 |
+ |
else |
135 |
+ |
super.runTest(); |
136 |
+ |
} |
137 |
+ |
|
138 |
+ |
protected void runTestProfiled() throws Throwable { |
139 |
+ |
long t0 = System.nanoTime(); |
140 |
+ |
try { |
141 |
+ |
super.runTest(); |
142 |
+ |
} finally { |
143 |
+ |
long elapsedMillis = |
144 |
+ |
(System.nanoTime() - t0) / (1000L * 1000L); |
145 |
+ |
if (elapsedMillis >= profileThreshold) |
146 |
+ |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
147 |
+ |
} |
148 |
+ |
} |
149 |
+ |
|
150 |
|
/** |
151 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
152 |
< |
*/ |
153 |
< |
public static void main (String[] args) { |
154 |
< |
int iters = 1; |
155 |
< |
if (args.length > 0) |
156 |
< |
iters = Integer.parseInt(args[0]); |
151 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
> |
* Optional command line arg provides the number of iterations to |
153 |
> |
* repeat running the tests. |
154 |
> |
*/ |
155 |
> |
public static void main(String[] args) { |
156 |
> |
if (useSecurityManager) { |
157 |
> |
System.err.println("Setting a permissive security manager"); |
158 |
> |
Policy.setPolicy(permissivePolicy()); |
159 |
> |
System.setSecurityManager(new SecurityManager()); |
160 |
> |
} |
161 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
162 |
> |
|
163 |
|
Test s = suite(); |
164 |
< |
for (int i = 0; i < iters; ++i) |
165 |
< |
junit.textui.TestRunner.run (s); |
164 |
> |
for (int i = 0; i < iters; ++i) { |
165 |
> |
junit.textui.TestRunner.run(s); |
166 |
> |
System.gc(); |
167 |
> |
System.runFinalization(); |
168 |
> |
} |
169 |
> |
System.exit(0); |
170 |
> |
} |
171 |
> |
|
172 |
> |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
173 |
> |
TestSuite suite = new TestSuite(); |
174 |
> |
for (Object suiteOrClass : suiteOrClasses) { |
175 |
> |
if (suiteOrClass instanceof TestSuite) |
176 |
> |
suite.addTest((TestSuite) suiteOrClass); |
177 |
> |
else if (suiteOrClass instanceof Class) |
178 |
> |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
179 |
> |
else |
180 |
> |
throw new ClassCastException("not a test suite or class"); |
181 |
> |
} |
182 |
> |
return suite; |
183 |
|
} |
184 |
|
|
185 |
+ |
public static void addNamedTestClasses(TestSuite suite, |
186 |
+ |
String... testClassNames) { |
187 |
+ |
for (String testClassName : testClassNames) { |
188 |
+ |
try { |
189 |
+ |
Class<?> testClass = Class.forName(testClassName); |
190 |
+ |
Method m = testClass.getDeclaredMethod("suite", |
191 |
+ |
new Class<?>[0]); |
192 |
+ |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
+ |
} catch (Exception e) { |
194 |
+ |
throw new Error("Missing test class", e); |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
public static final double JAVA_CLASS_VERSION; |
200 |
+ |
static { |
201 |
+ |
try { |
202 |
+ |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
+ |
new java.security.PrivilegedAction<Double>() { |
204 |
+ |
public Double run() { |
205 |
+ |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
+ |
} catch (Throwable t) { |
207 |
+ |
throw new Error(t); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
+ |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
+ |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
+ |
|
215 |
|
/** |
216 |
< |
* Collects all JSR166 unit tests as one suite |
217 |
< |
*/ |
218 |
< |
public static Test suite ( ) { |
219 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
220 |
< |
|
221 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
222 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
223 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
224 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
225 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
226 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
227 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
228 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
229 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
230 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
231 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
232 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
233 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
234 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
235 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
236 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
237 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
238 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
239 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
240 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
241 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
242 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
243 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
244 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
245 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
246 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
247 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
248 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
249 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
250 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
251 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
252 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
253 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
254 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
255 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
256 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
257 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
258 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
259 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
260 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
261 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
262 |
< |
|
216 |
> |
* Collects all JSR166 unit tests as one suite. |
217 |
> |
*/ |
218 |
> |
public static Test suite() { |
219 |
> |
// Java7+ test classes |
220 |
> |
TestSuite suite = newTestSuite( |
221 |
> |
ForkJoinPoolTest.suite(), |
222 |
> |
ForkJoinTaskTest.suite(), |
223 |
> |
RecursiveActionTest.suite(), |
224 |
> |
RecursiveTaskTest.suite(), |
225 |
> |
LinkedTransferQueueTest.suite(), |
226 |
> |
PhaserTest.suite(), |
227 |
> |
ThreadLocalRandomTest.suite(), |
228 |
> |
AbstractExecutorServiceTest.suite(), |
229 |
> |
AbstractQueueTest.suite(), |
230 |
> |
AbstractQueuedSynchronizerTest.suite(), |
231 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
232 |
> |
ArrayBlockingQueueTest.suite(), |
233 |
> |
ArrayDequeTest.suite(), |
234 |
> |
AtomicBooleanTest.suite(), |
235 |
> |
AtomicIntegerArrayTest.suite(), |
236 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
237 |
> |
AtomicIntegerTest.suite(), |
238 |
> |
AtomicLongArrayTest.suite(), |
239 |
> |
AtomicLongFieldUpdaterTest.suite(), |
240 |
> |
AtomicLongTest.suite(), |
241 |
> |
AtomicMarkableReferenceTest.suite(), |
242 |
> |
AtomicReferenceArrayTest.suite(), |
243 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
244 |
> |
AtomicReferenceTest.suite(), |
245 |
> |
AtomicStampedReferenceTest.suite(), |
246 |
> |
ConcurrentHashMapTest.suite(), |
247 |
> |
ConcurrentLinkedDequeTest.suite(), |
248 |
> |
ConcurrentLinkedQueueTest.suite(), |
249 |
> |
ConcurrentSkipListMapTest.suite(), |
250 |
> |
ConcurrentSkipListSubMapTest.suite(), |
251 |
> |
ConcurrentSkipListSetTest.suite(), |
252 |
> |
ConcurrentSkipListSubSetTest.suite(), |
253 |
> |
CopyOnWriteArrayListTest.suite(), |
254 |
> |
CopyOnWriteArraySetTest.suite(), |
255 |
> |
CountDownLatchTest.suite(), |
256 |
> |
CyclicBarrierTest.suite(), |
257 |
> |
DelayQueueTest.suite(), |
258 |
> |
EntryTest.suite(), |
259 |
> |
ExchangerTest.suite(), |
260 |
> |
ExecutorsTest.suite(), |
261 |
> |
ExecutorCompletionServiceTest.suite(), |
262 |
> |
FutureTaskTest.suite(), |
263 |
> |
LinkedBlockingDequeTest.suite(), |
264 |
> |
LinkedBlockingQueueTest.suite(), |
265 |
> |
LinkedListTest.suite(), |
266 |
> |
LockSupportTest.suite(), |
267 |
> |
PriorityBlockingQueueTest.suite(), |
268 |
> |
PriorityQueueTest.suite(), |
269 |
> |
ReentrantLockTest.suite(), |
270 |
> |
ReentrantReadWriteLockTest.suite(), |
271 |
> |
ScheduledExecutorTest.suite(), |
272 |
> |
ScheduledExecutorSubclassTest.suite(), |
273 |
> |
SemaphoreTest.suite(), |
274 |
> |
SynchronousQueueTest.suite(), |
275 |
> |
SystemTest.suite(), |
276 |
> |
ThreadLocalTest.suite(), |
277 |
> |
ThreadPoolExecutorTest.suite(), |
278 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
279 |
> |
ThreadTest.suite(), |
280 |
> |
TimeUnitTest.suite(), |
281 |
> |
TreeMapTest.suite(), |
282 |
> |
TreeSetTest.suite(), |
283 |
> |
TreeSubMapTest.suite(), |
284 |
> |
TreeSubSetTest.suite()); |
285 |
> |
|
286 |
> |
// Java8+ test classes |
287 |
> |
if (atLeastJava8()) { |
288 |
> |
String[] java8TestClassNames = { |
289 |
> |
"LongAdderTest", |
290 |
> |
"LongAccumulatorTest", |
291 |
> |
"DoubleAdderTest", |
292 |
> |
"DoubleAccumulatorTest", |
293 |
> |
"CompletableFutureTest", |
294 |
> |
"ForkJoinPool8Test", |
295 |
> |
"StampedLockTest", |
296 |
> |
}; |
297 |
> |
addNamedTestClasses(suite, java8TestClassNames); |
298 |
> |
} |
299 |
> |
|
300 |
|
return suite; |
301 |
|
} |
302 |
|
|
308 |
|
|
309 |
|
|
310 |
|
/** |
311 |
< |
* Return the shortest timed delay. This could |
311 |
> |
* Returns the shortest timed delay. This could |
312 |
|
* be reimplemented to use for example a Property. |
313 |
< |
*/ |
313 |
> |
*/ |
314 |
|
protected long getShortDelay() { |
315 |
< |
return 10; |
315 |
> |
return 50; |
316 |
|
} |
317 |
|
|
167 |
– |
|
318 |
|
/** |
319 |
< |
* Set delays as multiples of SHORT_DELAY. |
319 |
> |
* Sets delays as multiples of SHORT_DELAY. |
320 |
|
*/ |
321 |
< |
protected void setDelays() { |
321 |
> |
protected void setDelays() { |
322 |
|
SHORT_DELAY_MS = getShortDelay(); |
323 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
323 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
324 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
325 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
325 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* Flag set true if any threadAssert methods fail |
329 |
> |
* Returns a timeout in milliseconds to be used in tests that |
330 |
> |
* verify that operations block or time out. |
331 |
|
*/ |
332 |
< |
volatile boolean threadFailed; |
332 |
> |
long timeoutMillis() { |
333 |
> |
return SHORT_DELAY_MS / 4; |
334 |
> |
} |
335 |
> |
|
336 |
> |
/** |
337 |
> |
* Returns a new Date instance representing a time delayMillis |
338 |
> |
* milliseconds in the future. |
339 |
> |
*/ |
340 |
> |
Date delayedDate(long delayMillis) { |
341 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
342 |
> |
} |
343 |
|
|
344 |
|
/** |
345 |
< |
* Initialize test to indicate that no thread assertions have failed |
345 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
346 |
|
*/ |
347 |
< |
public void setUp() { |
347 |
> |
private final AtomicReference<Throwable> threadFailure |
348 |
> |
= new AtomicReference<Throwable>(null); |
349 |
> |
|
350 |
> |
/** |
351 |
> |
* Records an exception so that it can be rethrown later in the test |
352 |
> |
* harness thread, triggering a test case failure. Only the first |
353 |
> |
* failure is recorded; subsequent calls to this method from within |
354 |
> |
* the same test have no effect. |
355 |
> |
*/ |
356 |
> |
public void threadRecordFailure(Throwable t) { |
357 |
> |
threadFailure.compareAndSet(null, t); |
358 |
> |
} |
359 |
> |
|
360 |
> |
public void setUp() { |
361 |
|
setDelays(); |
188 |
– |
threadFailed = false; |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* Trigger test case failure if any thread assertions have failed |
365 |
> |
* Extra checks that get done for all test cases. |
366 |
> |
* |
367 |
> |
* Triggers test case failure if any thread assertions have failed, |
368 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
369 |
> |
* earlier by threadRecordFailure. |
370 |
> |
* |
371 |
> |
* Triggers test case failure if interrupt status is set in the main thread. |
372 |
> |
*/ |
373 |
> |
public void tearDown() throws Exception { |
374 |
> |
Throwable t = threadFailure.getAndSet(null); |
375 |
> |
if (t != null) { |
376 |
> |
if (t instanceof Error) |
377 |
> |
throw (Error) t; |
378 |
> |
else if (t instanceof RuntimeException) |
379 |
> |
throw (RuntimeException) t; |
380 |
> |
else if (t instanceof Exception) |
381 |
> |
throw (Exception) t; |
382 |
> |
else { |
383 |
> |
AssertionFailedError afe = |
384 |
> |
new AssertionFailedError(t.toString()); |
385 |
> |
afe.initCause(t); |
386 |
> |
throw afe; |
387 |
> |
} |
388 |
> |
} |
389 |
> |
|
390 |
> |
if (Thread.interrupted()) |
391 |
> |
throw new AssertionFailedError("interrupt status set in main thread"); |
392 |
> |
|
393 |
> |
checkForkJoinPoolThreadLeaks(); |
394 |
> |
} |
395 |
> |
|
396 |
> |
/** |
397 |
> |
* Find missing try { ... } finally { joinPool(e); } |
398 |
|
*/ |
399 |
< |
public void tearDown() { |
400 |
< |
assertFalse(threadFailed); |
399 |
> |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
400 |
> |
Thread[] survivors = new Thread[5]; |
401 |
> |
int count = Thread.enumerate(survivors); |
402 |
> |
for (int i = 0; i < count; i++) { |
403 |
> |
Thread thread = survivors[i]; |
404 |
> |
String name = thread.getName(); |
405 |
> |
if (name.startsWith("ForkJoinPool-")) { |
406 |
> |
// give thread some time to terminate |
407 |
> |
thread.join(LONG_DELAY_MS); |
408 |
> |
if (!thread.isAlive()) continue; |
409 |
> |
thread.stop(); |
410 |
> |
throw new AssertionFailedError |
411 |
> |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
412 |
> |
toString(), name)); |
413 |
> |
} |
414 |
> |
} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* Fail, also setting status to indicate current testcase should fail |
419 |
< |
*/ |
418 |
> |
* Just like fail(reason), but additionally recording (using |
419 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
420 |
> |
* the current testcase will fail. |
421 |
> |
*/ |
422 |
|
public void threadFail(String reason) { |
423 |
< |
threadFailed = true; |
424 |
< |
fail(reason); |
423 |
> |
try { |
424 |
> |
fail(reason); |
425 |
> |
} catch (AssertionFailedError t) { |
426 |
> |
threadRecordFailure(t); |
427 |
> |
fail(reason); |
428 |
> |
} |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* If expression not true, set status to indicate current testcase |
433 |
< |
* should fail |
434 |
< |
*/ |
432 |
> |
* Just like assertTrue(b), but additionally recording (using |
433 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
434 |
> |
* the current testcase will fail. |
435 |
> |
*/ |
436 |
|
public void threadAssertTrue(boolean b) { |
437 |
< |
if (!b) { |
212 |
< |
threadFailed = true; |
437 |
> |
try { |
438 |
|
assertTrue(b); |
439 |
+ |
} catch (AssertionFailedError t) { |
440 |
+ |
threadRecordFailure(t); |
441 |
+ |
throw t; |
442 |
|
} |
443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
< |
* If expression not false, set status to indicate current testcase |
447 |
< |
* should fail |
448 |
< |
*/ |
446 |
> |
* Just like assertFalse(b), but additionally recording (using |
447 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
448 |
> |
* the current testcase will fail. |
449 |
> |
*/ |
450 |
|
public void threadAssertFalse(boolean b) { |
451 |
< |
if (b) { |
223 |
< |
threadFailed = true; |
451 |
> |
try { |
452 |
|
assertFalse(b); |
453 |
+ |
} catch (AssertionFailedError t) { |
454 |
+ |
threadRecordFailure(t); |
455 |
+ |
throw t; |
456 |
|
} |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
< |
* If argument not null, set status to indicate current testcase |
461 |
< |
* should fail |
462 |
< |
*/ |
460 |
> |
* Just like assertNull(x), but additionally recording (using |
461 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
462 |
> |
* the current testcase will fail. |
463 |
> |
*/ |
464 |
|
public void threadAssertNull(Object x) { |
465 |
< |
if (x != null) { |
234 |
< |
threadFailed = true; |
465 |
> |
try { |
466 |
|
assertNull(x); |
467 |
+ |
} catch (AssertionFailedError t) { |
468 |
+ |
threadRecordFailure(t); |
469 |
+ |
throw t; |
470 |
|
} |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* If arguments not equal, set status to indicate current testcase |
475 |
< |
* should fail |
476 |
< |
*/ |
474 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
475 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
476 |
> |
* the current testcase will fail. |
477 |
> |
*/ |
478 |
|
public void threadAssertEquals(long x, long y) { |
479 |
< |
if (x != y) { |
245 |
< |
threadFailed = true; |
479 |
> |
try { |
480 |
|
assertEquals(x, y); |
481 |
+ |
} catch (AssertionFailedError t) { |
482 |
+ |
threadRecordFailure(t); |
483 |
+ |
throw t; |
484 |
|
} |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
< |
* If arguments not equal, set status to indicate current testcase |
489 |
< |
* should fail |
490 |
< |
*/ |
488 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
489 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
490 |
> |
* the current testcase will fail. |
491 |
> |
*/ |
492 |
|
public void threadAssertEquals(Object x, Object y) { |
493 |
< |
if (x != y && (x == null || !x.equals(y))) { |
256 |
< |
threadFailed = true; |
493 |
> |
try { |
494 |
|
assertEquals(x, y); |
495 |
+ |
} catch (AssertionFailedError t) { |
496 |
+ |
threadRecordFailure(t); |
497 |
+ |
throw t; |
498 |
+ |
} catch (Throwable t) { |
499 |
+ |
threadUnexpectedException(t); |
500 |
|
} |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
< |
* threadFail with message "should throw exception" |
505 |
< |
*/ |
504 |
> |
* Just like assertSame(x, y), but additionally recording (using |
505 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
506 |
> |
* the current testcase will fail. |
507 |
> |
*/ |
508 |
> |
public void threadAssertSame(Object x, Object y) { |
509 |
> |
try { |
510 |
> |
assertSame(x, y); |
511 |
> |
} catch (AssertionFailedError t) { |
512 |
> |
threadRecordFailure(t); |
513 |
> |
throw t; |
514 |
> |
} |
515 |
> |
} |
516 |
> |
|
517 |
> |
/** |
518 |
> |
* Calls threadFail with message "should throw exception". |
519 |
> |
*/ |
520 |
|
public void threadShouldThrow() { |
521 |
< |
threadFailed = true; |
266 |
< |
fail("should throw exception"); |
521 |
> |
threadFail("should throw exception"); |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* threadFail with message "Unexpected exception" |
525 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
526 |
|
*/ |
527 |
< |
public void threadUnexpectedException() { |
528 |
< |
threadFailed = true; |
274 |
< |
fail("Unexpected exception"); |
527 |
> |
public void threadShouldThrow(String exceptionName) { |
528 |
> |
threadFail("should throw " + exceptionName); |
529 |
|
} |
530 |
|
|
531 |
+ |
/** |
532 |
+ |
* Records the given exception using {@link #threadRecordFailure}, |
533 |
+ |
* then rethrows the exception, wrapping it in an |
534 |
+ |
* AssertionFailedError if necessary. |
535 |
+ |
*/ |
536 |
+ |
public void threadUnexpectedException(Throwable t) { |
537 |
+ |
threadRecordFailure(t); |
538 |
+ |
t.printStackTrace(); |
539 |
+ |
if (t instanceof RuntimeException) |
540 |
+ |
throw (RuntimeException) t; |
541 |
+ |
else if (t instanceof Error) |
542 |
+ |
throw (Error) t; |
543 |
+ |
else { |
544 |
+ |
AssertionFailedError afe = |
545 |
+ |
new AssertionFailedError("unexpected exception: " + t); |
546 |
+ |
afe.initCause(t); |
547 |
+ |
throw afe; |
548 |
+ |
} |
549 |
+ |
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* Wait out termination of a thread pool or fail doing so |
552 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
553 |
> |
* if the sleep is shorter than specified, may re-sleep or yield |
554 |
> |
* until time elapses. |
555 |
|
*/ |
556 |
< |
public void joinPool(ExecutorService exec) { |
556 |
> |
static void delay(long millis) throws InterruptedException { |
557 |
> |
long startTime = System.nanoTime(); |
558 |
> |
long ns = millis * 1000 * 1000; |
559 |
> |
for (;;) { |
560 |
> |
if (millis > 0L) |
561 |
> |
Thread.sleep(millis); |
562 |
> |
else // too short to sleep |
563 |
> |
Thread.yield(); |
564 |
> |
long d = ns - (System.nanoTime() - startTime); |
565 |
> |
if (d > 0L) |
566 |
> |
millis = d / (1000 * 1000); |
567 |
> |
else |
568 |
> |
break; |
569 |
> |
} |
570 |
> |
} |
571 |
> |
|
572 |
> |
/** |
573 |
> |
* Waits out termination of a thread pool or fails doing so. |
574 |
> |
*/ |
575 |
> |
void joinPool(ExecutorService exec) { |
576 |
|
try { |
577 |
|
exec.shutdown(); |
578 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
579 |
< |
} catch(InterruptedException ie) { |
580 |
< |
fail("Unexpected exception"); |
578 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
579 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
580 |
> |
} catch (SecurityException ok) { |
581 |
> |
// Allowed in case test doesn't have privs |
582 |
> |
} catch (InterruptedException ie) { |
583 |
> |
fail("Unexpected InterruptedException"); |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
+ |
/** |
588 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
589 |
+ |
*/ |
590 |
+ |
static void printAllStackTraces() { |
591 |
+ |
for (ThreadInfo info : |
592 |
+ |
ManagementFactory.getThreadMXBean() |
593 |
+ |
.dumpAllThreads(true, true)) |
594 |
+ |
System.err.print(info); |
595 |
+ |
} |
596 |
+ |
|
597 |
+ |
/** |
598 |
+ |
* Checks that thread does not terminate within the default |
599 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
600 |
+ |
*/ |
601 |
+ |
void assertThreadStaysAlive(Thread thread) { |
602 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
603 |
+ |
} |
604 |
+ |
|
605 |
+ |
/** |
606 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
607 |
+ |
*/ |
608 |
+ |
void assertThreadStaysAlive(Thread thread, long millis) { |
609 |
+ |
try { |
610 |
+ |
// No need to optimize the failing case via Thread.join. |
611 |
+ |
delay(millis); |
612 |
+ |
assertTrue(thread.isAlive()); |
613 |
+ |
} catch (InterruptedException ie) { |
614 |
+ |
fail("Unexpected InterruptedException"); |
615 |
+ |
} |
616 |
+ |
} |
617 |
|
|
618 |
|
/** |
619 |
< |
* fail with message "should throw exception" |
620 |
< |
*/ |
619 |
> |
* Checks that the threads do not terminate within the default |
620 |
> |
* millisecond delay of {@code timeoutMillis()}. |
621 |
> |
*/ |
622 |
> |
void assertThreadsStayAlive(Thread... threads) { |
623 |
> |
assertThreadsStayAlive(timeoutMillis(), threads); |
624 |
> |
} |
625 |
> |
|
626 |
> |
/** |
627 |
> |
* Checks that the threads do not terminate within the given millisecond delay. |
628 |
> |
*/ |
629 |
> |
void assertThreadsStayAlive(long millis, Thread... threads) { |
630 |
> |
try { |
631 |
> |
// No need to optimize the failing case via Thread.join. |
632 |
> |
delay(millis); |
633 |
> |
for (Thread thread : threads) |
634 |
> |
assertTrue(thread.isAlive()); |
635 |
> |
} catch (InterruptedException ie) { |
636 |
> |
fail("Unexpected InterruptedException"); |
637 |
> |
} |
638 |
> |
} |
639 |
> |
|
640 |
> |
/** |
641 |
> |
* Checks that future.get times out, with the default timeout of |
642 |
> |
* {@code timeoutMillis()}. |
643 |
> |
*/ |
644 |
> |
void assertFutureTimesOut(Future future) { |
645 |
> |
assertFutureTimesOut(future, timeoutMillis()); |
646 |
> |
} |
647 |
> |
|
648 |
> |
/** |
649 |
> |
* Checks that future.get times out, with the given millisecond timeout. |
650 |
> |
*/ |
651 |
> |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
652 |
> |
long startTime = System.nanoTime(); |
653 |
> |
try { |
654 |
> |
future.get(timeoutMillis, MILLISECONDS); |
655 |
> |
shouldThrow(); |
656 |
> |
} catch (TimeoutException success) { |
657 |
> |
} catch (Exception e) { |
658 |
> |
threadUnexpectedException(e); |
659 |
> |
} finally { future.cancel(true); } |
660 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
661 |
> |
} |
662 |
> |
|
663 |
> |
/** |
664 |
> |
* Fails with message "should throw exception". |
665 |
> |
*/ |
666 |
|
public void shouldThrow() { |
667 |
|
fail("Should throw exception"); |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
< |
* fail with message "Unexpected exception" |
671 |
> |
* Fails with message "should throw " + exceptionName. |
672 |
|
*/ |
673 |
< |
public void unexpectedException() { |
674 |
< |
fail("Unexpected exception"); |
673 |
> |
public void shouldThrow(String exceptionName) { |
674 |
> |
fail("Should throw " + exceptionName); |
675 |
|
} |
676 |
|
|
305 |
– |
|
677 |
|
/** |
678 |
|
* The number of elements to place in collections, arrays, etc. |
679 |
|
*/ |
680 |
< |
static final int SIZE = 20; |
680 |
> |
public static final int SIZE = 20; |
681 |
|
|
682 |
|
// Some convenient Integer constants |
683 |
|
|
684 |
< |
static final Integer zero = new Integer(0); |
685 |
< |
static final Integer one = new Integer(1); |
686 |
< |
static final Integer two = new Integer(2); |
687 |
< |
static final Integer three = new Integer(3); |
688 |
< |
static final Integer four = new Integer(4); |
689 |
< |
static final Integer five = new Integer(5); |
690 |
< |
static final Integer six = new Integer(6); |
691 |
< |
static final Integer seven = new Integer(7); |
692 |
< |
static final Integer eight = new Integer(8); |
693 |
< |
static final Integer nine = new Integer(9); |
694 |
< |
static final Integer m1 = new Integer(-1); |
695 |
< |
static final Integer m2 = new Integer(-2); |
696 |
< |
static final Integer m3 = new Integer(-3); |
697 |
< |
static final Integer m4 = new Integer(-4); |
698 |
< |
static final Integer m5 = new Integer(-5); |
699 |
< |
static final Integer m10 = new Integer(-10); |
684 |
> |
public static final Integer zero = new Integer(0); |
685 |
> |
public static final Integer one = new Integer(1); |
686 |
> |
public static final Integer two = new Integer(2); |
687 |
> |
public static final Integer three = new Integer(3); |
688 |
> |
public static final Integer four = new Integer(4); |
689 |
> |
public static final Integer five = new Integer(5); |
690 |
> |
public static final Integer six = new Integer(6); |
691 |
> |
public static final Integer seven = new Integer(7); |
692 |
> |
public static final Integer eight = new Integer(8); |
693 |
> |
public static final Integer nine = new Integer(9); |
694 |
> |
public static final Integer m1 = new Integer(-1); |
695 |
> |
public static final Integer m2 = new Integer(-2); |
696 |
> |
public static final Integer m3 = new Integer(-3); |
697 |
> |
public static final Integer m4 = new Integer(-4); |
698 |
> |
public static final Integer m5 = new Integer(-5); |
699 |
> |
public static final Integer m6 = new Integer(-6); |
700 |
> |
public static final Integer m10 = new Integer(-10); |
701 |
|
|
702 |
|
|
703 |
|
/** |
704 |
+ |
* Runs Runnable r with a security policy that permits precisely |
705 |
+ |
* the specified permissions. If there is no current security |
706 |
+ |
* manager, the runnable is run twice, both with and without a |
707 |
+ |
* security manager. We require that any security manager permit |
708 |
+ |
* getPolicy/setPolicy. |
709 |
+ |
*/ |
710 |
+ |
public void runWithPermissions(Runnable r, Permission... permissions) { |
711 |
+ |
SecurityManager sm = System.getSecurityManager(); |
712 |
+ |
if (sm == null) { |
713 |
+ |
r.run(); |
714 |
+ |
} |
715 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
716 |
+ |
} |
717 |
+ |
|
718 |
+ |
/** |
719 |
+ |
* Runs Runnable r with a security policy that permits precisely |
720 |
+ |
* the specified permissions. If there is no current security |
721 |
+ |
* manager, a temporary one is set for the duration of the |
722 |
+ |
* Runnable. We require that any security manager permit |
723 |
+ |
* getPolicy/setPolicy. |
724 |
+ |
*/ |
725 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
726 |
+ |
Permission... permissions) { |
727 |
+ |
SecurityManager sm = System.getSecurityManager(); |
728 |
+ |
if (sm == null) { |
729 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
730 |
+ |
try { |
731 |
+ |
Policy.setPolicy(permissivePolicy()); |
732 |
+ |
System.setSecurityManager(new SecurityManager()); |
733 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
734 |
+ |
} finally { |
735 |
+ |
System.setSecurityManager(null); |
736 |
+ |
Policy.setPolicy(savedPolicy); |
737 |
+ |
} |
738 |
+ |
} else { |
739 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
740 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
741 |
+ |
Policy.setPolicy(policy); |
742 |
+ |
|
743 |
+ |
try { |
744 |
+ |
r.run(); |
745 |
+ |
} finally { |
746 |
+ |
policy.addPermission(new SecurityPermission("setPolicy")); |
747 |
+ |
Policy.setPolicy(savedPolicy); |
748 |
+ |
} |
749 |
+ |
} |
750 |
+ |
} |
751 |
+ |
|
752 |
+ |
/** |
753 |
+ |
* Runs a runnable without any permissions. |
754 |
+ |
*/ |
755 |
+ |
public void runWithoutPermissions(Runnable r) { |
756 |
+ |
runWithPermissions(r); |
757 |
+ |
} |
758 |
+ |
|
759 |
+ |
/** |
760 |
|
* A security policy where new permissions can be dynamically added |
761 |
|
* or all cleared. |
762 |
|
*/ |
763 |
< |
static class AdjustablePolicy extends java.security.Policy { |
763 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
764 |
|
Permissions perms = new Permissions(); |
765 |
< |
AdjustablePolicy() { } |
765 |
> |
AdjustablePolicy(Permission... permissions) { |
766 |
> |
for (Permission permission : permissions) |
767 |
> |
perms.add(permission); |
768 |
> |
} |
769 |
|
void addPermission(Permission perm) { perms.add(perm); } |
770 |
|
void clearPermissions() { perms = new Permissions(); } |
771 |
< |
public PermissionCollection getPermissions(CodeSource cs) { |
772 |
< |
return perms; |
773 |
< |
} |
774 |
< |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
775 |
< |
return perms; |
776 |
< |
} |
777 |
< |
public boolean implies(ProtectionDomain pd, Permission p) { |
778 |
< |
return perms.implies(p); |
779 |
< |
} |
780 |
< |
public void refresh() {} |
771 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
772 |
> |
return perms; |
773 |
> |
} |
774 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
775 |
> |
return perms; |
776 |
> |
} |
777 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
778 |
> |
return perms.implies(p); |
779 |
> |
} |
780 |
> |
public void refresh() {} |
781 |
> |
public String toString() { |
782 |
> |
List<Permission> ps = new ArrayList<Permission>(); |
783 |
> |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
784 |
> |
ps.add(e.nextElement()); |
785 |
> |
return "AdjustablePolicy with permissions " + ps; |
786 |
> |
} |
787 |
|
} |
788 |
|
|
789 |
+ |
/** |
790 |
+ |
* Returns a policy containing all the permissions we ever need. |
791 |
+ |
*/ |
792 |
+ |
public static Policy permissivePolicy() { |
793 |
+ |
return new AdjustablePolicy |
794 |
+ |
// Permissions j.u.c. needs directly |
795 |
+ |
(new RuntimePermission("modifyThread"), |
796 |
+ |
new RuntimePermission("getClassLoader"), |
797 |
+ |
new RuntimePermission("setContextClassLoader"), |
798 |
+ |
// Permissions needed to change permissions! |
799 |
+ |
new SecurityPermission("getPolicy"), |
800 |
+ |
new SecurityPermission("setPolicy"), |
801 |
+ |
new RuntimePermission("setSecurityManager"), |
802 |
+ |
// Permissions needed by the junit test harness |
803 |
+ |
new RuntimePermission("accessDeclaredMembers"), |
804 |
+ |
new PropertyPermission("*", "read"), |
805 |
+ |
new java.io.FilePermission("<<ALL FILES>>", "read")); |
806 |
+ |
} |
807 |
|
|
808 |
< |
// Some convenient Runnable classes |
808 |
> |
/** |
809 |
> |
* Sleeps until the given time has elapsed. |
810 |
> |
* Throws AssertionFailedError if interrupted. |
811 |
> |
*/ |
812 |
> |
void sleep(long millis) { |
813 |
> |
try { |
814 |
> |
delay(millis); |
815 |
> |
} catch (InterruptedException ie) { |
816 |
> |
AssertionFailedError afe = |
817 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
818 |
> |
afe.initCause(ie); |
819 |
> |
throw afe; |
820 |
> |
} |
821 |
> |
} |
822 |
|
|
823 |
< |
static class NoOpRunnable implements Runnable { |
824 |
< |
public void run() {} |
823 |
> |
/** |
824 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
825 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
826 |
> |
*/ |
827 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
828 |
> |
long startTime = System.nanoTime(); |
829 |
> |
for (;;) { |
830 |
> |
Thread.State s = thread.getState(); |
831 |
> |
if (s == Thread.State.BLOCKED || |
832 |
> |
s == Thread.State.WAITING || |
833 |
> |
s == Thread.State.TIMED_WAITING) |
834 |
> |
return; |
835 |
> |
else if (s == Thread.State.TERMINATED) |
836 |
> |
fail("Unexpected thread termination"); |
837 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
838 |
> |
threadAssertTrue(thread.isAlive()); |
839 |
> |
return; |
840 |
> |
} |
841 |
> |
Thread.yield(); |
842 |
> |
} |
843 |
|
} |
844 |
|
|
845 |
< |
static class NoOpCallable implements Callable { |
846 |
< |
public Object call() { return Boolean.TRUE; } |
845 |
> |
/** |
846 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
847 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
848 |
> |
*/ |
849 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
850 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
851 |
|
} |
852 |
|
|
853 |
< |
static final String TEST_STRING = "a test string"; |
853 |
> |
/** |
854 |
> |
* Returns the number of milliseconds since time given by |
855 |
> |
* startNanoTime, which must have been previously returned from a |
856 |
> |
* call to {@link System.nanoTime()}. |
857 |
> |
*/ |
858 |
> |
long millisElapsedSince(long startNanoTime) { |
859 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
860 |
> |
} |
861 |
|
|
862 |
< |
static class StringTask implements Callable<String> { |
863 |
< |
public String call() { return TEST_STRING; } |
862 |
> |
/** |
863 |
> |
* Returns a new started daemon Thread running the given runnable. |
864 |
> |
*/ |
865 |
> |
Thread newStartedThread(Runnable runnable) { |
866 |
> |
Thread t = new Thread(runnable); |
867 |
> |
t.setDaemon(true); |
868 |
> |
t.start(); |
869 |
> |
return t; |
870 |
|
} |
871 |
|
|
872 |
< |
static class NPETask implements Callable<String> { |
873 |
< |
public String call() { throw new NullPointerException(); } |
872 |
> |
/** |
873 |
> |
* Waits for the specified time (in milliseconds) for the thread |
874 |
> |
* to terminate (using {@link Thread#join(long)}), else interrupts |
875 |
> |
* the thread (in the hope that it may terminate later) and fails. |
876 |
> |
*/ |
877 |
> |
void awaitTermination(Thread t, long timeoutMillis) { |
878 |
> |
try { |
879 |
> |
t.join(timeoutMillis); |
880 |
> |
} catch (InterruptedException ie) { |
881 |
> |
threadUnexpectedException(ie); |
882 |
> |
} finally { |
883 |
> |
if (t.getState() != Thread.State.TERMINATED) { |
884 |
> |
t.interrupt(); |
885 |
> |
fail("Test timed out"); |
886 |
> |
} |
887 |
> |
} |
888 |
|
} |
889 |
|
|
890 |
< |
static class CallableOne implements Callable<Integer> { |
891 |
< |
public Integer call() { return one; } |
890 |
> |
/** |
891 |
> |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
892 |
> |
* terminate (using {@link Thread#join(long)}), else interrupts |
893 |
> |
* the thread (in the hope that it may terminate later) and fails. |
894 |
> |
*/ |
895 |
> |
void awaitTermination(Thread t) { |
896 |
> |
awaitTermination(t, LONG_DELAY_MS); |
897 |
|
} |
898 |
|
|
899 |
< |
class ShortRunnable implements Runnable { |
900 |
< |
public void run() { |
899 |
> |
// Some convenient Runnable classes |
900 |
> |
|
901 |
> |
public abstract class CheckedRunnable implements Runnable { |
902 |
> |
protected abstract void realRun() throws Throwable; |
903 |
> |
|
904 |
> |
public final void run() { |
905 |
|
try { |
906 |
< |
Thread.sleep(SHORT_DELAY_MS); |
907 |
< |
} |
908 |
< |
catch(Exception e) { |
383 |
< |
threadUnexpectedException(); |
906 |
> |
realRun(); |
907 |
> |
} catch (Throwable t) { |
908 |
> |
threadUnexpectedException(t); |
909 |
|
} |
910 |
|
} |
911 |
|
} |
912 |
|
|
913 |
< |
class ShortInterruptedRunnable implements Runnable { |
914 |
< |
public void run() { |
913 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
914 |
> |
protected abstract void realRun() throws Throwable; |
915 |
> |
|
916 |
> |
final Class<?> exceptionClass; |
917 |
> |
|
918 |
> |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
919 |
> |
this.exceptionClass = exceptionClass; |
920 |
> |
} |
921 |
> |
|
922 |
> |
public final void run() { |
923 |
|
try { |
924 |
< |
Thread.sleep(SHORT_DELAY_MS); |
925 |
< |
threadShouldThrow(); |
926 |
< |
} |
927 |
< |
catch(InterruptedException success) { |
924 |
> |
realRun(); |
925 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
926 |
> |
} catch (Throwable t) { |
927 |
> |
if (! exceptionClass.isInstance(t)) |
928 |
> |
threadUnexpectedException(t); |
929 |
|
} |
930 |
|
} |
931 |
|
} |
932 |
|
|
933 |
< |
class SmallRunnable implements Runnable { |
934 |
< |
public void run() { |
933 |
> |
public abstract class ThreadShouldThrow extends Thread { |
934 |
> |
protected abstract void realRun() throws Throwable; |
935 |
> |
|
936 |
> |
final Class<?> exceptionClass; |
937 |
> |
|
938 |
> |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
939 |
> |
this.exceptionClass = exceptionClass; |
940 |
> |
} |
941 |
> |
|
942 |
> |
public final void run() { |
943 |
|
try { |
944 |
< |
Thread.sleep(SMALL_DELAY_MS); |
945 |
< |
} |
946 |
< |
catch(Exception e) { |
947 |
< |
threadUnexpectedException(); |
944 |
> |
realRun(); |
945 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
946 |
> |
} catch (Throwable t) { |
947 |
> |
if (! exceptionClass.isInstance(t)) |
948 |
> |
threadUnexpectedException(t); |
949 |
|
} |
950 |
|
} |
951 |
|
} |
952 |
|
|
953 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
954 |
< |
public void run() { |
953 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
954 |
> |
protected abstract void realRun() throws Throwable; |
955 |
> |
|
956 |
> |
public final void run() { |
957 |
|
try { |
958 |
< |
Thread.sleep(SMALL_DELAY_MS); |
959 |
< |
} |
960 |
< |
catch(Exception e) { |
958 |
> |
realRun(); |
959 |
> |
threadShouldThrow("InterruptedException"); |
960 |
> |
} catch (InterruptedException success) { |
961 |
> |
threadAssertFalse(Thread.interrupted()); |
962 |
> |
} catch (Throwable t) { |
963 |
> |
threadUnexpectedException(t); |
964 |
|
} |
965 |
|
} |
966 |
|
} |
967 |
|
|
968 |
< |
class SmallCallable implements Callable { |
969 |
< |
public Object call() { |
968 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
969 |
> |
protected abstract T realCall() throws Throwable; |
970 |
> |
|
971 |
> |
public final T call() { |
972 |
|
try { |
973 |
< |
Thread.sleep(SMALL_DELAY_MS); |
973 |
> |
return realCall(); |
974 |
> |
} catch (Throwable t) { |
975 |
> |
threadUnexpectedException(t); |
976 |
> |
return null; |
977 |
|
} |
425 |
– |
catch(Exception e) { |
426 |
– |
threadUnexpectedException(); |
427 |
– |
} |
428 |
– |
return Boolean.TRUE; |
978 |
|
} |
979 |
|
} |
980 |
|
|
981 |
< |
class SmallInterruptedRunnable implements Runnable { |
982 |
< |
public void run() { |
981 |
> |
public abstract class CheckedInterruptedCallable<T> |
982 |
> |
implements Callable<T> { |
983 |
> |
protected abstract T realCall() throws Throwable; |
984 |
> |
|
985 |
> |
public final T call() { |
986 |
|
try { |
987 |
< |
Thread.sleep(SMALL_DELAY_MS); |
988 |
< |
threadShouldThrow(); |
989 |
< |
} |
990 |
< |
catch(InterruptedException success) { |
987 |
> |
T result = realCall(); |
988 |
> |
threadShouldThrow("InterruptedException"); |
989 |
> |
return result; |
990 |
> |
} catch (InterruptedException success) { |
991 |
> |
threadAssertFalse(Thread.interrupted()); |
992 |
> |
} catch (Throwable t) { |
993 |
> |
threadUnexpectedException(t); |
994 |
|
} |
995 |
+ |
return null; |
996 |
|
} |
997 |
|
} |
998 |
|
|
999 |
+ |
public static class NoOpRunnable implements Runnable { |
1000 |
+ |
public void run() {} |
1001 |
+ |
} |
1002 |
|
|
1003 |
< |
class MediumRunnable implements Runnable { |
1004 |
< |
public void run() { |
1005 |
< |
try { |
1006 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1007 |
< |
} |
1008 |
< |
catch(Exception e) { |
1009 |
< |
threadUnexpectedException(); |
1010 |
< |
} |
1003 |
> |
public static class NoOpCallable implements Callable { |
1004 |
> |
public Object call() { return Boolean.TRUE; } |
1005 |
> |
} |
1006 |
> |
|
1007 |
> |
public static final String TEST_STRING = "a test string"; |
1008 |
> |
|
1009 |
> |
public static class StringTask implements Callable<String> { |
1010 |
> |
public String call() { return TEST_STRING; } |
1011 |
> |
} |
1012 |
> |
|
1013 |
> |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1014 |
> |
return new CheckedCallable<String>() { |
1015 |
> |
protected String realCall() { |
1016 |
> |
try { |
1017 |
> |
latch.await(); |
1018 |
> |
} catch (InterruptedException quittingTime) {} |
1019 |
> |
return TEST_STRING; |
1020 |
> |
}}; |
1021 |
> |
} |
1022 |
> |
|
1023 |
> |
public Runnable awaiter(final CountDownLatch latch) { |
1024 |
> |
return new CheckedRunnable() { |
1025 |
> |
public void realRun() throws InterruptedException { |
1026 |
> |
await(latch); |
1027 |
> |
}}; |
1028 |
> |
} |
1029 |
> |
|
1030 |
> |
public void await(CountDownLatch latch) { |
1031 |
> |
try { |
1032 |
> |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1033 |
> |
} catch (Throwable t) { |
1034 |
> |
threadUnexpectedException(t); |
1035 |
|
} |
1036 |
|
} |
1037 |
|
|
1038 |
< |
class MediumInterruptedRunnable implements Runnable { |
1039 |
< |
public void run() { |
1038 |
> |
public void await(Semaphore semaphore) { |
1039 |
> |
try { |
1040 |
> |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1041 |
> |
} catch (Throwable t) { |
1042 |
> |
threadUnexpectedException(t); |
1043 |
> |
} |
1044 |
> |
} |
1045 |
> |
|
1046 |
> |
// /** |
1047 |
> |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1048 |
> |
// */ |
1049 |
> |
// public void await(AtomicBoolean flag) { |
1050 |
> |
// await(flag, LONG_DELAY_MS); |
1051 |
> |
// } |
1052 |
> |
|
1053 |
> |
// /** |
1054 |
> |
// * Spin-waits up to the specified timeout until flag becomes true. |
1055 |
> |
// */ |
1056 |
> |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1057 |
> |
// long startTime = System.nanoTime(); |
1058 |
> |
// while (!flag.get()) { |
1059 |
> |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1060 |
> |
// throw new AssertionFailedError("timed out"); |
1061 |
> |
// Thread.yield(); |
1062 |
> |
// } |
1063 |
> |
// } |
1064 |
> |
|
1065 |
> |
public static class NPETask implements Callable<String> { |
1066 |
> |
public String call() { throw new NullPointerException(); } |
1067 |
> |
} |
1068 |
> |
|
1069 |
> |
public static class CallableOne implements Callable<Integer> { |
1070 |
> |
public Integer call() { return one; } |
1071 |
> |
} |
1072 |
> |
|
1073 |
> |
public class ShortRunnable extends CheckedRunnable { |
1074 |
> |
protected void realRun() throws Throwable { |
1075 |
> |
delay(SHORT_DELAY_MS); |
1076 |
> |
} |
1077 |
> |
} |
1078 |
> |
|
1079 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1080 |
> |
protected void realRun() throws InterruptedException { |
1081 |
> |
delay(SHORT_DELAY_MS); |
1082 |
> |
} |
1083 |
> |
} |
1084 |
> |
|
1085 |
> |
public class SmallRunnable extends CheckedRunnable { |
1086 |
> |
protected void realRun() throws Throwable { |
1087 |
> |
delay(SMALL_DELAY_MS); |
1088 |
> |
} |
1089 |
> |
} |
1090 |
> |
|
1091 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1092 |
> |
protected void realRun() { |
1093 |
|
try { |
1094 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1095 |
< |
threadShouldThrow(); |
460 |
< |
} |
461 |
< |
catch(InterruptedException success) { |
462 |
< |
} |
1094 |
> |
delay(SMALL_DELAY_MS); |
1095 |
> |
} catch (InterruptedException ok) {} |
1096 |
|
} |
1097 |
|
} |
1098 |
|
|
1099 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
1100 |
< |
public void run() { |
1099 |
> |
public class SmallCallable extends CheckedCallable { |
1100 |
> |
protected Object realCall() throws InterruptedException { |
1101 |
> |
delay(SMALL_DELAY_MS); |
1102 |
> |
return Boolean.TRUE; |
1103 |
> |
} |
1104 |
> |
} |
1105 |
> |
|
1106 |
> |
public class MediumRunnable extends CheckedRunnable { |
1107 |
> |
protected void realRun() throws Throwable { |
1108 |
> |
delay(MEDIUM_DELAY_MS); |
1109 |
> |
} |
1110 |
> |
} |
1111 |
> |
|
1112 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1113 |
> |
protected void realRun() throws InterruptedException { |
1114 |
> |
delay(MEDIUM_DELAY_MS); |
1115 |
> |
} |
1116 |
> |
} |
1117 |
> |
|
1118 |
> |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1119 |
> |
return new CheckedRunnable() { |
1120 |
> |
protected void realRun() { |
1121 |
> |
try { |
1122 |
> |
delay(timeoutMillis); |
1123 |
> |
} catch (InterruptedException ok) {} |
1124 |
> |
}}; |
1125 |
> |
} |
1126 |
> |
|
1127 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1128 |
> |
protected void realRun() { |
1129 |
|
try { |
1130 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1131 |
< |
} |
471 |
< |
catch(InterruptedException success) { |
472 |
< |
} |
1130 |
> |
delay(MEDIUM_DELAY_MS); |
1131 |
> |
} catch (InterruptedException ok) {} |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1135 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
1136 |
< |
public void run() { |
1135 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1136 |
> |
protected void realRun() { |
1137 |
|
try { |
1138 |
< |
Thread.sleep(LONG_DELAY_MS); |
1139 |
< |
} |
481 |
< |
catch(InterruptedException success) { |
482 |
< |
} |
1138 |
> |
delay(LONG_DELAY_MS); |
1139 |
> |
} catch (InterruptedException ok) {} |
1140 |
|
} |
1141 |
|
} |
1142 |
|
|
1143 |
|
/** |
1144 |
|
* For use as ThreadFactory in constructors |
1145 |
|
*/ |
1146 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
1147 |
< |
public Thread newThread(Runnable r){ |
1146 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
1147 |
> |
public Thread newThread(Runnable r) { |
1148 |
|
return new Thread(r); |
1149 |
< |
} |
1149 |
> |
} |
1150 |
> |
} |
1151 |
> |
|
1152 |
> |
public interface TrackedRunnable extends Runnable { |
1153 |
> |
boolean isDone(); |
1154 |
> |
} |
1155 |
> |
|
1156 |
> |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1157 |
> |
return new TrackedRunnable() { |
1158 |
> |
private volatile boolean done = false; |
1159 |
> |
public boolean isDone() { return done; } |
1160 |
> |
public void run() { |
1161 |
> |
try { |
1162 |
> |
delay(timeoutMillis); |
1163 |
> |
done = true; |
1164 |
> |
} catch (InterruptedException ok) {} |
1165 |
> |
} |
1166 |
> |
}; |
1167 |
|
} |
1168 |
|
|
1169 |
< |
static class TrackedShortRunnable implements Runnable { |
1170 |
< |
volatile boolean done = false; |
1169 |
> |
public static class TrackedShortRunnable implements Runnable { |
1170 |
> |
public volatile boolean done = false; |
1171 |
|
public void run() { |
1172 |
|
try { |
1173 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1173 |
> |
delay(SHORT_DELAY_MS); |
1174 |
|
done = true; |
1175 |
< |
} catch(Exception e){ |
502 |
< |
} |
1175 |
> |
} catch (InterruptedException ok) {} |
1176 |
|
} |
1177 |
|
} |
1178 |
|
|
1179 |
< |
static class TrackedMediumRunnable implements Runnable { |
1180 |
< |
volatile boolean done = false; |
1179 |
> |
public static class TrackedSmallRunnable implements Runnable { |
1180 |
> |
public volatile boolean done = false; |
1181 |
|
public void run() { |
1182 |
|
try { |
1183 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1183 |
> |
delay(SMALL_DELAY_MS); |
1184 |
|
done = true; |
1185 |
< |
} catch(Exception e){ |
513 |
< |
} |
1185 |
> |
} catch (InterruptedException ok) {} |
1186 |
|
} |
1187 |
|
} |
1188 |
|
|
1189 |
< |
static class TrackedLongRunnable implements Runnable { |
1190 |
< |
volatile boolean done = false; |
1189 |
> |
public static class TrackedMediumRunnable implements Runnable { |
1190 |
> |
public volatile boolean done = false; |
1191 |
|
public void run() { |
1192 |
|
try { |
1193 |
< |
Thread.sleep(LONG_DELAY_MS); |
1193 |
> |
delay(MEDIUM_DELAY_MS); |
1194 |
|
done = true; |
1195 |
< |
} catch(Exception e){ |
524 |
< |
} |
1195 |
> |
} catch (InterruptedException ok) {} |
1196 |
|
} |
1197 |
|
} |
1198 |
|
|
1199 |
< |
static class TrackedNoOpRunnable implements Runnable { |
1200 |
< |
volatile boolean done = false; |
1199 |
> |
public static class TrackedLongRunnable implements Runnable { |
1200 |
> |
public volatile boolean done = false; |
1201 |
> |
public void run() { |
1202 |
> |
try { |
1203 |
> |
delay(LONG_DELAY_MS); |
1204 |
> |
done = true; |
1205 |
> |
} catch (InterruptedException ok) {} |
1206 |
> |
} |
1207 |
> |
} |
1208 |
> |
|
1209 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
1210 |
> |
public volatile boolean done = false; |
1211 |
|
public void run() { |
1212 |
|
done = true; |
1213 |
|
} |
1214 |
|
} |
1215 |
|
|
1216 |
< |
static class TrackedCallable implements Callable { |
1217 |
< |
volatile boolean done = false; |
1216 |
> |
public static class TrackedCallable implements Callable { |
1217 |
> |
public volatile boolean done = false; |
1218 |
|
public Object call() { |
1219 |
|
try { |
1220 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1220 |
> |
delay(SMALL_DELAY_MS); |
1221 |
|
done = true; |
1222 |
< |
} catch(Exception e){ |
542 |
< |
} |
1222 |
> |
} catch (InterruptedException ok) {} |
1223 |
|
return Boolean.TRUE; |
1224 |
|
} |
1225 |
|
} |
1226 |
|
|
1227 |
+ |
/** |
1228 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
1229 |
+ |
*/ |
1230 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1231 |
+ |
protected abstract void realCompute() throws Throwable; |
1232 |
+ |
|
1233 |
+ |
public final void compute() { |
1234 |
+ |
try { |
1235 |
+ |
realCompute(); |
1236 |
+ |
} catch (Throwable t) { |
1237 |
+ |
threadUnexpectedException(t); |
1238 |
+ |
} |
1239 |
+ |
} |
1240 |
+ |
} |
1241 |
+ |
|
1242 |
+ |
/** |
1243 |
+ |
* Analog of CheckedCallable for RecursiveTask |
1244 |
+ |
*/ |
1245 |
+ |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1246 |
+ |
protected abstract T realCompute() throws Throwable; |
1247 |
+ |
|
1248 |
+ |
public final T compute() { |
1249 |
+ |
try { |
1250 |
+ |
return realCompute(); |
1251 |
+ |
} catch (Throwable t) { |
1252 |
+ |
threadUnexpectedException(t); |
1253 |
+ |
return null; |
1254 |
+ |
} |
1255 |
+ |
} |
1256 |
+ |
} |
1257 |
|
|
1258 |
|
/** |
1259 |
|
* For use as RejectedExecutionHandler in constructors |
1260 |
|
*/ |
1261 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
1262 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
1261 |
> |
public static class NoOpREHandler implements RejectedExecutionHandler { |
1262 |
> |
public void rejectedExecution(Runnable r, |
1263 |
> |
ThreadPoolExecutor executor) {} |
1264 |
> |
} |
1265 |
> |
|
1266 |
> |
/** |
1267 |
> |
* A CyclicBarrier that uses timed await and fails with |
1268 |
> |
* AssertionFailedErrors instead of throwing checked exceptions. |
1269 |
> |
*/ |
1270 |
> |
public class CheckedBarrier extends CyclicBarrier { |
1271 |
> |
public CheckedBarrier(int parties) { super(parties); } |
1272 |
> |
|
1273 |
> |
public int await() { |
1274 |
> |
try { |
1275 |
> |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1276 |
> |
} catch (TimeoutException e) { |
1277 |
> |
throw new AssertionFailedError("timed out"); |
1278 |
> |
} catch (Exception e) { |
1279 |
> |
AssertionFailedError afe = |
1280 |
> |
new AssertionFailedError("Unexpected exception: " + e); |
1281 |
> |
afe.initCause(e); |
1282 |
> |
throw afe; |
1283 |
> |
} |
1284 |
> |
} |
1285 |
> |
} |
1286 |
> |
|
1287 |
> |
void checkEmpty(BlockingQueue q) { |
1288 |
> |
try { |
1289 |
> |
assertTrue(q.isEmpty()); |
1290 |
> |
assertEquals(0, q.size()); |
1291 |
> |
assertNull(q.peek()); |
1292 |
> |
assertNull(q.poll()); |
1293 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
1294 |
> |
assertEquals(q.toString(), "[]"); |
1295 |
> |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1296 |
> |
assertFalse(q.iterator().hasNext()); |
1297 |
> |
try { |
1298 |
> |
q.element(); |
1299 |
> |
shouldThrow(); |
1300 |
> |
} catch (NoSuchElementException success) {} |
1301 |
> |
try { |
1302 |
> |
q.iterator().next(); |
1303 |
> |
shouldThrow(); |
1304 |
> |
} catch (NoSuchElementException success) {} |
1305 |
> |
try { |
1306 |
> |
q.remove(); |
1307 |
> |
shouldThrow(); |
1308 |
> |
} catch (NoSuchElementException success) {} |
1309 |
> |
} catch (InterruptedException ie) { |
1310 |
> |
threadUnexpectedException(ie); |
1311 |
> |
} |
1312 |
> |
} |
1313 |
> |
|
1314 |
> |
void assertSerialEquals(Object x, Object y) { |
1315 |
> |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1316 |
> |
} |
1317 |
> |
|
1318 |
> |
void assertNotSerialEquals(Object x, Object y) { |
1319 |
> |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1320 |
> |
} |
1321 |
> |
|
1322 |
> |
byte[] serialBytes(Object o) { |
1323 |
> |
try { |
1324 |
> |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1325 |
> |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1326 |
> |
oos.writeObject(o); |
1327 |
> |
oos.flush(); |
1328 |
> |
oos.close(); |
1329 |
> |
return bos.toByteArray(); |
1330 |
> |
} catch (Throwable t) { |
1331 |
> |
threadUnexpectedException(t); |
1332 |
> |
return new byte[0]; |
1333 |
> |
} |
1334 |
> |
} |
1335 |
> |
|
1336 |
> |
@SuppressWarnings("unchecked") |
1337 |
> |
<T> T serialClone(T o) { |
1338 |
> |
try { |
1339 |
> |
ObjectInputStream ois = new ObjectInputStream |
1340 |
> |
(new ByteArrayInputStream(serialBytes(o))); |
1341 |
> |
T clone = (T) ois.readObject(); |
1342 |
> |
assertSame(o.getClass(), clone.getClass()); |
1343 |
> |
return clone; |
1344 |
> |
} catch (Throwable t) { |
1345 |
> |
threadUnexpectedException(t); |
1346 |
> |
return null; |
1347 |
> |
} |
1348 |
|
} |
554 |
– |
|
555 |
– |
|
1349 |
|
} |