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