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