Search in sources :

Example 16 with Context

use of com.alibaba.csp.sentinel.context.Context 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 17 with Context

use of com.alibaba.csp.sentinel.context.Context 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 18 with Context

use of com.alibaba.csp.sentinel.context.Context 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 19 with Context

use of com.alibaba.csp.sentinel.context.Context 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)

Example 20 with Context

use of com.alibaba.csp.sentinel.context.Context in project Sentinel by alibaba.

the class AsyncEntry method cleanCurrentEntryInLocal.

/**
 * Remove current entry from local context, but does not exit.
 */
void cleanCurrentEntryInLocal() {
    if (context instanceof NullContext) {
        return;
    }
    Context originalContext = context;
    if (originalContext != null) {
        Entry curEntry = originalContext.getCurEntry();
        if (curEntry == this) {
            Entry parent = this.parent;
            originalContext.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry) parent).child = null;
            }
        } else {
            String curEntryName = curEntry == null ? "none" : curEntry.resourceWrapper.getName() + "@" + curEntry.hashCode();
            String msg = String.format("Bad async context state, expected entry: %s, but actual: %s", getResourceWrapper().getName() + "@" + hashCode(), curEntryName);
            throw new IllegalStateException(msg);
        }
    }
}
Also used : NullContext(com.alibaba.csp.sentinel.context.NullContext) Context(com.alibaba.csp.sentinel.context.Context) NullContext(com.alibaba.csp.sentinel.context.NullContext)

Aggregations

Context (com.alibaba.csp.sentinel.context.Context)44 Test (org.junit.Test)29 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)17 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)15 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)10 Entry (com.alibaba.csp.sentinel.Entry)9 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)9 NullContext (com.alibaba.csp.sentinel.context.NullContext)8 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)7 BaseTest (com.alibaba.csp.sentinel.BaseTest)6 Node (com.alibaba.csp.sentinel.node.Node)6 StatisticNode (com.alibaba.csp.sentinel.node.StatisticNode)5 DemoService (com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService)4 Method (java.lang.reflect.Method)4 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 Invocation (com.alibaba.dubbo.rpc.Invocation)2 Invoker (com.alibaba.dubbo.rpc.Invoker)2 Result (com.alibaba.dubbo.rpc.Result)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2