Search in sources :

Example 31 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class FlowRuleCheckerTest method testCustomOriginFlowSelectNode.

@Test
public void testCustomOriginFlowSelectNode() {
    String origin = "appA";
    String limitAppB = "appB";
    DefaultNode node = mock(DefaultNode.class);
    DefaultNode originNode = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(node.getClusterNode()).thenReturn(cn);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn(origin);
    when(context.getOriginNode()).thenReturn(originNode);
    FlowRule rule = new FlowRule("testCustomOriginFlowSelectNode").setCount(1);
    rule.setLimitApp(origin);
    // Origin matches, return the origin node.
    assertEquals(originNode, FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
    rule.setLimitApp(limitAppB);
    // Origin mismatch, no node found.
    assertNull(FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 32 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class FlowRuleCheckerTest method testDefaultLimitAppFlowSelectNode.

@Test
public void testDefaultLimitAppFlowSelectNode() {
    DefaultNode node = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(node.getClusterNode()).thenReturn(cn);
    Context context = mock(Context.class);
    // limitApp: default
    FlowRule rule = new FlowRule("testDefaultLimitAppFlowSelectNode").setCount(1);
    assertEquals(cn, FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 33 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class FlowRuleCheckerTest method testOtherOriginFlowSelectNode.

@Test
public void testOtherOriginFlowSelectNode() {
    String originA = "appA";
    String originB = "appB";
    DefaultNode node = mock(DefaultNode.class);
    DefaultNode originNode = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(node.getClusterNode()).thenReturn(cn);
    Context context = mock(Context.class);
    when(context.getOriginNode()).thenReturn(originNode);
    FlowRule ruleA = new FlowRule("testOtherOriginFlowSelectNode").setCount(1);
    ruleA.setLimitApp(originA);
    FlowRule ruleB = new FlowRule("testOtherOriginFlowSelectNode").setCount(2);
    ruleB.setLimitApp(RuleConstant.LIMIT_APP_OTHER);
    FlowRuleManager.loadRules(Arrays.asList(ruleA, ruleB));
    // Origin matches other, return the origin node.
    when(context.getOrigin()).thenReturn(originB);
    assertEquals(originNode, FlowRuleChecker.selectNodeByRequesterAndStrategy(ruleB, context, node));
    // Origin matches limitApp of an existing rule, so no nodes are selected.
    when(context.getOrigin()).thenReturn(originA);
    assertNull(FlowRuleChecker.selectNodeByRequesterAndStrategy(ruleB, context, node));
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 34 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class CommonFilterContextTest method testCommonFilterMiscellaneous.

@Test
public void testCommonFilterMiscellaneous() throws Exception {
    String url = "/hello";
    this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(HELLO_STR));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(url);
    assertNotNull(cn);
    assertEquals(1, cn.passQps(), 0.01);
    String context = "";
    for (Node n : Constants.ROOT.getChildList()) {
        if (n instanceof EntranceNode) {
            String id = ((EntranceNode) n).getId().getName();
            if (url.equals(id)) {
                context = ((EntranceNode) n).getId().getName();
            }
        }
    }
    assertEquals(url, context);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 35 with ClusterNode

use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.

the class CommonFilterMethodTest method testCommonFilterMiscellaneous.

@Test
public void testCommonFilterMiscellaneous() throws Exception {
    String url = "/hello";
    this.mvc.perform(get(url)).andExpect(status().isOk()).andExpect(content().string(HELLO_STR));
    ClusterNode cnGet = ClusterBuilderSlot.getClusterNode(GET + COLON + url);
    assertNotNull(cnGet);
    assertEquals(1, cnGet.passQps(), 0.01);
    ClusterNode cnPost = ClusterBuilderSlot.getClusterNode(POST + COLON + url);
    assertNull(cnPost);
    this.mvc.perform(post(url)).andExpect(status().isOk()).andExpect(content().string(HELLO_POST_STR));
    cnPost = ClusterBuilderSlot.getClusterNode(POST + COLON + url);
    assertNotNull(cnPost);
    assertEquals(1, cnPost.passQps(), 0.01);
    testCommonBlockAndRedirectBlockPage(url, cnGet, cnPost);
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)92 Test (org.junit.Test)61 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)26 Response (io.restassured.response.Response)14 Node (com.alibaba.csp.sentinel.node.Node)13 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)13 Test (org.junit.jupiter.api.Test)13 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)13 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)11 Response (javax.ws.rs.core.Response)11 Context (com.alibaba.csp.sentinel.context.Context)10 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)10 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)9 ArrayList (java.util.ArrayList)7 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)6 ClusterBuilderSlot (com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot)6 Collections (java.util.Collections)6 Assert (org.junit.Assert)6