use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class ParamFlowCheckerTest method testSingleValueCheckQpsWithExceptionItems.
@Test
public void testSingleValueCheckQpsWithExceptionItems() throws InterruptedException {
final String resourceName = "testSingleValueCheckQpsWithExceptionItems";
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
TimeUtil.currentTimeMillis();
int paramIdx = 0;
long globalThreshold = 5L;
int thresholdB = 0;
int thresholdD = 7;
ParamFlowRule rule = new ParamFlowRule();
rule.setResource(resourceName);
rule.setCount(globalThreshold);
rule.setParamIdx(paramIdx);
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
String valueA = "valueA";
String valueB = "valueB";
String valueC = "valueC";
String valueD = "valueD";
// Directly set parsed map for test.
Map<Object, Integer> map = new HashMap<Object, Integer>();
map.put(valueB, thresholdB);
map.put(valueD, thresholdD);
rule.setParsedHotItems(map);
ParameterMetric metric = new ParameterMetric();
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB));
TimeUnit.SECONDS.sleep(3);
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class ParamFlowDefaultCheckerTest method testParamFlowDefaultCheckSingleQps.
@Test
public void testParamFlowDefaultCheckSingleQps() {
final String resourceName = "testParamFlowDefaultCheckSingleQps";
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
int paramIdx = 0;
long threshold = 5L;
ParamFlowRule rule = new ParamFlowRule();
rule.setResource(resourceName);
rule.setCount(threshold);
rule.setParamIdx(paramIdx);
String valueA = "valueA";
ParameterMetric metric = new ParameterMetric();
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
// We mock the time directly to avoid unstable behaviour.
setCurrentMillis(System.currentTimeMillis());
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
sleep(3000);
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class ParamFlowDefaultCheckerTest method testParamFlowDefaultCheckQpsInDifferentDuration.
@Test
public void testParamFlowDefaultCheckQpsInDifferentDuration() throws InterruptedException {
final String resourceName = "testParamFlowDefaultCheckQpsInDifferentDuration";
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
int paramIdx = 0;
long threshold = 5L;
ParamFlowRule rule = new ParamFlowRule();
rule.setResource(resourceName);
rule.setCount(threshold);
rule.setParamIdx(paramIdx);
rule.setDurationInSec(60);
String valueA = "helloWorld";
ParameterMetric metric = new ParameterMetric();
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
// We mock the time directly to avoid unstable behaviour.
setCurrentMillis(System.currentTimeMillis());
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
sleepSecond(1);
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
sleepSecond(10);
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
sleepSecond(30);
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
sleepSecond(30);
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class ParamFlowSlotTest method testEntryWhenParamFlowRuleNotExists.
@Test
public void testEntryWhenParamFlowRuleNotExists() throws Throwable {
String resourceName = "testEntryWhenParamFlowRuleNotExists";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc");
// The parameter metric instance will not be created.
assertNull(ParameterMetricStorage.getParamMetric(resourceWrapper));
}
use of com.alibaba.csp.sentinel.slotchain.ResourceWrapper in project Sentinel by alibaba.
the class ParamFlowSlotTest method testNegativeParamIdx.
@Test
public void testNegativeParamIdx() throws Throwable {
String resourceName = "testNegativeParamIdx";
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(1).setParamIdx(-1);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi");
assertEquals(2, rule.getParamIdx().longValue());
rule.setParamIdx(-1);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, null);
// Null args will not trigger conversion.
assertEquals(-1, rule.getParamIdx().intValue());
rule.setParamIdx(-100);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi");
assertEquals(100, rule.getParamIdx().longValue());
rule.setParamIdx(0);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi");
assertEquals(0, rule.getParamIdx().longValue());
}
Aggregations