41 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
42 |
|
* is always discriminable as larger than SHORT and smaller than |
43 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
44 |
< |
* (100, 500, 1000, 5000 MS) but even so, if there is ever any doubt, |
45 |
< |
* they can all be increased in one spot to rerun tests on slower |
46 |
< |
* platforms</li> |
44 |
> |
* but even so, if there is ever any doubt, they can all be increased |
45 |
> |
* in one spot to rerun tests on slower platforms</li> |
46 |
|
* |
47 |
|
* <li> All threads generated must be joined inside each test case |
48 |
|
* method (or <tt>fail</tt> to do so) before returning from the |
53 |
|
*/ |
54 |
|
public class JSR166TestCase extends TestCase { |
55 |
|
|
56 |
+ |
public static long SHORT_DELAY_MS; |
57 |
+ |
public static long SMALL_DELAY_MS; |
58 |
+ |
public static long MEDIUM_DELAY_MS; |
59 |
+ |
public static long LONG_DELAY_MS; |
60 |
+ |
|
61 |
+ |
|
62 |
+ |
/** |
63 |
+ |
* Return the shortest timed delay. This could |
64 |
+ |
* be reimplmented to use for example a Property. |
65 |
+ |
*/ |
66 |
+ |
protected long getShortDelay() { |
67 |
+ |
return 50; |
68 |
+ |
} |
69 |
+ |
|
70 |
+ |
|
71 |
+ |
/** |
72 |
+ |
* Set delays as multiples fo SHORT_DELAY. |
73 |
+ |
*/ |
74 |
+ |
protected void setDelays() { |
75 |
+ |
SHORT_DELAY_MS = getShortDelay(); |
76 |
+ |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
77 |
+ |
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
78 |
+ |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
79 |
+ |
} |
80 |
+ |
|
81 |
|
/** |
82 |
|
* Flag set true if any threadAssert methods fail |
83 |
|
*/ |
87 |
|
* Initialize test to indicat that no thread assertions have failed |
88 |
|
*/ |
89 |
|
public void setUp() { |
90 |
+ |
setDelays(); |
91 |
|
threadFailed = false; |
92 |
|
} |
93 |
|
|
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
+ |
public void threadShouldThrow() { |
138 |
+ |
threadFailed = true; |
139 |
+ |
fail("should throw exception"); |
140 |
+ |
} |
141 |
+ |
|
142 |
+ |
public void threadUnexpectedException() { |
143 |
+ |
threadFailed = true; |
144 |
+ |
fail("Unexpected exception"); |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
|
148 |
|
/** |
149 |
|
* Wait out termination of a thread pool or fail doing so |
150 |
|
*/ |
153 |
|
exec.shutdown(); |
154 |
|
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
155 |
|
} catch(InterruptedException ie) { |
156 |
< |
fail("unexpected exception"); |
156 |
> |
fail("Unexpected exception"); |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
< |
public static final long SHORT_DELAY_MS = 100; |
161 |
< |
public static final long SMALL_DELAY_MS = 500; |
162 |
< |
public static final long MEDIUM_DELAY_MS = 1000; |
163 |
< |
public static final long LONG_DELAY_MS = 5000; |
160 |
> |
|
161 |
> |
public void shouldThrow() { |
162 |
> |
fail("Should throw exception"); |
163 |
> |
} |
164 |
> |
|
165 |
> |
public void unexpectedException() { |
166 |
> |
fail("Unexpected exception"); |
167 |
> |
} |
168 |
|
|
169 |
|
|
170 |
|
/** |
208 |
|
Thread.sleep(SHORT_DELAY_MS); |
209 |
|
} |
210 |
|
catch(Exception e) { |
211 |
< |
threadFail("unexpectedException"); |
211 |
> |
threadUnexpectedException(); |
212 |
|
} |
213 |
|
} |
214 |
|
} |
217 |
|
public void run() { |
218 |
|
try { |
219 |
|
Thread.sleep(SHORT_DELAY_MS); |
220 |
< |
threadFail("should throw IE"); |
220 |
> |
threadShouldThrow(); |
221 |
|
} |
222 |
|
catch(InterruptedException success) { |
223 |
|
} |
230 |
|
Thread.sleep(SMALL_DELAY_MS); |
231 |
|
} |
232 |
|
catch(Exception e) { |
233 |
< |
threadFail("unexpectedException"); |
233 |
> |
threadUnexpectedException(); |
234 |
|
} |
235 |
|
} |
236 |
|
} |
241 |
|
Thread.sleep(SMALL_DELAY_MS); |
242 |
|
} |
243 |
|
catch(Exception e) { |
244 |
< |
threadFail("unexpectedException"); |
244 |
> |
threadUnexpectedException(); |
245 |
|
} |
246 |
|
return Boolean.TRUE; |
247 |
|
} |
251 |
|
public void run() { |
252 |
|
try { |
253 |
|
Thread.sleep(SMALL_DELAY_MS); |
254 |
< |
threadFail("should throw IE"); |
254 |
> |
threadShouldThrow(); |
255 |
|
} |
256 |
|
catch(InterruptedException success) { |
257 |
|
} |
265 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
266 |
|
} |
267 |
|
catch(Exception e) { |
268 |
< |
threadFail("unexpectedException"); |
268 |
> |
threadUnexpectedException(); |
269 |
|
} |
270 |
|
} |
271 |
|
} |
274 |
|
public void run() { |
275 |
|
try { |
276 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
277 |
< |
threadFail("should throw IE"); |
277 |
> |
threadShouldThrow(); |
278 |
|
} |
279 |
|
catch(InterruptedException success) { |
280 |
|
} |