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)); |
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)); |
324 |
|
} |
325 |
|
|
326 |
|
|
337 |
|
* Timed wait without holding lock throws |
338 |
|
* IllegalMonitorStateException |
339 |
|
*/ |
340 |
< |
public void testTimedWait_IllegalMonitorException() { |
341 |
< |
//created a new thread with anonymous runnable |
340 |
> |
public void testTimedWait_IllegalMonitorException() throws Exception { |
341 |
> |
Thread t = new Thread(new CheckedRunnable() { |
342 |
> |
public void realRun() throws InterruptedException { |
343 |
> |
Object o = new Object(); |
344 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
345 |
> |
try { |
346 |
> |
tu.timedWait(o,LONG_DELAY_MS); |
347 |
> |
threadShouldThrow(); |
348 |
> |
} catch (IllegalMonitorStateException success) {}}}); |
349 |
|
|
343 |
– |
Thread t = new Thread(new Runnable() { |
344 |
– |
public void run() { |
345 |
– |
Object o = new Object(); |
346 |
– |
TimeUnit tu = TimeUnit.MILLISECONDS; |
347 |
– |
try { |
348 |
– |
tu.timedWait(o,LONG_DELAY_MS); |
349 |
– |
threadShouldThrow(); |
350 |
– |
} |
351 |
– |
catch (InterruptedException ie) { |
352 |
– |
threadUnexpectedException(); |
353 |
– |
} |
354 |
– |
catch (IllegalMonitorStateException success) { |
355 |
– |
} |
356 |
– |
|
357 |
– |
} |
358 |
– |
}); |
350 |
|
t.start(); |
351 |
< |
try { |
352 |
< |
Thread.sleep(SHORT_DELAY_MS); |
353 |
< |
t.interrupt(); |
363 |
< |
t.join(); |
364 |
< |
} catch (Exception e) { |
365 |
< |
unexpectedException(); |
366 |
< |
} |
351 |
> |
Thread.sleep(SHORT_DELAY_MS); |
352 |
> |
t.interrupt(); |
353 |
> |
t.join(); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* timedWait throws InterruptedException when interrupted |
358 |
|
*/ |
359 |
< |
public void testTimedWait() { |
360 |
< |
Thread t = new Thread(new Runnable() { |
361 |
< |
public void run() { |
362 |
< |
Object o = new Object(); |
363 |
< |
|
364 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
365 |
< |
try { |
366 |
< |
synchronized(o) { |
367 |
< |
tu.timedWait(o,MEDIUM_DELAY_MS); |
368 |
< |
} |
382 |
< |
threadShouldThrow(); |
383 |
< |
} |
384 |
< |
catch (InterruptedException success) {} |
385 |
< |
catch (IllegalMonitorStateException failure) { |
386 |
< |
threadUnexpectedException(); |
387 |
< |
} |
388 |
< |
} |
389 |
< |
}); |
359 |
> |
public void testTimedWait() throws InterruptedException { |
360 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
361 |
> |
public void realRun() throws InterruptedException { |
362 |
> |
Object o = new Object(); |
363 |
> |
|
364 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
365 |
> |
synchronized(o) { |
366 |
> |
tu.timedWait(o,MEDIUM_DELAY_MS); |
367 |
> |
} |
368 |
> |
}}); |
369 |
|
t.start(); |
370 |
< |
try { |
371 |
< |
Thread.sleep(SHORT_DELAY_MS); |
372 |
< |
t.interrupt(); |
394 |
< |
t.join(); |
395 |
< |
} catch (Exception e) { |
396 |
< |
unexpectedException(); |
397 |
< |
} |
370 |
> |
Thread.sleep(SHORT_DELAY_MS); |
371 |
> |
t.interrupt(); |
372 |
> |
t.join(); |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
/** |
377 |
|
* timedJoin throws InterruptedException when interrupted |
378 |
|
*/ |
379 |
< |
public void testTimedJoin() { |
380 |
< |
Thread t = new Thread(new Runnable() { |
381 |
< |
public void run() { |
382 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
383 |
< |
try { |
384 |
< |
Thread s = new Thread(new Runnable() { |
385 |
< |
public void run() { |
386 |
< |
try { |
387 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
388 |
< |
} catch (InterruptedException success) {} |
389 |
< |
} |
415 |
< |
}); |
416 |
< |
s.start(); |
417 |
< |
tu.timedJoin(s,MEDIUM_DELAY_MS); |
418 |
< |
threadShouldThrow(); |
419 |
< |
} |
420 |
< |
catch (Exception e) {} |
421 |
< |
} |
422 |
< |
}); |
379 |
> |
public void testTimedJoin() throws InterruptedException { |
380 |
> |
final Thread s = new Thread(new CheckedInterruptedRunnable() { |
381 |
> |
public void realRun() throws InterruptedException { |
382 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
383 |
> |
}}); |
384 |
> |
final Thread t = new Thread(new CheckedInterruptedRunnable() { |
385 |
> |
public void realRun() throws InterruptedException { |
386 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
387 |
> |
tu.timedJoin(s, MEDIUM_DELAY_MS); |
388 |
> |
}});; |
389 |
> |
s.start(); |
390 |
|
t.start(); |
391 |
< |
try { |
392 |
< |
Thread.sleep(SHORT_DELAY_MS); |
393 |
< |
t.interrupt(); |
394 |
< |
t.join(); |
395 |
< |
} catch (Exception e) { |
429 |
< |
unexpectedException(); |
430 |
< |
} |
391 |
> |
Thread.sleep(SHORT_DELAY_MS); |
392 |
> |
t.interrupt(); |
393 |
> |
t.join(); |
394 |
> |
s.interrupt(); |
395 |
> |
s.join(); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
|
* timedSleep throws InterruptedException when interrupted |
400 |
|
*/ |
401 |
< |
public void testTimedSleep() { |
402 |
< |
//created a new thread with anonymous runnable |
401 |
> |
public void testTimedSleep() throws InterruptedException { |
402 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
403 |
> |
public void realRun() throws InterruptedException { |
404 |
> |
TimeUnit tu = TimeUnit.MILLISECONDS; |
405 |
> |
tu.sleep(MEDIUM_DELAY_MS); |
406 |
> |
}}); |
407 |
|
|
439 |
– |
Thread t = new Thread(new Runnable() { |
440 |
– |
public void run() { |
441 |
– |
TimeUnit tu = TimeUnit.MILLISECONDS; |
442 |
– |
try { |
443 |
– |
tu.sleep(MEDIUM_DELAY_MS); |
444 |
– |
threadShouldThrow(); |
445 |
– |
} |
446 |
– |
catch (InterruptedException success) {} |
447 |
– |
} |
448 |
– |
}); |
408 |
|
t.start(); |
409 |
< |
try { |
410 |
< |
Thread.sleep(SHORT_DELAY_MS); |
411 |
< |
t.interrupt(); |
453 |
< |
t.join(); |
454 |
< |
} catch (Exception e) { |
455 |
< |
unexpectedException(); |
456 |
< |
} |
409 |
> |
Thread.sleep(SHORT_DELAY_MS); |
410 |
> |
t.interrupt(); |
411 |
> |
t.join(); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* a deserialized serialized unit is equal |
415 |
> |
* a deserialized serialized unit is the same instance |
416 |
|
*/ |
417 |
< |
public void testSerialization() { |
417 |
> |
public void testSerialization() throws Exception { |
418 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
419 |
|
|
420 |
< |
try { |
421 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
422 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
423 |
< |
out.writeObject(q); |
424 |
< |
out.close(); |
425 |
< |
|
426 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
427 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
428 |
< |
TimeUnit r = (TimeUnit)in.readObject(); |
474 |
< |
|
475 |
< |
assertEquals(q.toString(), r.toString()); |
476 |
< |
} catch (Exception e) { |
477 |
< |
e.printStackTrace(); |
478 |
< |
unexpectedException(); |
479 |
< |
} |
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(); |
428 |
> |
assertSame(q, r); |
429 |
|
} |
430 |
|
|
431 |
|
} |