Search in sources :

Example 36 with Context

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

the class SentinelDubboConsumerFilterTest method verifyInvocationStructureForAsyncCall.

private void verifyInvocationStructureForAsyncCall(Invoker invoker, Invocation invocation) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    // As not call ContextUtil.enter(resourceName, application) in SentinelDubboConsumerFilter, use default context
    // In actual project, a consumer is usually also a provider, the context will be created by
    // SentinelDubboProviderFilter
    // If consumer is on the top of Dubbo RPC invocation chain, use default context
    String resourceName = consumerFilter.getMethodName(invoker, invocation, null);
    assertEquals(com.alibaba.csp.sentinel.Constants.CONTEXT_DEFAULT_NAME, context.getName());
    assertEquals("", context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(com.alibaba.csp.sentinel.Constants.CONTEXT_DEFAULT_NAME, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceName, EntryType.OUT);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(2, childList.size());
    DefaultNode interfaceNode = getNode(DubboUtils.getInterfaceName(invoker), entranceNode);
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(DubboUtils.getInterfaceName(invoker), interfaceResource.getName());
    assertSame(EntryType.OUT, interfaceResource.getEntryType());
    // As SphU.entry(resourceName, EntryType.OUT);
    childList = interfaceNode.getChildList();
    assertEquals(0, childList.size());
    DefaultNode methodNode = getNode(resourceName, entranceNode);
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(resourceName, methodResource.getName());
    assertSame(EntryType.OUT, methodResource.getEntryType());
    // Verify curEntry
    // nothing will bind to local context when use the AsyncEntry
    Entry curEntry = context.getCurEntry();
    assertNull(curEntry);
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    assertNotSame(methodClusterNode, // Different resource->Different ProcessorSlot->Different ClusterNode
    interfaceClusterNode);
    // As context origin is "", the StatisticNode should not be created in originCountMap of ClusterNode
    Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
    assertEquals(0, methodOriginCountMap.size());
    Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
    assertEquals(0, interfaceOriginCountMap.size());
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Entry(com.alibaba.csp.sentinel.Entry) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

Example 37 with Context

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

the class SentinelDubboConsumerFilterTest method testDegradeSync.

@Test
public void testDegradeSync() throws InterruptedException {
    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
    initDegradeRule(DubboUtils.getInterfaceName(invoker));
    Result result = invokeDubboRpc(false, invoker, invocation);
    verifyInvocationStructureForCallFinish(invoker, invocation);
    assertEquals("normal", result.getValue());
    // inc the clusterNode's exception to trigger the fallback
    for (int i = 0; i < 5; i++) {
        invokeDubboRpc(true, invoker, invocation);
        verifyInvocationStructureForCallFinish(invoker, invocation);
    }
    Result result2 = invokeDubboRpc(false, invoker, invocation);
    assertEquals("fallback", result2.getValue());
    // sleeping 1000 ms to reset exception
    Thread.sleep(1000);
    Result result3 = invokeDubboRpc(false, invoker, invocation);
    assertEquals("normal", result3.getValue());
    Context context = ContextUtil.getContext();
    assertNull(context);
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) Test(org.junit.Test) BaseTest(com.alibaba.csp.sentinel.BaseTest)

Example 38 with Context

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

the class SystemRuleTest method testSystemRule_avgRt.

@Test
public void testSystemRule_avgRt() throws BlockException {
    SystemRule systemRule = new SystemRule();
    systemRule.setAvgRt(4L);
    Context context = mock(Context.class);
    DefaultNode node = mock(DefaultNode.class);
    ClusterNode cn = mock(ClusterNode.class);
    when(context.getOrigin()).thenReturn("");
    when(node.getClusterNode()).thenReturn(cn);
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule) Test(org.junit.Test)

Example 39 with Context

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

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

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