Search in sources :

Example 21 with DefaultNode

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();
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 22 with DefaultNode

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();
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 23 with DefaultNode

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();
}
Also used : EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Node(com.alibaba.csp.sentinel.node.Node) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 24 with DefaultNode

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));
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) Test(org.junit.Test)

Example 25 with DefaultNode

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));
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) 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