Search in sources :

Example 31 with FlowRule

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

the class SentinelModeRedisDataSourceTest method testConnectToSentinelAndPubMsgSuccess.

@Test
public void testConnectToSentinelAndPubMsgSuccess() {
    int maxQueueingTimeMs = new Random().nextInt();
    String flowRulesJson = "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, " + "\"refResource\":null, " + "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs + ", \"controller\":null}]";
    RedisCommands<String, String> subCommands = client.connect().sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRulesJson);
    subCommands.publish(channel, flowRulesJson);
    subCommands.exec();
    await().timeout(2, TimeUnit.SECONDS).until(new Callable<List<FlowRule>>() {

        @Override
        public List<FlowRule> call() throws Exception {
            return FlowRuleManager.getRules();
        }
    }, Matchers.hasSize(1));
    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
Also used : Random(java.util.Random) List(java.util.List) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 32 with FlowRule

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

the class SentinelAnnotationInterceptorIntegrationTest method testBlockHandlerNotFound.

@Test(expected = FlowException.class)
public void testBlockHandlerNotFound() {
    assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
    String resourceName = "apiBaz";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    fooService.baz("Sentinel");
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 33 with FlowRule

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

the class TransportSpringMvcDemoApplication method initFlowRules.

public static void initFlowRules() {
    List<FlowRule> rules = new ArrayList<>();
    FlowRule rule = new FlowRule();
    rule.setResource("demo-hello-api");
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setCount(1);
    rules.add(rule);
    FlowRuleManager.loadRules(rules);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 34 with FlowRule

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

the class SentinelAnnotationInterceptorIntegrationTest method testNormalBlockHandlerAndFallback.

@Test
public void testNormalBlockHandlerAndFallback() throws Exception {
    assertThat(fooService.foo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiFoo";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    // Test for biz exception.
    try {
        fooService.foo(5758);
        fail("should not reach here");
    } catch (Exception ex) {
        // Should not be traced.
        assertThat(cn.exceptionQps()).isZero();
    }
    try {
        fooService.foo(5763);
        fail("should not reach here");
    } catch (Exception ex) {
        assertThat(cn.exceptionQps()).isPositive();
    }
    // Test for blockHandler
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    assertThat(fooService.foo(1121)).isEqualTo("Oops, 1121");
    assertThat(cn.blockQps()).isPositive();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException)

Example 35 with FlowRule

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

the class SentinelAnnotationInterceptorIntegrationTest method testForeignBlockHandlerClass.

@Test
public void testForeignBlockHandlerClass() throws Exception {
    assertThat(fooService.random()).isNotEqualTo(FooUtil.BLOCK_FLAG);
    String resourceName = MethodUtil.resolveMethodName(FooService.class.getDeclaredMethod("random"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();
    FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
    assertThat(fooService.random()).isEqualTo(FooUtil.BLOCK_FLAG);
    assertThat(cn.blockQps()).isPositive();
}
Also used : FooService(com.alibaba.csp.sentinel.annotation.cdi.interceptor.integration.service.FooService) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) 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