use of com.alibaba.csp.sentinel.node.DefaultNode in project Sentinel by alibaba.
the class ClusterNodeBuilderTest method clusterNodeBuilder_normal.
@Test
public void clusterNodeBuilder_normal() throws Exception {
ContextUtil.enter("entry1", "caller1");
Entry nodeA = SphU.entry("nodeA");
Node curNode = nodeA.getCurNode();
assertSame(curNode.getClass(), DefaultNode.class);
DefaultNode dN = (DefaultNode) curNode;
assertTrue(dN.getClusterNode().getOriginCountMap().containsKey("caller1"));
assertSame(nodeA.getOriginNode(), dN.getClusterNode().getOrCreateOriginNode("caller1"));
if (nodeA != null) {
nodeA.exit();
}
ContextUtil.exit();
ContextUtil.enter("entry4", "caller2");
nodeA = SphU.entry("nodeA");
curNode = nodeA.getCurNode();
assertSame(curNode.getClass(), DefaultNode.class);
DefaultNode dN1 = (DefaultNode) curNode;
assertTrue(dN1.getClusterNode().getOriginCountMap().containsKey("caller2"));
assertNotSame(dN1, dN);
if (nodeA != null) {
nodeA.exit();
}
ContextUtil.exit();
}
use of com.alibaba.csp.sentinel.node.DefaultNode in project Sentinel by alibaba.
the class NodeSelectorTest method testMultipleEntrance.
@Test
public void testMultipleEntrance() throws Exception {
final String firstEntry = "entry_multiple_one";
final String anotherEntry = "entry_multiple_another";
final String resName = "nodeA";
Node firstNode, anotherNode;
ContextUtil.enter(firstEntry);
Entry nodeA = SphU.entry(resName);
firstNode = ContextUtil.getContext().getCurNode();
if (nodeA != null) {
nodeA.exit();
}
ContextUtil.exit();
ContextUtil.enter(anotherEntry);
nodeA = SphU.entry(resName);
anotherNode = ContextUtil.getContext().getCurNode();
if (nodeA != null) {
nodeA.exit();
}
assertNotSame(firstNode, anotherNode);
for (Node node : Constants.ROOT.getChildList()) {
EntranceNode firstEntrance = (EntranceNode) node;
if (firstEntrance.getId().getName().equals(firstEntry)) {
assertEquals(1, firstEntrance.getChildList().size());
for (Node child : firstEntrance.getChildList()) {
assertEquals(resName, ((DefaultNode) child).getId().getName());
}
} else if (firstEntrance.getId().getName().equals(anotherEntry)) {
assertEquals(1, firstEntrance.getChildList().size());
for (Node child : firstEntrance.getChildList()) {
assertEquals(resName, ((DefaultNode) child).getId().getName());
}
} else {
System.out.println("Multiple entries: " + firstEntrance.getId().getName());
}
}
ContextUtil.exit();
}
use of com.alibaba.csp.sentinel.node.DefaultNode in project Sentinel by alibaba.
the class NodeSelectorTest method testSingleEntrance.
@Test
public void testSingleEntrance() throws Exception {
final String contextName = "entry_SingleEntrance";
ContextUtil.enter(contextName);
EntranceNode entranceNode = null;
for (Node node : Constants.ROOT.getChildList()) {
entranceNode = (EntranceNode) node;
if (entranceNode.getId().getName().equals(contextName)) {
break;
} else {
System.out.println("Single entry: " + entranceNode.getId().getName());
}
}
assertNotNull(entranceNode);
assertTrue(entranceNode.getId().getName().equalsIgnoreCase(contextName));
final String resName = "nodeA";
Entry nodeA = SphU.entry(resName);
assertNotNull(ContextUtil.getContext().getCurNode());
assertEquals(resName, ((DefaultNode) ContextUtil.getContext().getCurNode()).getId().getName());
boolean hasNode = false;
for (Node node : entranceNode.getChildList()) {
if (((DefaultNode) node).getId().getName().equals(resName)) {
hasNode = true;
}
}
assertTrue(hasNode);
if (nodeA != null) {
nodeA.exit();
}
ContextUtil.exit();
}
use of com.alibaba.csp.sentinel.node.DefaultNode in project Sentinel by alibaba.
the class FlowRuleCheckerTest method testSelectNodeForRelateReference.
@Test
public void testSelectNodeForRelateReference() {
String refResource = "testSelectNodeForRelateReference_refResource";
DefaultNode node = mock(DefaultNode.class);
ClusterNode refCn = mock(ClusterNode.class);
ClusterBuilderSlot.getClusterNodeMap().put(new StringResourceWrapper(refResource, EntryType.IN), refCn);
Context context = mock(Context.class);
FlowRule rule = new FlowRule("testSelectNodeForRelateReference").setCount(1).setStrategy(RuleConstant.STRATEGY_RELATE).setRefResource(refResource);
assertEquals(refCn, FlowRuleChecker.selectReferenceNode(rule, context, node));
}
use of com.alibaba.csp.sentinel.node.DefaultNode in project Sentinel by alibaba.
the class FlowRuleCheckerTest method testPassCheckSelectEmptyNodeSuccess.
@Test
public void testPassCheckSelectEmptyNodeSuccess() {
FlowRule rule = new FlowRule("abc").setCount(1);
rule.setLimitApp("abc");
DefaultNode node = mock(DefaultNode.class);
Context context = mock(Context.class);
when(context.getOrigin()).thenReturn("def");
FlowRuleChecker checker = new FlowRuleChecker();
assertTrue(checker.canPassCheck(rule, context, node, 1));
}
Aggregations