Search in sources :

Example 11 with Context

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

the class SentinelDubboConsumerFilterTest method testDegradeAsync.

@Test
public void testDegradeAsync() throws InterruptedException {
    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
    when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
    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 12 with Context

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

the class SentinelDubboConsumerFilterTest method testInvokeSync.

@Test
public void testInvokeSync() {
    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(result.getException()).thenReturn(new Exception());
    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructure(invoker, invocation);
        return result;
    });
    consumerFilter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);
    Context context = ContextUtil.getContext();
    assertNull(context);
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) Test(org.junit.Test) BaseTest(com.alibaba.csp.sentinel.BaseTest)

Example 13 with Context

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

the class SentinelDubboConsumerFilterTest method testInvokeAsync.

@Test
public void testInvokeAsync() {
    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();
    when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructureForAsyncCall(invoker, invocation);
        return result;
    });
    consumerFilter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);
    Context context = ContextUtil.getContext();
    assertNotNull(context);
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) Test(org.junit.Test) BaseTest(com.alibaba.csp.sentinel.BaseTest)

Example 14 with Context

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

the class FlowRuleCheckerTest method testCustomOriginFlowSelectNode.

@Test
public void testCustomOriginFlowSelectNode() {
    String origin = "appA";
    String limitAppB = "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.getOrigin()).thenReturn(origin);
    when(context.getOriginNode()).thenReturn(originNode);
    FlowRule rule = new FlowRule("testCustomOriginFlowSelectNode").setCount(1);
    rule.setLimitApp(origin);
    // Origin matches, return the origin node.
    assertEquals(originNode, FlowRuleChecker.selectNodeByRequesterAndStrategy(rule, context, node));
    rule.setLimitApp(limitAppB);
    // Origin mismatch, no node found.
    assertNull(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 15 with Context

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

the class FlowRuleCheckerTest method testSelectNodeForEmptyReference.

@Test
public void testSelectNodeForEmptyReference() {
    DefaultNode node = mock(DefaultNode.class);
    Context context = mock(Context.class);
    FlowRule rule = new FlowRule("testSelectNodeForEmptyReference").setCount(1).setStrategy(RuleConstant.STRATEGY_CHAIN);
    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)

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