12 |
|
|
13 |
|
|
14 |
|
/** |
15 |
< |
* Base class for JSR166 Junit TCK tests. Defines some constants and |
16 |
< |
* utility methods, as well as a simple framework for helping to make |
17 |
< |
* sure that assertions failing in generated threads cause the |
18 |
< |
* associated test that generated them to itself fail (which JUnit doe |
19 |
< |
* not otherwise arrange). The rules for creating such tests are: |
15 |
> |
* Base class for JSR166 Junit TCK tests. Defines some constants, |
16 |
> |
* utility methods and classes, as well as a simple framework for |
17 |
> |
* helping to make sure that assertions failing in generated threads |
18 |
> |
* cause the associated test that generated them to itself fail (which |
19 |
> |
* JUnit doe not otherwise arrange). The rules for creating such |
20 |
> |
* tests are: |
21 |
|
* |
22 |
|
* <ol> |
23 |
|
* |
37 |
|
* <li>All delays and timeouts must use one of the constants {@link |
38 |
|
* SHORT_DELAY_MS}, {@link SMALL_DELAY_MS}, {@link MEDIUM_DELAY_MS}, |
39 |
|
* {@link LONG_DELAY_MS}. The idea here is that a SHORT is always |
40 |
< |
* discriminatable from zero time, and always allows enough time for |
41 |
< |
* the small amounts of computation (creating a thread, calling a few |
40 |
> |
* discriminable from zero time, and always allows enough time for the |
41 |
> |
* small amounts of computation (creating a thread, calling a few |
42 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
43 |
|
* is always discriminable as larger than SHORT and smaller than |
44 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
65 |
|
* be reimplmented to use for example a Property. |
66 |
|
*/ |
67 |
|
protected long getShortDelay() { |
68 |
< |
return 50; |
68 |
> |
return 100; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
/** |
73 |
< |
* Set delays as multiples fo SHORT_DELAY. |
73 |
> |
* Set delays as multiples of SHORT_DELAY. |
74 |
|
*/ |
75 |
|
protected void setDelays() { |
76 |
|
SHORT_DELAY_MS = getShortDelay(); |
82 |
|
/** |
83 |
|
* Flag set true if any threadAssert methods fail |
84 |
|
*/ |
85 |
< |
protected volatile boolean threadFailed; |
85 |
> |
volatile boolean threadFailed; |
86 |
|
|
87 |
|
/** |
88 |
< |
* Initialize test to indicat that no thread assertions have failed |
88 |
> |
* Initialize test to indicate that no thread assertions have failed |
89 |
|
*/ |
90 |
|
public void setUp() { |
91 |
|
setDelays(); |
99 |
|
assertFalse(threadFailed); |
100 |
|
} |
101 |
|
|
102 |
+ |
/** |
103 |
+ |
* Fail, also setting status to indicate current testcase should fail |
104 |
+ |
*/ |
105 |
|
public void threadFail(String reason) { |
106 |
|
threadFailed = true; |
107 |
|
fail(reason); |
108 |
|
} |
109 |
|
|
110 |
+ |
/** |
111 |
+ |
* If expression not true, set status to indicate current testcase |
112 |
+ |
* should fail |
113 |
+ |
*/ |
114 |
|
public void threadAssertTrue(boolean b) { |
115 |
|
if (!b) { |
116 |
|
threadFailed = true; |
117 |
|
assertTrue(b); |
118 |
|
} |
119 |
|
} |
120 |
+ |
|
121 |
+ |
/** |
122 |
+ |
* If expression not false, set status to indicate current testcase |
123 |
+ |
* should fail |
124 |
+ |
*/ |
125 |
|
public void threadAssertFalse(boolean b) { |
126 |
|
if (b) { |
127 |
|
threadFailed = true; |
128 |
|
assertFalse(b); |
129 |
|
} |
130 |
|
} |
131 |
+ |
|
132 |
+ |
/** |
133 |
+ |
* If argument not null, set status to indicate current testcase |
134 |
+ |
* should fail |
135 |
+ |
*/ |
136 |
|
public void threadAssertNull(Object x) { |
137 |
|
if (x != null) { |
138 |
|
threadFailed = true; |
139 |
|
assertNull(x); |
140 |
|
} |
141 |
|
} |
142 |
+ |
|
143 |
+ |
/** |
144 |
+ |
* If arguments not equal, set status to indicate current testcase |
145 |
+ |
* should fail |
146 |
+ |
*/ |
147 |
|
public void threadAssertEquals(long x, long y) { |
148 |
|
if (x != y) { |
149 |
|
threadFailed = true; |
150 |
|
assertEquals(x, y); |
151 |
|
} |
152 |
|
} |
153 |
+ |
|
154 |
+ |
/** |
155 |
+ |
* If arguments not equal, set status to indicate current testcase |
156 |
+ |
* should fail |
157 |
+ |
*/ |
158 |
|
public void threadAssertEquals(Object x, Object y) { |
159 |
|
if (x != y && (x == null || !x.equals(y))) { |
160 |
|
threadFailed = true; |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
+ |
* threadFail with message "should throw exception" |
167 |
+ |
*/ |
168 |
+ |
public void threadShouldThrow() { |
169 |
+ |
threadFailed = true; |
170 |
+ |
fail("should throw exception"); |
171 |
+ |
} |
172 |
+ |
|
173 |
+ |
/** |
174 |
+ |
* threadFail with message "Unexpected exception" |
175 |
+ |
*/ |
176 |
+ |
public void threadUnexpectedException() { |
177 |
+ |
threadFailed = true; |
178 |
+ |
fail("Unexpected exception"); |
179 |
+ |
} |
180 |
+ |
|
181 |
+ |
|
182 |
+ |
/** |
183 |
|
* Wait out termination of a thread pool or fail doing so |
184 |
|
*/ |
185 |
|
public void joinPool(ExecutorService exec) { |
187 |
|
exec.shutdown(); |
188 |
|
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
189 |
|
} catch(InterruptedException ie) { |
190 |
< |
fail("unexpected exception"); |
190 |
> |
fail("Unexpected exception"); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
+ |
/** |
196 |
+ |
* fail with message "should throw exception" |
197 |
+ |
*/ |
198 |
+ |
public void shouldThrow() { |
199 |
+ |
fail("Should throw exception"); |
200 |
+ |
} |
201 |
+ |
|
202 |
+ |
/** |
203 |
+ |
* fail with message "Unexpected exception" |
204 |
+ |
*/ |
205 |
+ |
public void unexpectedException() { |
206 |
+ |
fail("Unexpected exception"); |
207 |
+ |
} |
208 |
+ |
|
209 |
|
|
210 |
|
/** |
211 |
|
* The number of elements to place in collections, arrays, etc. |
212 |
|
*/ |
213 |
< |
public static final int SIZE = 20; |
213 |
> |
static final int SIZE = 20; |
214 |
|
|
215 |
|
// Some convenient Integer constants |
216 |
|
|
217 |
< |
public static final Integer zero = new Integer(0); |
218 |
< |
public static final Integer one = new Integer(1); |
219 |
< |
public static final Integer two = new Integer(2); |
220 |
< |
public static final Integer three = new Integer(3); |
221 |
< |
public static final Integer four = new Integer(4); |
222 |
< |
public static final Integer five = new Integer(5); |
223 |
< |
public static final Integer six = new Integer(6); |
224 |
< |
public static final Integer seven = new Integer(7); |
225 |
< |
public static final Integer eight = new Integer(8); |
226 |
< |
public static final Integer nine = new Integer(9); |
227 |
< |
public static final Integer m1 = new Integer(-1); |
228 |
< |
public static final Integer m2 = new Integer(-2); |
229 |
< |
public static final Integer m3 = new Integer(-3); |
230 |
< |
public static final Integer m4 = new Integer(-4); |
231 |
< |
public static final Integer m5 = new Integer(-5); |
232 |
< |
public static final Integer m10 = new Integer(-10); |
217 |
> |
static final Integer zero = new Integer(0); |
218 |
> |
static final Integer one = new Integer(1); |
219 |
> |
static final Integer two = new Integer(2); |
220 |
> |
static final Integer three = new Integer(3); |
221 |
> |
static final Integer four = new Integer(4); |
222 |
> |
static final Integer five = new Integer(5); |
223 |
> |
static final Integer six = new Integer(6); |
224 |
> |
static final Integer seven = new Integer(7); |
225 |
> |
static final Integer eight = new Integer(8); |
226 |
> |
static final Integer nine = new Integer(9); |
227 |
> |
static final Integer m1 = new Integer(-1); |
228 |
> |
static final Integer m2 = new Integer(-2); |
229 |
> |
static final Integer m3 = new Integer(-3); |
230 |
> |
static final Integer m4 = new Integer(-4); |
231 |
> |
static final Integer m5 = new Integer(-5); |
232 |
> |
static final Integer m10 = new Integer(-10); |
233 |
|
|
234 |
|
|
235 |
|
// Some convenient Runnable classes |
236 |
|
|
237 |
< |
public static class NoOpRunnable implements Runnable { |
237 |
> |
static class NoOpRunnable implements Runnable { |
238 |
|
public void run() {} |
239 |
|
} |
240 |
|
|
241 |
< |
public static class NoOpCallable implements Callable { |
241 |
> |
static class NoOpCallable implements Callable { |
242 |
|
public Object call() { return Boolean.TRUE; } |
243 |
|
} |
244 |
|
|
245 |
< |
public class ShortRunnable implements Runnable { |
245 |
> |
class ShortRunnable implements Runnable { |
246 |
|
public void run() { |
247 |
|
try { |
248 |
|
Thread.sleep(SHORT_DELAY_MS); |
249 |
|
} |
250 |
|
catch(Exception e) { |
251 |
< |
threadFail("unexpectedException"); |
251 |
> |
threadUnexpectedException(); |
252 |
|
} |
253 |
|
} |
254 |
|
} |
255 |
|
|
256 |
< |
public class ShortInterruptedRunnable implements Runnable { |
256 |
> |
class ShortInterruptedRunnable implements Runnable { |
257 |
|
public void run() { |
258 |
|
try { |
259 |
|
Thread.sleep(SHORT_DELAY_MS); |
260 |
< |
threadFail("should throw IE"); |
260 |
> |
threadShouldThrow(); |
261 |
|
} |
262 |
|
catch(InterruptedException success) { |
263 |
|
} |
264 |
|
} |
265 |
|
} |
266 |
|
|
267 |
< |
public class SmallRunnable implements Runnable { |
267 |
> |
class SmallRunnable implements Runnable { |
268 |
|
public void run() { |
269 |
|
try { |
270 |
|
Thread.sleep(SMALL_DELAY_MS); |
271 |
|
} |
272 |
|
catch(Exception e) { |
273 |
< |
threadFail("unexpectedException"); |
273 |
> |
threadUnexpectedException(); |
274 |
|
} |
275 |
|
} |
276 |
|
} |
277 |
|
|
278 |
< |
public class SmallCallable implements Callable { |
278 |
> |
class SmallCallable implements Callable { |
279 |
|
public Object call() { |
280 |
|
try { |
281 |
|
Thread.sleep(SMALL_DELAY_MS); |
282 |
|
} |
283 |
|
catch(Exception e) { |
284 |
< |
threadFail("unexpectedException"); |
284 |
> |
threadUnexpectedException(); |
285 |
|
} |
286 |
|
return Boolean.TRUE; |
287 |
|
} |
288 |
|
} |
289 |
|
|
290 |
< |
public class SmallInterruptedRunnable implements Runnable { |
290 |
> |
class SmallInterruptedRunnable implements Runnable { |
291 |
|
public void run() { |
292 |
|
try { |
293 |
|
Thread.sleep(SMALL_DELAY_MS); |
294 |
< |
threadFail("should throw IE"); |
294 |
> |
threadShouldThrow(); |
295 |
|
} |
296 |
|
catch(InterruptedException success) { |
297 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
< |
public class MediumRunnable implements Runnable { |
302 |
> |
class MediumRunnable implements Runnable { |
303 |
|
public void run() { |
304 |
|
try { |
305 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
306 |
|
} |
307 |
|
catch(Exception e) { |
308 |
< |
threadFail("unexpectedException"); |
308 |
> |
threadUnexpectedException(); |
309 |
|
} |
310 |
|
} |
311 |
|
} |
312 |
|
|
313 |
< |
public class MediumInterruptedRunnable implements Runnable { |
313 |
> |
class MediumInterruptedRunnable implements Runnable { |
314 |
|
public void run() { |
315 |
|
try { |
316 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
317 |
< |
threadFail("should throw IE"); |
317 |
> |
threadShouldThrow(); |
318 |
|
} |
319 |
|
catch(InterruptedException success) { |
320 |
|
} |
321 |
|
} |
322 |
|
} |
323 |
|
|
324 |
< |
public class MediumPossiblyInterruptedRunnable implements Runnable { |
324 |
> |
class MediumPossiblyInterruptedRunnable implements Runnable { |
325 |
|
public void run() { |
326 |
|
try { |
327 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
330 |
|
} |
331 |
|
} |
332 |
|
} |
333 |
+ |
|
334 |
+ |
/** |
335 |
+ |
* For use as ThreadFactory in constructors |
336 |
+ |
*/ |
337 |
+ |
static class SimpleThreadFactory implements ThreadFactory{ |
338 |
+ |
public Thread newThread(Runnable r){ |
339 |
+ |
return new Thread(r); |
340 |
+ |
} |
341 |
+ |
} |
342 |
+ |
|
343 |
+ |
static class TrackedRunnable implements Runnable { |
344 |
+ |
volatile boolean done = false; |
345 |
+ |
public void run() { |
346 |
+ |
try { |
347 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
348 |
+ |
done = true; |
349 |
+ |
} catch(Exception e){ |
350 |
+ |
} |
351 |
+ |
} |
352 |
+ |
} |
353 |
+ |
|
354 |
+ |
static class TrackedCallable implements Callable { |
355 |
+ |
volatile boolean done = false; |
356 |
+ |
public Object call() { |
357 |
+ |
try { |
358 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
359 |
+ |
done = true; |
360 |
+ |
} catch(Exception e){ |
361 |
+ |
} |
362 |
+ |
return Boolean.TRUE; |
363 |
+ |
} |
364 |
+ |
} |
365 |
+ |
|
366 |
+ |
/** |
367 |
+ |
* For use as RejectedExecutionHandler in constructors |
368 |
+ |
*/ |
369 |
+ |
static class NoOpREHandler implements RejectedExecutionHandler{ |
370 |
+ |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
371 |
+ |
} |
372 |
+ |
|
373 |
|
|
374 |
|
} |