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