HBASE-19490 Rare failure in TestRateLimiter

This commit is contained in:
Chia-Ping Tsai 2018-01-03 03:02:45 +08:00
parent 490728ae7f
commit 2c30c9bbb6
1 changed files with 11 additions and 1 deletions

View File

@ -16,8 +16,8 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.hadoop.hbase.testclassification.SmallTests; import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.EnvironmentEdge;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.ManualEnvironmentEdge; import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
import org.junit.Test; import org.junit.Test;
@ -114,6 +114,15 @@ public class TestRateLimiter {
RateLimiter limiter = new FixedIntervalRateLimiter(); RateLimiter limiter = new FixedIntervalRateLimiter();
limiter.set(10, TimeUnit.SECONDS); limiter.set(10, TimeUnit.SECONDS);
// fix the current time in order to get the precise value of interval
EnvironmentEdge edge = new EnvironmentEdge() {
private final long ts = System.currentTimeMillis();
@Override
public long currentTime() {
return ts;
}
};
EnvironmentEdgeManager.injectEdge(edge);
// 10 resources are available, but we need to consume 20 resources // 10 resources are available, but we need to consume 20 resources
// Verify that we have to wait at least 1.1sec to have 1 resource available // Verify that we have to wait at least 1.1sec to have 1 resource available
assertTrue(limiter.canExecute()); assertTrue(limiter.canExecute());
@ -122,6 +131,7 @@ public class TestRateLimiter {
assertEquals(1000, limiter.waitInterval(1)); assertEquals(1000, limiter.waitInterval(1));
// To consume 10 resource wait for 100ms // To consume 10 resource wait for 100ms
assertEquals(1000, limiter.waitInterval(10)); assertEquals(1000, limiter.waitInterval(10));
EnvironmentEdgeManager.reset();
limiter.setNextRefillTime(limiter.getNextRefillTime() - 900); limiter.setNextRefillTime(limiter.getNextRefillTime() - 900);
// Verify that after 1sec also no resource should be available // Verify that after 1sec also no resource should be available