Improve AbstractTimeBasedTest in sentinel-core
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
030ec23736
commit
7d344dcb95
|
|
@ -23,14 +23,14 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock support for {@link TimeUtil}
|
* Mock support for {@link TimeUtil}.
|
||||||
*
|
*
|
||||||
* @author jason
|
* @author jason
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
@PrepareForTest({ TimeUtil.class })
|
@PrepareForTest({ TimeUtil.class })
|
||||||
public abstract class AbstractTimeBasedTest {
|
public abstract class AbstractTimeBasedTest {
|
||||||
|
|
||||||
private long currentMillis = 0;
|
private long currentMillis = 0;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -38,6 +38,10 @@ public abstract class AbstractTimeBasedTest {
|
||||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final void useActualTime() {
|
||||||
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenCallRealMethod();
|
||||||
|
}
|
||||||
|
|
||||||
protected final void setCurrentMillis(long cur) {
|
protected final void setCurrentMillis(long cur) {
|
||||||
currentMillis = cur;
|
currentMillis = cur;
|
||||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||||
|
|
@ -47,4 +51,8 @@ public abstract class AbstractTimeBasedTest {
|
||||||
currentMillis += t;
|
currentMillis += t;
|
||||||
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final void sleepSecond(int timeSec) {
|
||||||
|
sleep(timeSec * 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue