Search in sources :

Example 21 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParameterMetricStorageTest method testInitParamMetrics.

@Test
public void testInitParamMetrics() {
    ParamFlowRule rule = new ParamFlowRule();
    rule.setParamIdx(1);
    int index = 1;
    String resourceName = "res-" + System.currentTimeMillis();
    ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    assertNull(ParameterMetricStorage.getParamMetric(resourceWrapper));
    ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);
    ParameterMetric metric = ParameterMetricStorage.getParamMetric(resourceWrapper);
    assertNotNull(metric);
    assertNotNull(metric.getRuleTimeCounterMap().get(rule));
    assertNotNull(metric.getThreadCountMap().get(index));
    // Duplicate init.
    ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);
    assertSame(metric, ParameterMetricStorage.getParamMetric(resourceWrapper));
    ParamFlowRule rule2 = new ParamFlowRule();
    rule2.setParamIdx(1);
    assertSame(metric, ParameterMetricStorage.getParamMetric(resourceWrapper));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 22 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testHotParamCheckerPassCheckExceedArgs.

@Test
public void testHotParamCheckerPassCheckExceedArgs() {
    final String resourceName = "testHotParamCheckerPassCheckExceedArgs";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 1;
    ParamFlowRule rule = new ParamFlowRule();
    rule.setResource(resourceName);
    rule.setCount(10);
    rule.setParamIdx(paramIdx);
    assertTrue("The rule will pass if the paramIdx exceeds provided args", ParamFlowChecker.passCheck(resourceWrapper, rule, 1, "abc"));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 23 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testSingleValueCheckThreadCountWithExceptionItems.

@Test
public void testSingleValueCheckThreadCountWithExceptionItems() {
    final String resourceName = "testSingleValueCheckThreadCountWithExceptionItems";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    long globalThreshold = 5L;
    int thresholdB = 3;
    int thresholdD = 7;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(globalThreshold).setParamIdx(paramIdx).setGrade(RuleConstant.FLOW_GRADE_THREAD);
    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 = mock(ParameterMetric.class);
    when(metric.getThreadCount(paramIdx, valueA)).thenReturn(globalThreshold - 1);
    when(metric.getThreadCount(paramIdx, valueB)).thenReturn(globalThreshold - 1);
    when(metric.getThreadCount(paramIdx, valueC)).thenReturn(globalThreshold - 1);
    when(metric.getThreadCount(paramIdx, valueD)).thenReturn(globalThreshold + 1);
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueC));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueD));
    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);
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueC));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueD));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueD));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 24 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testPassLocalCheckForCollection.

@Test
public void testPassLocalCheckForCollection() throws InterruptedException {
    final String resourceName = "testPassLocalCheckForCollection";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    double globalThreshold = 1;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx).setCount(globalThreshold);
    String v1 = "a", v2 = "B", v3 = "Cc";
    List<String> list = Arrays.asList(v1, v2, v3);
    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, list));
    assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, list));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 25 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper 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);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) HashMap(java.util.HashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Aggregations

StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)48 Test (org.junit.Test)43 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)31 Context (com.alibaba.csp.sentinel.context.Context)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)7 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 NullContext (com.alibaba.csp.sentinel.context.NullContext)5 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)4 HashMap (java.util.HashMap)3 Entry (com.alibaba.csp.sentinel.Entry)2 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)2 Node (com.alibaba.csp.sentinel.node.Node)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 ConcurrentLinkedHashMapWrapper (com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper)1 Random (java.util.Random)1