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