blob: 880035e5bc1497e514c598139debb2f34484d646 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean.concurrent;
import com.yahoo.yolean.UncheckedInterruptedException;
import java.time.Duration;
import static com.yahoo.yolean.Exceptions.uncheckInterrupted;
/**
* An abstraction used for mocking {@link Thread#sleep(long)} in unit tests.
*
* @author bjorncs
*/
public interface Sleeper {
default void sleep(Duration duration) throws UncheckedInterruptedException {
uncheckInterrupted(() -> sleepChecked(duration.toMillis()));
}
default void sleepChecked(Duration duration) throws InterruptedException { sleepChecked(duration.toMillis()); }
default void sleep(long millis) throws UncheckedInterruptedException { uncheckInterrupted(() -> sleepChecked(millis)); }
void sleepChecked(long millis) throws InterruptedException;
Sleeper DEFAULT = Thread::sleep;
Sleeper NOOP = millis -> {};
}
|