Search in sources :

Example 6 with DefaultNode

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

the class FlowRuleCheckerTest method testSelectNodeForEmptyReference.

@Test
public void testSelectNodeForEmptyReference() {
    DefaultNode node = mock(DefaultNode.class);
    Context context = mock(Context.class);
    FlowRule rule = new FlowRule("testSelectNodeForEmptyReference").setCount(1).setStrategy(RuleConstant.STRATEGY_CHAIN);
    assertNull(FlowRuleChecker.selectReferenceNode(rule, context, node));
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 7 with DefaultNode

use of com.alibaba.csp.sentinel.node.DefaultNode 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 8 with DefaultNode

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

the class FlowRuleCheckerTest method testSelectReferenceNodeForContextEntrance.

@Test
public void testSelectReferenceNodeForContextEntrance() {
    String contextName = "good_context";
    DefaultNode node = mock(DefaultNode.class);
    Context context = mock(Context.class);
    FlowRule rule = new FlowRule("testSelectReferenceNodeForContextEntrance").setCount(1).setStrategy(RuleConstant.STRATEGY_CHAIN).setRefResource(contextName);
    when(context.getName()).thenReturn(contextName);
    assertEquals(node, FlowRuleChecker.selectReferenceNode(rule, context, node));
    when(context.getName()).thenReturn("other_context");
    assertNull(FlowRuleChecker.selectReferenceNode(rule, context, node));
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 9 with DefaultNode

use of com.alibaba.csp.sentinel.node.DefaultNode 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 10 with DefaultNode

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

the class FlowSlotTest method testCheckFlowPass.

@Test
@SuppressWarnings("unchecked")
public void testCheckFlowPass() throws Exception {
    FlowRuleChecker checker = mock(FlowRuleChecker.class);
    FlowSlot flowSlot = new FlowSlot(checker);
    Context context = mock(Context.class);
    DefaultNode node = mock(DefaultNode.class);
    doCallRealMethod().when(checker).checkFlow(any(Function.class), any(ResourceWrapper.class), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean());
    String resA = "resAK";
    String resB = "resBK";
    FlowRule rule1 = new FlowRule(resA).setCount(10);
    FlowRule rule2 = new FlowRule(resB).setCount(10);
    // Here we only load rules for resA.
    FlowRuleManager.loadRules(Collections.singletonList(rule1));
    when(checker.canPassCheck(eq(rule1), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean())).thenReturn(true);
    when(checker.canPassCheck(eq(rule2), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean())).thenReturn(false);
    flowSlot.checkFlow(new StringResourceWrapper(resA, EntryType.IN), context, node, 1, false);
    flowSlot.checkFlow(new StringResourceWrapper(resB, EntryType.IN), context, node, 1, false);
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Function(com.alibaba.csp.sentinel.util.function.Function) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Aggregations

DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)27 Context (com.alibaba.csp.sentinel.context.Context)15 Node (com.alibaba.csp.sentinel.node.Node)13 Test (org.junit.Test)13 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)12 Entry (com.alibaba.csp.sentinel.Entry)9 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)8 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)6 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)5 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)5 HashMap (java.util.HashMap)3 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 NodeVo (com.alibaba.csp.sentinel.command.vo.NodeVo)1 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)1