749 |
|
/** |
750 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
751 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
752 |
< |
* until time elapses. |
752 |
> |
* until time elapses. Ensures that the given time, as measured |
753 |
> |
* by System.nanoTime(), has elapsed. |
754 |
|
*/ |
755 |
|
static void delay(long millis) throws InterruptedException { |
756 |
< |
long startTime = System.nanoTime(); |
757 |
< |
long ns = millis * 1000 * 1000; |
758 |
< |
for (;;) { |
756 |
> |
long nanos = millis * (1000 * 1000); |
757 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
758 |
> |
do { |
759 |
|
if (millis > 0L) |
760 |
|
Thread.sleep(millis); |
761 |
|
else // too short to sleep |
762 |
|
Thread.yield(); |
763 |
< |
long d = ns - (System.nanoTime() - startTime); |
764 |
< |
if (d > 0L) |
765 |
< |
millis = d / (1000 * 1000); |
765 |
< |
else |
766 |
< |
break; |
767 |
< |
} |
763 |
> |
nanos = wakeupTime - System.nanoTime(); |
764 |
> |
millis = nanos / (1000 * 1000); |
765 |
> |
} while (nanos >= 0L); |
766 |
|
} |
767 |
|
|
768 |
|
/** |
769 |
|
* Allows use of try-with-resources with per-test thread pools. |
770 |
|
*/ |
771 |
< |
class PoolCleaner<T extends ExecutorService> |
772 |
< |
implements AutoCloseable { |
773 |
< |
public final T pool; |
776 |
< |
public PoolCleaner(T pool) { this.pool = pool; } |
771 |
> |
class PoolCleaner implements AutoCloseable { |
772 |
> |
private final ExecutorService pool; |
773 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
774 |
|
public void close() { joinPool(pool); } |
775 |
|
} |
776 |
|
|
777 |
< |
<T extends ExecutorService> PoolCleaner<T> cleaner(T pool) { |
778 |
< |
return new PoolCleaner<T>(pool); |
777 |
> |
PoolCleaner cleaner(ExecutorService pool) { |
778 |
> |
return new PoolCleaner(pool); |
779 |
|
} |
780 |
|
|
781 |
|
/** |
810 |
|
* necessarily individually slow because they must block. |
811 |
|
*/ |
812 |
|
void testInParallel(Action ... actions) { |
813 |
< |
try (PoolCleaner<ExecutorService> cleaner |
814 |
< |
= cleaner(Executors.newCachedThreadPool())) { |
818 |
< |
ExecutorService pool = cleaner.pool; |
813 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
814 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
815 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
816 |
|
for (final Action action : actions) |
817 |
|
futures.add(pool.submit(new CheckedRunnable() { |
1316 |
|
}}; |
1317 |
|
} |
1318 |
|
|
1319 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1320 |
< |
return new CheckedRunnable() { |
1321 |
< |
public void realRun() throws InterruptedException { |
1322 |
< |
await(latch); |
1323 |
< |
}}; |
1319 |
> |
class LatchAwaiter extends CheckedRunnable { |
1320 |
> |
static final int NEW = 0; |
1321 |
> |
static final int RUNNING = 1; |
1322 |
> |
static final int DONE = 2; |
1323 |
> |
final CountDownLatch latch; |
1324 |
> |
int state = NEW; |
1325 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1326 |
> |
public void realRun() throws InterruptedException { |
1327 |
> |
state = 1; |
1328 |
> |
await(latch); |
1329 |
> |
state = 2; |
1330 |
> |
} |
1331 |
> |
} |
1332 |
> |
|
1333 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1334 |
> |
return new LatchAwaiter(latch); |
1335 |
|
} |
1336 |
|
|
1337 |
|
public void await(CountDownLatch latch) { |