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