7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
+ |
import java.io.ByteArrayInputStream; |
11 |
+ |
import java.io.ByteArrayOutputStream; |
12 |
+ |
import java.io.ObjectInputStream; |
13 |
+ |
import java.io.ObjectOutputStream; |
14 |
|
import java.util.Arrays; |
15 |
|
import java.util.NoSuchElementException; |
16 |
|
import java.util.PropertyPermission; |
451 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
452 |
|
* until time elapses. |
453 |
|
*/ |
454 |
< |
public static void delay(long ms) throws InterruptedException { |
454 |
> |
public static void delay(long millis) throws InterruptedException { |
455 |
|
long startTime = System.nanoTime(); |
456 |
< |
long ns = ms * 1000 * 1000; |
456 |
> |
long ns = millis * 1000 * 1000; |
457 |
|
for (;;) { |
458 |
< |
if (ms > 0L) |
459 |
< |
Thread.sleep(ms); |
458 |
> |
if (millis > 0L) |
459 |
> |
Thread.sleep(millis); |
460 |
|
else // too short to sleep |
461 |
|
Thread.yield(); |
462 |
|
long d = ns - (System.nanoTime() - startTime); |
463 |
|
if (d > 0L) |
464 |
< |
ms = d / (1000 * 1000); |
464 |
> |
millis = d / (1000 * 1000); |
465 |
|
else |
466 |
|
break; |
467 |
|
} |
482 |
|
} |
483 |
|
} |
484 |
|
|
485 |
+ |
/** |
486 |
+ |
* Checks that thread does not terminate within the given millisecond delay. |
487 |
+ |
*/ |
488 |
+ |
public void assertThreadStaysAlive(Thread thread, long millis) { |
489 |
+ |
try { |
490 |
+ |
// No need to optimize the failing case via Thread.join. |
491 |
+ |
delay(millis); |
492 |
+ |
assertTrue(thread.isAlive()); |
493 |
+ |
} catch (InterruptedException ie) { |
494 |
+ |
fail("Unexpected InterruptedException"); |
495 |
+ |
} |
496 |
+ |
} |
497 |
|
|
498 |
|
/** |
499 |
|
* Fails with message "should throw exception". |
846 |
|
public Runnable awaiter(final CountDownLatch latch) { |
847 |
|
return new CheckedRunnable() { |
848 |
|
public void realRun() throws InterruptedException { |
849 |
< |
latch.await(); |
849 |
> |
await(latch); |
850 |
|
}}; |
851 |
|
} |
852 |
|
|
853 |
+ |
public void await(CountDownLatch latch) { |
854 |
+ |
try { |
855 |
+ |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
856 |
+ |
} catch (Throwable t) { |
857 |
+ |
threadUnexpectedException(t); |
858 |
+ |
} |
859 |
+ |
} |
860 |
+ |
|
861 |
|
public static class NPETask implements Callable<String> { |
862 |
|
public String call() { throw new NullPointerException(); } |
863 |
|
} |
1105 |
|
} |
1106 |
|
} |
1107 |
|
|
1108 |
+ |
@SuppressWarnings("unchecked") |
1109 |
+ |
public <T> T serialClone(T o) { |
1110 |
+ |
try { |
1111 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1112 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1113 |
+ |
oos.writeObject(o); |
1114 |
+ |
oos.flush(); |
1115 |
+ |
oos.close(); |
1116 |
+ |
ByteArrayInputStream bin = |
1117 |
+ |
new ByteArrayInputStream(bos.toByteArray()); |
1118 |
+ |
ObjectInputStream ois = new ObjectInputStream(bin); |
1119 |
+ |
return (T) ois.readObject(); |
1120 |
+ |
} catch (Throwable t) { |
1121 |
+ |
threadUnexpectedException(t); |
1122 |
+ |
return null; |
1123 |
+ |
} |
1124 |
+ |
} |
1125 |
|
} |