13 |
|
|
14 |
|
public class TimeUnitTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run(suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
|
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(TimeUnitTest.class); |
20 |
> |
return new TestSuite(TimeUnitTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
// (loops to 88888 check increments at all time divisions.) |
309 |
|
assertEquals(Long.MIN_VALUE, |
310 |
|
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
311 |
|
TimeUnit.DAYS)); |
312 |
– |
|
312 |
|
} |
313 |
|
|
314 |
|
/** |
316 |
|
* and negative to LONG.MIN_VALUE |
317 |
|
*/ |
318 |
|
public void testToNanosSaturate() { |
319 |
< |
assertEquals(Long.MAX_VALUE, |
320 |
< |
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
321 |
< |
assertEquals(Long.MIN_VALUE, |
322 |
< |
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
319 |
> |
assertEquals(Long.MAX_VALUE, |
320 |
> |
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
321 |
> |
assertEquals(Long.MIN_VALUE, |
322 |
> |
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
323 |
|
} |
324 |
|
|
325 |
|
|
336 |
|
* Timed wait without holding lock throws |
337 |
|
* IllegalMonitorStateException |
338 |
|
*/ |
339 |
< |
public void testTimedWait_IllegalMonitorException() { |
340 |
< |
//created a new thread with anonymous runnable |
341 |
< |
|
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
public void run() { |
344 |
< |
Object o = new Object(); |
345 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
346 |
< |
try { |
347 |
< |
tu.timedWait(o,LONG_DELAY_MS); |
349 |
< |
threadShouldThrow(); |
350 |
< |
} |
351 |
< |
catch (InterruptedException ie) { |
352 |
< |
threadUnexpectedException(); |
353 |
< |
} |
354 |
< |
catch (IllegalMonitorStateException success) { |
355 |
< |
} |
339 |
> |
public void testTimedWait_IllegalMonitorException() throws Exception { |
340 |
> |
Thread t = new Thread(new CheckedRunnable() { |
341 |
> |
public void realRun() throws InterruptedException { |
342 |
> |
Object o = new Object(); |
343 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
344 |
> |
try { |
345 |
> |
tu.timedWait(o,LONG_DELAY_MS); |
346 |
> |
threadShouldThrow(); |
347 |
> |
} catch (IllegalMonitorStateException success) {}}}); |
348 |
|
|
357 |
– |
} |
358 |
– |
}); |
349 |
|
t.start(); |
350 |
< |
try { |
351 |
< |
Thread.sleep(SHORT_DELAY_MS); |
352 |
< |
t.interrupt(); |
363 |
< |
t.join(); |
364 |
< |
} catch (Exception e) { |
365 |
< |
unexpectedException(); |
366 |
< |
} |
350 |
> |
Thread.sleep(SHORT_DELAY_MS); |
351 |
> |
t.interrupt(); |
352 |
> |
t.join(); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* timedWait throws InterruptedException when interrupted |
357 |
|
*/ |
358 |
< |
public void testTimedWait() { |
359 |
< |
Thread t = new Thread(new Runnable() { |
360 |
< |
public void run() { |
361 |
< |
Object o = new Object(); |
362 |
< |
|
363 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
364 |
< |
try { |
365 |
< |
synchronized(o) { |
366 |
< |
tu.timedWait(o,MEDIUM_DELAY_MS); |
367 |
< |
} |
368 |
< |
threadShouldThrow(); |
369 |
< |
} |
370 |
< |
catch (InterruptedException success) {} |
371 |
< |
catch (IllegalMonitorStateException failure) { |
386 |
< |
threadUnexpectedException(); |
387 |
< |
} |
388 |
< |
} |
389 |
< |
}); |
390 |
< |
t.start(); |
391 |
< |
try { |
392 |
< |
Thread.sleep(SHORT_DELAY_MS); |
393 |
< |
t.interrupt(); |
394 |
< |
t.join(); |
395 |
< |
} catch (Exception e) { |
396 |
< |
unexpectedException(); |
397 |
< |
} |
358 |
> |
public void testTimedWait() throws InterruptedException { |
359 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
360 |
> |
public void realRun() throws InterruptedException { |
361 |
> |
Object o = new Object(); |
362 |
> |
|
363 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
364 |
> |
synchronized (o) { |
365 |
> |
tu.timedWait(o,MEDIUM_DELAY_MS); |
366 |
> |
} |
367 |
> |
}}); |
368 |
> |
t.start(); |
369 |
> |
Thread.sleep(SHORT_DELAY_MS); |
370 |
> |
t.interrupt(); |
371 |
> |
t.join(); |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
|
/** |
376 |
|
* timedJoin throws InterruptedException when interrupted |
377 |
|
*/ |
378 |
< |
public void testTimedJoin() { |
379 |
< |
Thread t = new Thread(new Runnable() { |
380 |
< |
public void run() { |
381 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
382 |
< |
try { |
383 |
< |
Thread s = new Thread(new Runnable() { |
384 |
< |
public void run() { |
385 |
< |
try { |
386 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
387 |
< |
} catch (InterruptedException success) {} |
388 |
< |
} |
389 |
< |
}); |
390 |
< |
s.start(); |
391 |
< |
tu.timedJoin(s,MEDIUM_DELAY_MS); |
392 |
< |
threadShouldThrow(); |
393 |
< |
} |
394 |
< |
catch (Exception e) {} |
421 |
< |
} |
422 |
< |
}); |
423 |
< |
t.start(); |
424 |
< |
try { |
425 |
< |
Thread.sleep(SHORT_DELAY_MS); |
426 |
< |
t.interrupt(); |
427 |
< |
t.join(); |
428 |
< |
} catch (Exception e) { |
429 |
< |
unexpectedException(); |
430 |
< |
} |
378 |
> |
public void testTimedJoin() throws InterruptedException { |
379 |
> |
final Thread s = new Thread(new CheckedInterruptedRunnable() { |
380 |
> |
public void realRun() throws InterruptedException { |
381 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
382 |
> |
}}); |
383 |
> |
final Thread t = new Thread(new CheckedInterruptedRunnable() { |
384 |
> |
public void realRun() throws InterruptedException { |
385 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
386 |
> |
tu.timedJoin(s, MEDIUM_DELAY_MS); |
387 |
> |
}});; |
388 |
> |
s.start(); |
389 |
> |
t.start(); |
390 |
> |
Thread.sleep(SHORT_DELAY_MS); |
391 |
> |
t.interrupt(); |
392 |
> |
t.join(); |
393 |
> |
s.interrupt(); |
394 |
> |
s.join(); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
|
* timedSleep throws InterruptedException when interrupted |
399 |
|
*/ |
400 |
< |
public void testTimedSleep() { |
401 |
< |
//created a new thread with anonymous runnable |
400 |
> |
public void testTimedSleep() throws InterruptedException { |
401 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
402 |
> |
public void realRun() throws InterruptedException { |
403 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
404 |
> |
tu.sleep(MEDIUM_DELAY_MS); |
405 |
> |
}}); |
406 |
|
|
407 |
< |
Thread t = new Thread(new Runnable() { |
408 |
< |
public void run() { |
409 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
410 |
< |
try { |
443 |
< |
tu.sleep(MEDIUM_DELAY_MS); |
444 |
< |
threadShouldThrow(); |
445 |
< |
} |
446 |
< |
catch (InterruptedException success) {} |
447 |
< |
} |
448 |
< |
}); |
449 |
< |
t.start(); |
450 |
< |
try { |
451 |
< |
Thread.sleep(SHORT_DELAY_MS); |
452 |
< |
t.interrupt(); |
453 |
< |
t.join(); |
454 |
< |
} catch (Exception e) { |
455 |
< |
unexpectedException(); |
456 |
< |
} |
407 |
> |
t.start(); |
408 |
> |
Thread.sleep(SHORT_DELAY_MS); |
409 |
> |
t.interrupt(); |
410 |
> |
t.join(); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
< |
* a deserialized serialized unit is equal |
414 |
> |
* a deserialized serialized unit is the same instance |
415 |
|
*/ |
416 |
< |
public void testSerialization() { |
416 |
> |
public void testSerialization() throws Exception { |
417 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
418 |
|
|
419 |
< |
try { |
420 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
421 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
422 |
< |
out.writeObject(q); |
423 |
< |
out.close(); |
424 |
< |
|
425 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
426 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
427 |
< |
TimeUnit r = (TimeUnit)in.readObject(); |
474 |
< |
|
475 |
< |
assertEquals(q.toString(), r.toString()); |
476 |
< |
} catch (Exception e) { |
477 |
< |
e.printStackTrace(); |
478 |
< |
unexpectedException(); |
479 |
< |
} |
419 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
420 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
421 |
> |
out.writeObject(q); |
422 |
> |
out.close(); |
423 |
> |
|
424 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
425 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
426 |
> |
TimeUnit r = (TimeUnit)in.readObject(); |
427 |
> |
assertSame(q, r); |
428 |
|
} |
429 |
|
|
430 |
|
} |