|
|
@@ -23,28 +23,36 @@ import org.powermock.modules.junit4.PowerMockRunner; |
|
|
|
import com.alibaba.csp.sentinel.util.TimeUtil; |
|
|
|
|
|
|
|
/** |
|
|
|
* Mock support for {@link TimeUtil} |
|
|
|
* Mock support for {@link TimeUtil}. |
|
|
|
* |
|
|
|
* @author jason |
|
|
|
* |
|
|
|
*/ |
|
|
|
@RunWith(PowerMockRunner.class) |
|
|
|
@PrepareForTest({ TimeUtil.class }) |
|
|
|
public abstract class AbstractTimeBasedTest { |
|
|
|
|
|
|
|
private long currentMillis = 0; |
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
PowerMockito.mockStatic(TimeUtil.class); |
|
|
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected final void useActualTime() { |
|
|
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenCallRealMethod(); |
|
|
|
} |
|
|
|
|
|
|
|
protected final void setCurrentMillis(long cur) { |
|
|
|
currentMillis = cur; |
|
|
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected final void sleep(int t) { |
|
|
|
currentMillis += t; |
|
|
|
PowerMockito.when(TimeUtil.currentTimeMillis()).thenReturn(currentMillis); |
|
|
|
} |
|
|
|
|
|
|
|
protected final void sleepSecond(int timeSec) { |
|
|
|
sleep(timeSec * 1000); |
|
|
|
} |
|
|
|
} |