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 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.io.*; |
13 |
|
|
14 |
< |
public class TimeUnitTest extends TestCase { |
13 |
< |
static public boolean DEBUG = false; |
14 |
< |
|
15 |
< |
|
14 |
> |
public class TimeUnitTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
20 |
|
return new TestSuite(TimeUnitTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
// (loops to 88888 check increments at all time divisions.) |
24 |
+ |
|
25 |
+ |
/** |
26 |
+ |
* convert correctly converts sample values across the units |
27 |
+ |
*/ |
28 |
|
public void testConvert() { |
29 |
< |
for (long t = 0; t < 10; ++t) { |
29 |
> |
for (long t = 0; t < 88888; ++t) { |
30 |
> |
assertEquals(t*60*60*24, |
31 |
> |
TimeUnit.SECONDS.convert(t, |
32 |
> |
TimeUnit.DAYS)); |
33 |
> |
assertEquals(t*60*60, |
34 |
> |
TimeUnit.SECONDS.convert(t, |
35 |
> |
TimeUnit.HOURS)); |
36 |
> |
assertEquals(t*60, |
37 |
> |
TimeUnit.SECONDS.convert(t, |
38 |
> |
TimeUnit.MINUTES)); |
39 |
|
assertEquals(t, |
40 |
|
TimeUnit.SECONDS.convert(t, |
41 |
|
TimeUnit.SECONDS)); |
42 |
|
assertEquals(t, |
43 |
< |
TimeUnit.SECONDS.convert(1000 * t, |
43 |
> |
TimeUnit.SECONDS.convert(1000L*t, |
44 |
|
TimeUnit.MILLISECONDS)); |
45 |
|
assertEquals(t, |
46 |
< |
TimeUnit.SECONDS.convert(1000000 * t, |
46 |
> |
TimeUnit.SECONDS.convert(1000000L*t, |
47 |
|
TimeUnit.MICROSECONDS)); |
48 |
|
assertEquals(t, |
49 |
< |
TimeUnit.SECONDS.convert(1000000000 * t, |
49 |
> |
TimeUnit.SECONDS.convert(1000000000L*t, |
50 |
|
TimeUnit.NANOSECONDS)); |
51 |
< |
assertEquals(1000 * t, |
51 |
> |
|
52 |
> |
|
53 |
> |
assertEquals(1000L*t*60*60*24, |
54 |
> |
TimeUnit.MILLISECONDS.convert(t, |
55 |
> |
TimeUnit.DAYS)); |
56 |
> |
assertEquals(1000L*t*60*60, |
57 |
> |
TimeUnit.MILLISECONDS.convert(t, |
58 |
> |
TimeUnit.HOURS)); |
59 |
> |
assertEquals(1000L*t*60, |
60 |
> |
TimeUnit.MILLISECONDS.convert(t, |
61 |
> |
TimeUnit.MINUTES)); |
62 |
> |
assertEquals(1000L*t, |
63 |
|
TimeUnit.MILLISECONDS.convert(t, |
64 |
|
TimeUnit.SECONDS)); |
65 |
|
assertEquals(t, |
66 |
|
TimeUnit.MILLISECONDS.convert(t, |
67 |
|
TimeUnit.MILLISECONDS)); |
68 |
|
assertEquals(t, |
69 |
< |
TimeUnit.MILLISECONDS.convert(1000 * t, |
69 |
> |
TimeUnit.MILLISECONDS.convert(1000L*t, |
70 |
|
TimeUnit.MICROSECONDS)); |
71 |
|
assertEquals(t, |
72 |
< |
TimeUnit.MILLISECONDS.convert(1000000 * t, |
72 |
> |
TimeUnit.MILLISECONDS.convert(1000000L*t, |
73 |
|
TimeUnit.NANOSECONDS)); |
74 |
< |
assertEquals(1000000 * t, |
74 |
> |
|
75 |
> |
assertEquals(1000000L*t*60*60*24, |
76 |
> |
TimeUnit.MICROSECONDS.convert(t, |
77 |
> |
TimeUnit.DAYS)); |
78 |
> |
assertEquals(1000000L*t*60*60, |
79 |
> |
TimeUnit.MICROSECONDS.convert(t, |
80 |
> |
TimeUnit.HOURS)); |
81 |
> |
assertEquals(1000000L*t*60, |
82 |
> |
TimeUnit.MICROSECONDS.convert(t, |
83 |
> |
TimeUnit.MINUTES)); |
84 |
> |
assertEquals(1000000L*t, |
85 |
|
TimeUnit.MICROSECONDS.convert(t, |
86 |
|
TimeUnit.SECONDS)); |
87 |
< |
assertEquals(1000 * t, |
87 |
> |
assertEquals(1000L*t, |
88 |
|
TimeUnit.MICROSECONDS.convert(t, |
89 |
|
TimeUnit.MILLISECONDS)); |
90 |
|
assertEquals(t, |
91 |
|
TimeUnit.MICROSECONDS.convert(t, |
92 |
|
TimeUnit.MICROSECONDS)); |
93 |
|
assertEquals(t, |
94 |
< |
TimeUnit.MICROSECONDS.convert(1000 * t, |
94 |
> |
TimeUnit.MICROSECONDS.convert(1000L*t, |
95 |
|
TimeUnit.NANOSECONDS)); |
96 |
< |
assertEquals(1000000000 * t, |
96 |
> |
|
97 |
> |
assertEquals(1000000000L*t*60*60*24, |
98 |
> |
TimeUnit.NANOSECONDS.convert(t, |
99 |
> |
TimeUnit.DAYS)); |
100 |
> |
assertEquals(1000000000L*t*60*60, |
101 |
> |
TimeUnit.NANOSECONDS.convert(t, |
102 |
> |
TimeUnit.HOURS)); |
103 |
> |
assertEquals(1000000000L*t*60, |
104 |
> |
TimeUnit.NANOSECONDS.convert(t, |
105 |
> |
TimeUnit.MINUTES)); |
106 |
> |
assertEquals(1000000000L*t, |
107 |
|
TimeUnit.NANOSECONDS.convert(t, |
108 |
|
TimeUnit.SECONDS)); |
109 |
< |
assertEquals(1000000 * t, |
109 |
> |
assertEquals(1000000L*t, |
110 |
|
TimeUnit.NANOSECONDS.convert(t, |
111 |
|
TimeUnit.MILLISECONDS)); |
112 |
< |
assertEquals(1000 * t, |
112 |
> |
assertEquals(1000L*t, |
113 |
|
TimeUnit.NANOSECONDS.convert(t, |
114 |
|
TimeUnit.MICROSECONDS)); |
115 |
|
assertEquals(t, |
118 |
|
} |
119 |
|
} |
120 |
|
|
121 |
+ |
/** |
122 |
+ |
* toNanos correctly converts sample values in different units to |
123 |
+ |
* nanoseconds |
124 |
+ |
*/ |
125 |
|
public void testToNanos() { |
126 |
< |
for (long t = 0; t < 10; ++t) { |
127 |
< |
assertEquals(1000000000 * t, |
126 |
> |
for (long t = 0; t < 88888; ++t) { |
127 |
> |
assertEquals(t*1000000000L*60*60*24, |
128 |
> |
TimeUnit.DAYS.toNanos(t)); |
129 |
> |
assertEquals(t*1000000000L*60*60, |
130 |
> |
TimeUnit.HOURS.toNanos(t)); |
131 |
> |
assertEquals(t*1000000000L*60, |
132 |
> |
TimeUnit.MINUTES.toNanos(t)); |
133 |
> |
assertEquals(1000000000L*t, |
134 |
|
TimeUnit.SECONDS.toNanos(t)); |
135 |
< |
|
82 |
< |
assertEquals(1000000 * t, |
135 |
> |
assertEquals(1000000L*t, |
136 |
|
TimeUnit.MILLISECONDS.toNanos(t)); |
137 |
< |
assertEquals(1000 * t, |
137 |
> |
assertEquals(1000L*t, |
138 |
|
TimeUnit.MICROSECONDS.toNanos(t)); |
139 |
|
assertEquals(t, |
140 |
< |
TimeUnit.SECONDS.NANOSECONDS.toNanos(t)); |
140 |
> |
TimeUnit.NANOSECONDS.toNanos(t)); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
+ |
/** |
145 |
+ |
* toMicros correctly converts sample values in different units to |
146 |
+ |
* microseconds |
147 |
+ |
*/ |
148 |
+ |
public void testToMicros() { |
149 |
+ |
for (long t = 0; t < 88888; ++t) { |
150 |
+ |
assertEquals(t*1000000L*60*60*24, |
151 |
+ |
TimeUnit.DAYS.toMicros(t)); |
152 |
+ |
assertEquals(t*1000000L*60*60, |
153 |
+ |
TimeUnit.HOURS.toMicros(t)); |
154 |
+ |
assertEquals(t*1000000L*60, |
155 |
+ |
TimeUnit.MINUTES.toMicros(t)); |
156 |
+ |
assertEquals(1000000L*t, |
157 |
+ |
TimeUnit.SECONDS.toMicros(t)); |
158 |
+ |
assertEquals(1000L*t, |
159 |
+ |
TimeUnit.MILLISECONDS.toMicros(t)); |
160 |
+ |
assertEquals(t, |
161 |
+ |
TimeUnit.MICROSECONDS.toMicros(t)); |
162 |
+ |
assertEquals(t, |
163 |
+ |
TimeUnit.NANOSECONDS.toMicros(t*1000L)); |
164 |
+ |
} |
165 |
+ |
} |
166 |
+ |
|
167 |
+ |
/** |
168 |
+ |
* toMillis correctly converts sample values in different units to |
169 |
+ |
* milliseconds |
170 |
+ |
*/ |
171 |
+ |
public void testToMillis() { |
172 |
+ |
for (long t = 0; t < 88888; ++t) { |
173 |
+ |
assertEquals(t*1000L*60*60*24, |
174 |
+ |
TimeUnit.DAYS.toMillis(t)); |
175 |
+ |
assertEquals(t*1000L*60*60, |
176 |
+ |
TimeUnit.HOURS.toMillis(t)); |
177 |
+ |
assertEquals(t*1000L*60, |
178 |
+ |
TimeUnit.MINUTES.toMillis(t)); |
179 |
+ |
assertEquals(1000L*t, |
180 |
+ |
TimeUnit.SECONDS.toMillis(t)); |
181 |
+ |
assertEquals(t, |
182 |
+ |
TimeUnit.MILLISECONDS.toMillis(t)); |
183 |
+ |
assertEquals(t, |
184 |
+ |
TimeUnit.MICROSECONDS.toMillis(t*1000L)); |
185 |
+ |
assertEquals(t, |
186 |
+ |
TimeUnit.NANOSECONDS.toMillis(t*1000000L)); |
187 |
+ |
} |
188 |
+ |
} |
189 |
|
|
190 |
+ |
/** |
191 |
+ |
* toSeconds correctly converts sample values in different units to |
192 |
+ |
* seconds |
193 |
+ |
*/ |
194 |
+ |
public void testToSeconds() { |
195 |
+ |
for (long t = 0; t < 88888; ++t) { |
196 |
+ |
assertEquals(t*60*60*24, |
197 |
+ |
TimeUnit.DAYS.toSeconds(t)); |
198 |
+ |
assertEquals(t*60*60, |
199 |
+ |
TimeUnit.HOURS.toSeconds(t)); |
200 |
+ |
assertEquals(t*60, |
201 |
+ |
TimeUnit.MINUTES.toSeconds(t)); |
202 |
+ |
assertEquals(t, |
203 |
+ |
TimeUnit.SECONDS.toSeconds(t)); |
204 |
+ |
assertEquals(t, |
205 |
+ |
TimeUnit.MILLISECONDS.toSeconds(t*1000L)); |
206 |
+ |
assertEquals(t, |
207 |
+ |
TimeUnit.MICROSECONDS.toSeconds(t*1000000L)); |
208 |
+ |
assertEquals(t, |
209 |
+ |
TimeUnit.NANOSECONDS.toSeconds(t*1000000000L)); |
210 |
+ |
} |
211 |
+ |
} |
212 |
+ |
|
213 |
+ |
/** |
214 |
+ |
* toMinutes correctly converts sample values in different units to |
215 |
+ |
* minutes |
216 |
+ |
*/ |
217 |
+ |
public void testToMinutes() { |
218 |
+ |
for (long t = 0; t < 88888; ++t) { |
219 |
+ |
assertEquals(t*60*24, |
220 |
+ |
TimeUnit.DAYS.toMinutes(t)); |
221 |
+ |
assertEquals(t*60, |
222 |
+ |
TimeUnit.HOURS.toMinutes(t)); |
223 |
+ |
assertEquals(t, |
224 |
+ |
TimeUnit.MINUTES.toMinutes(t)); |
225 |
+ |
assertEquals(t, |
226 |
+ |
TimeUnit.SECONDS.toMinutes(t*60)); |
227 |
+ |
assertEquals(t, |
228 |
+ |
TimeUnit.MILLISECONDS.toMinutes(t*1000L*60)); |
229 |
+ |
assertEquals(t, |
230 |
+ |
TimeUnit.MICROSECONDS.toMinutes(t*1000000L*60)); |
231 |
+ |
assertEquals(t, |
232 |
+ |
TimeUnit.NANOSECONDS.toMinutes(t*1000000000L*60)); |
233 |
+ |
} |
234 |
+ |
} |
235 |
+ |
|
236 |
+ |
/** |
237 |
+ |
* toHours correctly converts sample values in different units to |
238 |
+ |
* hours |
239 |
+ |
*/ |
240 |
+ |
public void testToHours() { |
241 |
+ |
for (long t = 0; t < 88888; ++t) { |
242 |
+ |
assertEquals(t*24, |
243 |
+ |
TimeUnit.DAYS.toHours(t)); |
244 |
+ |
assertEquals(t, |
245 |
+ |
TimeUnit.HOURS.toHours(t)); |
246 |
+ |
assertEquals(t, |
247 |
+ |
TimeUnit.MINUTES.toHours(t*60)); |
248 |
+ |
assertEquals(t, |
249 |
+ |
TimeUnit.SECONDS.toHours(t*60*60)); |
250 |
+ |
assertEquals(t, |
251 |
+ |
TimeUnit.MILLISECONDS.toHours(t*1000L*60*60)); |
252 |
+ |
assertEquals(t, |
253 |
+ |
TimeUnit.MICROSECONDS.toHours(t*1000000L*60*60)); |
254 |
+ |
assertEquals(t, |
255 |
+ |
TimeUnit.NANOSECONDS.toHours(t*1000000000L*60*60)); |
256 |
+ |
} |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
/** |
260 |
+ |
* toDays correctly converts sample values in different units to |
261 |
+ |
* days |
262 |
+ |
*/ |
263 |
+ |
public void testToDays() { |
264 |
+ |
for (long t = 0; t < 88888; ++t) { |
265 |
+ |
assertEquals(t, |
266 |
+ |
TimeUnit.DAYS.toDays(t)); |
267 |
+ |
assertEquals(t, |
268 |
+ |
TimeUnit.HOURS.toDays(t*24)); |
269 |
+ |
assertEquals(t, |
270 |
+ |
TimeUnit.MINUTES.toDays(t*60*24)); |
271 |
+ |
assertEquals(t, |
272 |
+ |
TimeUnit.SECONDS.toDays(t*60*60*24)); |
273 |
+ |
assertEquals(t, |
274 |
+ |
TimeUnit.MILLISECONDS.toDays(t*1000L*60*60*24)); |
275 |
+ |
assertEquals(t, |
276 |
+ |
TimeUnit.MICROSECONDS.toDays(t*1000000L*60*60*24)); |
277 |
+ |
assertEquals(t, |
278 |
+ |
TimeUnit.NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
279 |
+ |
} |
280 |
+ |
} |
281 |
+ |
|
282 |
+ |
|
283 |
+ |
/** |
284 |
+ |
* convert saturates positive too-large values to Long.MAX_VALUE |
285 |
+ |
* and negative to LONG.MIN_VALUE |
286 |
+ |
*/ |
287 |
|
public void testConvertSaturate() { |
288 |
|
assertEquals(Long.MAX_VALUE, |
289 |
|
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
291 |
|
assertEquals(Long.MIN_VALUE, |
292 |
|
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
293 |
|
TimeUnit.SECONDS)); |
294 |
+ |
assertEquals(Long.MAX_VALUE, |
295 |
+ |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
296 |
+ |
TimeUnit.MINUTES)); |
297 |
+ |
assertEquals(Long.MIN_VALUE, |
298 |
+ |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
299 |
+ |
TimeUnit.MINUTES)); |
300 |
+ |
assertEquals(Long.MAX_VALUE, |
301 |
+ |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
302 |
+ |
TimeUnit.HOURS)); |
303 |
+ |
assertEquals(Long.MIN_VALUE, |
304 |
+ |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
305 |
+ |
TimeUnit.HOURS)); |
306 |
+ |
assertEquals(Long.MAX_VALUE, |
307 |
+ |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
308 |
+ |
TimeUnit.DAYS)); |
309 |
+ |
assertEquals(Long.MIN_VALUE, |
310 |
+ |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
311 |
+ |
TimeUnit.DAYS)); |
312 |
|
|
313 |
|
} |
314 |
|
|
315 |
+ |
/** |
316 |
+ |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
317 |
+ |
* and negative to LONG.MIN_VALUE |
318 |
+ |
*/ |
319 |
|
public void testToNanosSaturate() { |
320 |
|
assertEquals(Long.MAX_VALUE, |
321 |
|
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
322 |
|
assertEquals(Long.MIN_VALUE, |
323 |
|
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
107 |
– |
|
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
+ |
/** |
328 |
+ |
* toString returns string containing common name of unit |
329 |
+ |
*/ |
330 |
|
public void testToString() { |
331 |
|
String s = TimeUnit.SECONDS.toString(); |
332 |
< |
assertTrue(s.indexOf("econd") >= 0); |
332 |
> |
assertTrue(s.indexOf("ECOND") >= 0); |
333 |
|
} |
334 |
|
|
116 |
– |
// Exception tests |
335 |
|
|
336 |
|
/** |
337 |
< |
* This test specifically to catch the unreported exception |
337 |
> |
* Timed wait without holding lock throws |
338 |
> |
* IllegalMonitorStateException |
339 |
|
*/ |
340 |
< |
public void testTimedWaitForUnreportedIllegalMonitorException() { |
340 |
> |
public void testTimedWait_IllegalMonitorException() { |
341 |
|
//created a new thread with anonymous runnable |
342 |
|
|
343 |
|
Thread t = new Thread(new Runnable() { |
345 |
|
Object o = new Object(); |
346 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
347 |
|
try { |
348 |
< |
tu.timedWait(o,40000); |
349 |
< |
fail("should throw"); |
348 |
> |
tu.timedWait(o,LONG_DELAY_MS); |
349 |
> |
threadShouldThrow(); |
350 |
|
} |
351 |
|
catch (InterruptedException ie) { |
352 |
< |
fail("should not throw IE here"); |
352 |
> |
threadUnexpectedException(); |
353 |
|
} |
354 |
|
catch(IllegalMonitorStateException success) { |
355 |
|
} |
358 |
|
}); |
359 |
|
t.start(); |
360 |
|
try { |
361 |
< |
Thread.sleep(100); |
361 |
> |
Thread.sleep(SHORT_DELAY_MS); |
362 |
|
t.interrupt(); |
363 |
|
t.join(); |
364 |
|
} catch(Exception e) { |
365 |
< |
fail("Unexpected exception"); |
365 |
> |
unexpectedException(); |
366 |
|
} |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* Test to verify that timedWait will throw InterruptedException. |
152 |
< |
* Thread t waits on timedWait while the main thread interrupts it. |
153 |
< |
* Note: This does not throw IllegalMonitorException since timeWait |
154 |
< |
* is synchronized on o |
370 |
> |
* timedWait throws InterruptedException when interrupted |
371 |
|
*/ |
372 |
|
public void testTimedWait() { |
373 |
|
Thread t = new Thread(new Runnable() { |
377 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
378 |
|
try { |
379 |
|
synchronized(o) { |
380 |
< |
tu.timedWait(o,1000); |
380 |
> |
tu.timedWait(o,MEDIUM_DELAY_MS); |
381 |
|
} |
382 |
< |
fail("should throw"); |
382 |
> |
threadShouldThrow(); |
383 |
|
} |
384 |
|
catch(InterruptedException success) {} |
385 |
|
catch(IllegalMonitorStateException failure) { |
386 |
< |
fail("should not throw"); |
386 |
> |
threadUnexpectedException(); |
387 |
|
} |
388 |
|
} |
389 |
|
}); |
390 |
|
t.start(); |
391 |
|
try { |
392 |
< |
Thread.sleep(100); |
392 |
> |
Thread.sleep(SHORT_DELAY_MS); |
393 |
|
t.interrupt(); |
394 |
|
t.join(); |
395 |
|
} catch(Exception e) { |
396 |
< |
fail("Unexpected exception"); |
396 |
> |
unexpectedException(); |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
|
/** |
402 |
< |
* Test to verify that timedJoin will throw InterruptedException. |
187 |
< |
* Thread t waits on timedJoin while the main thread interrupts it. |
402 |
> |
* timedJoin throws InterruptedException when interrupted |
403 |
|
*/ |
404 |
|
public void testTimedJoin() { |
405 |
|
Thread t = new Thread(new Runnable() { |
408 |
|
try { |
409 |
|
Thread s = new Thread(new Runnable() { |
410 |
|
public void run() { |
411 |
< |
try{ |
412 |
< |
Thread.sleep(1000); |
413 |
< |
}catch(InterruptedException success){} |
411 |
> |
try { |
412 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
413 |
> |
} catch(InterruptedException success){} |
414 |
|
} |
415 |
|
}); |
416 |
|
s.start(); |
417 |
< |
tu.timedJoin(s,1000); |
418 |
< |
fail("should throw"); |
417 |
> |
tu.timedJoin(s,MEDIUM_DELAY_MS); |
418 |
> |
threadShouldThrow(); |
419 |
|
} |
420 |
|
catch(Exception e) {} |
421 |
|
} |
422 |
|
}); |
423 |
|
t.start(); |
424 |
|
try { |
425 |
< |
Thread.sleep(100); |
425 |
> |
Thread.sleep(SHORT_DELAY_MS); |
426 |
|
t.interrupt(); |
427 |
|
t.join(); |
428 |
|
} catch(Exception e) { |
429 |
< |
fail("Unexpected exception"); |
429 |
> |
unexpectedException(); |
430 |
|
} |
431 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
< |
* Test to verify that timedSleep will throw InterruptedException. |
220 |
< |
* Thread t waits on timedSleep while the main thread interrupts it. |
434 |
> |
* timedSleep throws InterruptedException when interrupted |
435 |
|
*/ |
436 |
|
public void testTimedSleep() { |
437 |
|
//created a new thread with anonymous runnable |
440 |
|
public void run() { |
441 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
442 |
|
try { |
443 |
< |
tu.sleep(1000); |
444 |
< |
fail("should throw"); |
443 |
> |
tu.sleep(MEDIUM_DELAY_MS); |
444 |
> |
threadShouldThrow(); |
445 |
|
} |
446 |
|
catch(InterruptedException success) {} |
447 |
|
} |
448 |
|
}); |
449 |
|
t.start(); |
450 |
|
try { |
451 |
< |
Thread.sleep(100); |
451 |
> |
Thread.sleep(SHORT_DELAY_MS); |
452 |
|
t.interrupt(); |
453 |
|
t.join(); |
454 |
|
} catch(Exception e) { |
455 |
< |
fail("Unexpected exception"); |
455 |
> |
unexpectedException(); |
456 |
|
} |
457 |
|
} |
458 |
+ |
|
459 |
+ |
/** |
460 |
+ |
* a deserialized serialized unit is equal |
461 |
+ |
*/ |
462 |
+ |
public void testSerialization() { |
463 |
+ |
TimeUnit q = TimeUnit.MILLISECONDS; |
464 |
+ |
|
465 |
+ |
try { |
466 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
467 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
468 |
+ |
out.writeObject(q); |
469 |
+ |
out.close(); |
470 |
+ |
|
471 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
472 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
473 |
+ |
TimeUnit r = (TimeUnit)in.readObject(); |
474 |
+ |
|
475 |
+ |
assertEquals(q.toString(), r.toString()); |
476 |
+ |
} catch(Exception e){ |
477 |
+ |
e.printStackTrace(); |
478 |
+ |
unexpectedException(); |
479 |
+ |
} |
480 |
+ |
} |
481 |
+ |
|
482 |
|
} |