Search in sources :

Example 41 with Context

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

the class FlowSlotTest method testCheckFlowBlock.

@Test(expected = FlowException.class)
@SuppressWarnings("unchecked")
public void testCheckFlowBlock() 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";
    FlowRule rule = new FlowRule(resA).setCount(10);
    FlowRuleManager.loadRules(Collections.singletonList(rule));
    when(checker.canPassCheck(any(FlowRule.class), any(Context.class), any(DefaultNode.class), anyInt(), anyBoolean())).thenReturn(false);
    flowSlot.checkFlow(new StringResourceWrapper(resA, 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 42 with Context

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

the class MetricEntryCallbackTest method onBlocked.

@Test
public void onBlocked() throws Exception {
    FakeMetricExtension extension = new FakeMetricExtension();
    FakeAdvancedMetricExtension advancedExtension = new FakeAdvancedMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);
    MetricExtensionProvider.addMetricExtension(advancedExtension);
    MetricEntryCallback entryCallback = new MetricEntryCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn("origin1");
    int count = 2;
    Object[] args = { "args1", "args2" };
    entryCallback.onBlocked(new FlowException("xx"), context, resourceWrapper, null, count, args);
    // assert extension
    Assert.assertEquals(extension.block, count);
    // assert advancedExtension
    Assert.assertEquals(advancedExtension.block, count);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) Test(org.junit.Test)

Example 43 with Context

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

the class MetricExitCallbackTest method advancedExtensionOnExit.

/**
 * @author bill_yip
 */
@Test
public void advancedExtensionOnExit() {
    FakeAdvancedMetricExtension extension = new FakeAdvancedMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);
    MetricExitCallback exitCallback = new MetricExitCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    int count = 2;
    Object[] args = { "args1", "args2" };
    long prevRt = 20;
    extension.rt = prevRt;
    extension.complete = 6;
    extension.concurrency = 10;
    Context context = mock(Context.class);
    Entry entry = mock(Entry.class);
    // Mock current time
    long curMillis = System.currentTimeMillis();
    setCurrentMillis(curMillis);
    int deltaMs = 100;
    when(entry.getError()).thenReturn(null);
    when(entry.getCreateTimestamp()).thenReturn(curMillis - deltaMs);
    when(context.getCurEntry()).thenReturn(entry);
    exitCallback.onExit(context, resourceWrapper, count, args);
    Assert.assertEquals(prevRt + deltaMs, extension.rt);
    Assert.assertEquals(extension.complete, 6 + count);
    Assert.assertEquals(extension.concurrency, 10 - 1);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) Entry(com.alibaba.csp.sentinel.Entry) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 44 with Context

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

the class SentinelSofaRpcConsumerFilterTest method verifyInvocationStructure.

/**
 * Verify Sentinel invocation structure in memory:
 * EntranceNode(defaultContextName)
 * --InterfaceNode(interfaceName)
 * ----MethodNode(resourceName)
 */
private void verifyInvocationStructure(String interfaceResourceName, String methodResourceName) {
    Context context = ContextUtil.getContext();
    assertNotNull(context);
    // As not call ContextUtil.enter(methodResourceName, applicationName) in SentinelSofaRpcConsumerFilter, use default context
    // In actual project, a consumer is usually also a provider, the context will be created by SentinelSofaRpcProviderFilter
    // If consumer is on the top of SOFARPC invocation chain, use default context
    assertEquals(Constants.CONTEXT_DEFAULT_NAME, context.getName());
    assertEquals("", context.getOrigin());
    DefaultNode entranceNode = context.getEntranceNode();
    ResourceWrapper entranceResource = entranceNode.getId();
    assertEquals(Constants.CONTEXT_DEFAULT_NAME, entranceResource.getName());
    assertSame(EntryType.IN, entranceResource.getEntryType());
    // As SphU.entry(interfaceResourceName, EntryType.OUT);
    Set<Node> childList = entranceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper interfaceResource = interfaceNode.getId();
    assertEquals(interfaceResourceName, interfaceResource.getName());
    assertSame(EntryType.OUT, interfaceResource.getEntryType());
    // As SphU.entry(methodResourceName, EntryType.OUT);
    childList = interfaceNode.getChildList();
    assertEquals(1, childList.size());
    DefaultNode methodNode = (DefaultNode) childList.iterator().next();
    ResourceWrapper methodResource = methodNode.getId();
    assertEquals(methodResourceName, methodResource.getName());
    assertSame(EntryType.OUT, methodResource.getEntryType());
    // Verify curEntry
    Entry curEntry = context.getCurEntry();
    assertSame(methodNode, curEntry.getCurNode());
    assertSame(interfaceNode, curEntry.getLastNode());
    // As context origin is not "", no originNode should be created in curEntry
    assertNull(curEntry.getOriginNode());
    // Verify clusterNode
    ClusterNode methodClusterNode = methodNode.getClusterNode();
    ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
    // Different resource->Different ProcessorSlot->Different ClusterNode
    assertNotSame(methodClusterNode, 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) Node(com.alibaba.csp.sentinel.node.Node) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultNode(com.alibaba.csp.sentinel.node.DefaultNode) StatisticNode(com.alibaba.csp.sentinel.node.StatisticNode)

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