|
|
@@ -15,29 +15,27 @@ |
|
|
|
*/ |
|
|
|
package com.alibaba.csp.sentinel.slots.block.flow.param; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
import com.alibaba.csp.sentinel.EntryType; |
|
|
|
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; |
|
|
|
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.RuleConstant; |
|
|
|
import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; |
|
|
|
import com.alibaba.csp.sentinel.util.TimeUtil; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
import java.util.concurrent.atomic.AtomicLong; |
|
|
|
|
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import com.alibaba.csp.sentinel.EntryType; |
|
|
|
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; |
|
|
|
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.RuleConstant; |
|
|
|
import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; |
|
|
|
import com.alibaba.csp.sentinel.util.TimeUtil; |
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
/** |
|
|
|
* Test cases for {@link ParamFlowChecker}. |
|
|
@@ -58,7 +56,7 @@ public class ParamFlowCheckerTest { |
|
|
|
rule.setParamIdx(paramIdx); |
|
|
|
|
|
|
|
assertTrue("The rule will pass if the paramIdx exceeds provided args", |
|
|
|
ParamFlowChecker.passCheck(resourceWrapper, rule, 1, "abc")); |
|
|
|
ParamFlowChecker.passCheck(resourceWrapper, rule, 1, "abc")); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
@@ -109,7 +107,7 @@ public class ParamFlowCheckerTest { |
|
|
|
int thresholdD = 7; |
|
|
|
|
|
|
|
ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(globalThreshold).setParamIdx(paramIdx) |
|
|
|
.setGrade(RuleConstant.FLOW_GRADE_THREAD); |
|
|
|
.setGrade(RuleConstant.FLOW_GRADE_THREAD); |
|
|
|
|
|
|
|
String valueA = "valueA"; |
|
|
|
String valueB = "valueB"; |
|
|
@@ -137,7 +135,7 @@ public class ParamFlowCheckerTest { |
|
|
|
when(metric.getThreadCount(paramIdx, valueA)).thenReturn(globalThreshold); |
|
|
|
when(metric.getThreadCount(paramIdx, valueB)).thenReturn(thresholdB - 1L); |
|
|
|
when(metric.getThreadCount(paramIdx, valueC)).thenReturn(globalThreshold + 1); |
|
|
|
when(metric.getThreadCount(paramIdx, valueD)).thenReturn(globalThreshold - 1).thenReturn((long)thresholdD); |
|
|
|
when(metric.getThreadCount(paramIdx, valueD)).thenReturn(globalThreshold - 1).thenReturn((long) thresholdD); |
|
|
|
|
|
|
|
assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA)); |
|
|
|
assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB)); |
|
|
@@ -174,12 +172,12 @@ public class ParamFlowCheckerTest { |
|
|
|
double globalThreshold = 1; |
|
|
|
|
|
|
|
ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx) |
|
|
|
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setCount(globalThreshold); |
|
|
|
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setCount(globalThreshold); |
|
|
|
|
|
|
|
TimeUtil.currentTimeMillis(); |
|
|
|
|
|
|
|
String v1 = "a", v2 = "B", v3 = "Cc"; |
|
|
|
Object arr = new String[] {v1, v2, v3}; |
|
|
|
Object arr = new String[]{v1, v2, v3}; |
|
|
|
ParameterMetric metric = new ParameterMetric(); |
|
|
|
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); |
|
|
|
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000)); |
|
|
@@ -188,6 +186,41 @@ public class ParamFlowCheckerTest { |
|
|
|
assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr)); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testPassLocalCheckForComplexParam() throws InterruptedException { |
|
|
|
class User implements ParamFlowArgument { |
|
|
|
Integer id; |
|
|
|
String name; |
|
|
|
String address; |
|
|
|
|
|
|
|
public User(Integer id, String name, String address) { |
|
|
|
this.id = id; |
|
|
|
this.name = name; |
|
|
|
this.address = address; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Object paramFlowKey() { |
|
|
|
return name; |
|
|
|
} |
|
|
|
} |
|
|
|
final String resourceName = "testPassLocalCheckForComplexParam"; |
|
|
|
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN); |
|
|
|
int paramIdx = 0; |
|
|
|
double globalThreshold = 1; |
|
|
|
|
|
|
|
ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx).setCount(globalThreshold); |
|
|
|
|
|
|
|
Object[] args = new Object[]{new User(1, "Bob", "Hangzhou"), 10, "Demo"}; |
|
|
|
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)); |
|
|
|
|
|
|
|
assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, args)); |
|
|
|
assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, args)); |
|
|
|
} |
|
|
|
|
|
|
|
@Before |
|
|
|
public void setUp() throws Exception { |
|
|
|
ParameterMetricStorage.getMetricsMap().clear(); |
|
|
|