Search in sources :

Example 21 with ResourceWrapper

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);
}
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)

Example 22 with ResourceWrapper

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));
}
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) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 23 with ResourceWrapper

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));
}
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) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 24 with ResourceWrapper

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));
}
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 25 with 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());
}
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)

Aggregations

ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)42 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)31 Test (org.junit.Test)26 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)10 Context (com.alibaba.csp.sentinel.context.Context)7 Node (com.alibaba.csp.sentinel.node.Node)7 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 Entry (com.alibaba.csp.sentinel.Entry)5 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)5 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)5 HashMap (java.util.HashMap)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 MetricNode (com.alibaba.csp.sentinel.node.metric.MetricNode)1 MethodResourceWrapper (com.alibaba.csp.sentinel.slotchain.MethodResourceWrapper)1