use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testFallBackPrivateMethod.
@Test
public void testFallBackPrivateMethod() throws Exception {
String resourceName = "apiFooWithFallback";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
try {
fooService.fooWithPrivateFallback(5758);
fail("should not reach here");
} catch (Exception ex) {
// Should not be traced.
assertThat(cn.exceptionQps()).isZero();
}
assertThat(fooService.fooWithPrivateFallback(5763)).isEqualTo("EEE...");
// Test for blockHandler
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
assertThat(fooService.fooWithPrivateFallback(2221)).isEqualTo("Oops, 2221");
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testBlockHandlerNotFound.
@Test(expected = UndeclaredThrowableException.class)
public void testBlockHandlerNotFound() {
assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
String resourceName = "apiBaz";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
fooService.baz("Sentinel");
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testNormalBlockHandlerAndFallback.
@Test
public void testNormalBlockHandlerAndFallback() throws Exception {
assertThat(fooService.foo(1)).isEqualTo("Hello for 1");
String resourceName = "apiFoo";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
// Test for biz exception.
try {
fooService.foo(5758);
fail("should not reach here");
} catch (Exception ex) {
// Should not be traced.
assertThat(cn.exceptionQps()).isZero();
}
try {
fooService.foo(5763);
fail("should not reach here");
} catch (Exception ex) {
assertThat(cn.exceptionQps()).isPositive();
}
// Test for blockHandler
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
assertThat(fooService.foo(1121)).isEqualTo("Oops, 1121");
assertThat(cn.blockQps()).isPositive();
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelAnnotationIntegrationTest method testDefaultFallbackWithSingleParam.
@Test
public void testDefaultFallbackWithSingleParam() {
assertThat(fooService.anotherFoo(1)).isEqualTo("Hello for 1");
String resourceName = "apiAnotherFooWithDefaultFallback";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
// Default fallback should take effect.
assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
assertThat(cn.exceptionQps()).isPositive();
assertThat(cn.blockQps()).isZero();
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
// Default fallback should also take effect for BlockException.
assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
assertThat(cn.blockQps()).isPositive();
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class PullConsumerDemo method initFlowControlRule.
private static void initFlowControlRule() {
FlowRule rule = new FlowRule();
rule.setResource(KEY);
// Indicates the interval between two adjacent requests is 200 ms.
rule.setCount(5);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setLimitApp("default");
// Enable rate limiting (uniform). This can ensure fixed intervals between two adjacent calls.
// In this example, intervals between two incoming calls (message consumption) will be 200 ms constantly.
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
// If more requests are coming, they'll be put into the waiting queue.
// The queue has a queueing timeout. Requests that may exceed the timeout will be immediately blocked.
// In this example, the max timeout is 5s.
rule.setMaxQueueingTimeMs(5 * 1000);
FlowRuleManager.loadRules(Collections.singletonList(rule));
}
Aggregations