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