Search in sources :

Example 66 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class ConcurrentClusterFlowCheckerTest method setUp.

@Before
public void setUp() {
    FlowRule rule = new FlowRule();
    ClusterFlowConfig config = new ClusterFlowConfig();
    config.setResourceTimeout(500);
    config.setClientOfflineTime(1000);
    config.setFlowId(111L);
    config.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
    rule.setClusterConfig(config);
    rule.setClusterMode(true);
    rule.setCount(10);
    rule.setResource("test");
    rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
    ArrayList<FlowRule> rules = new ArrayList<>();
    rules.add(rule);
    ClusterFlowRuleManager.registerPropertyIfAbsent("1-name");
    ClusterFlowRuleManager.loadRules("1-name", rules);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ClusterFlowConfig(com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig) Before(org.junit.Before)

Example 67 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class ConcurrentClusterFlowCheckerTest method testEasyAcquireAndRelease.

@Test
public void testEasyAcquireAndRelease() throws InterruptedException {
    setCurrentMillis(System.currentTimeMillis());
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(111L);
    ArrayList<Long> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        TokenResult result = ConcurrentClusterFlowChecker.acquireConcurrentToken("127.0.0.1", rule, 1);
        Assert.assertTrue("fail to acquire token", result.getStatus() == TokenResultStatus.OK && result.getTokenId() != 0);
        list.add(result.getTokenId());
    }
    for (int i = 0; i < 10; i++) {
        TokenResult result = ConcurrentClusterFlowChecker.acquireConcurrentToken("127.0.0.1", rule, 1);
        Assert.assertTrue("fail to acquire block token", result.getStatus() == TokenResultStatus.BLOCKED);
    }
    for (int i = 0; i < 10; i++) {
        TokenResult result = ConcurrentClusterFlowChecker.releaseConcurrentToken(list.get(i));
        Assert.assertTrue("fail to release token", result.getStatus() == TokenResultStatus.RELEASE_OK);
    }
    Assert.assertTrue("fail to release token", CurrentConcurrencyManager.get(111L).get() == 0 && TokenCacheNodeManager.getSize() == 0);
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 68 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class TokenCacheNodeManagerTest method setUp.

@Before
public void setUp() {
    FlowRule rule = new FlowRule();
    ClusterFlowConfig config = new ClusterFlowConfig();
    config.setResourceTimeout(500);
    config.setClientOfflineTime(1000);
    config.setFlowId(111L);
    config.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
    rule.setClusterConfig(config);
    rule.setClusterMode(true);
    rule.setCount(10);
    rule.setResource("test");
    rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
    ArrayList<FlowRule> rules = new ArrayList<>();
    rules.add(rule);
    ClusterFlowRuleManager.registerPropertyIfAbsent("1-name");
    ClusterFlowRuleManager.loadRules("1-name", rules);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ClusterFlowConfig(com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig) Before(org.junit.Before)

Example 69 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class CommonFilterTest method configureRulesFor.

private void configureRulesFor(String resource, int count, String limitApp) {
    FlowRule rule = new FlowRule().setCount(count).setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setResource(resource);
    if (StringUtil.isNotBlank(limitApp)) {
        rule.setLimitApp(limitApp);
    }
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 70 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.

the class CommonFilterMethodTest method configureRulesFor.

private void configureRulesFor(String resource, int count, String limitApp) {
    FlowRule rule = new FlowRule().setCount(count).setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setResource(resource);
    if (StringUtil.isNotBlank(limitApp)) {
        rule.setLimitApp(limitApp);
    }
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Aggregations

FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)113 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)22 List (java.util.List)17 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)11 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 Test (org.junit.jupiter.api.Test)6 ClusterFlowConfig (com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig)5 TypeReference (com.alibaba.fastjson.TypeReference)5 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 Before (org.junit.Before)5 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)4 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)3 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)3 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 IOException (java.io.IOException)3