use of com.alibaba.csp.sentinel.node.ClusterNode 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.node.ClusterNode in project Sentinel by alibaba.
the class MetricCollector method collectMetric.
/**
* collect the metrics in {@link MetricNode}.
*
* @return the metric grouped by resource name.
*/
public Map<String, MetricNode> collectMetric() {
final long currentTime = TimeUtil.currentTimeMillis();
final long maxTime = currentTime - currentTime % 1000;
final long minTime = maxTime - 1000;
Map<String, MetricNode> metricNodeMap = new HashMap<>();
for (Map.Entry<ResourceWrapper, ClusterNode> e : ClusterBuilderSlot.getClusterNodeMap().entrySet()) {
ClusterNode node = e.getValue();
List<MetricNode> metrics = getLastMetrics(node, minTime, maxTime);
aggregate(metricNodeMap, metrics, node);
}
aggregate(metricNodeMap, getLastMetrics(Constants.ENTRY_NODE, minTime, maxTime), Constants.ENTRY_NODE);
return metricNodeMap;
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationInterceptorIntegrationTest method testAnnotationExceptionsToIgnore.
@Test
public void testAnnotationExceptionsToIgnore() {
assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
String resourceName = "apiBaz";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
try {
fooService.baz("fail");
fail("should not reach here");
} catch (IllegalMonitorStateException ex) {
assertThat(cn.exceptionQps()).isZero();
}
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationInterceptorIntegrationTest 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.node.ClusterNode in project Sentinel by alibaba.
the class SentinelAnnotationInterceptorIntegrationTest method testFallbackWithNoParams.
@Test
public void testFallbackWithNoParams() throws Exception {
assertThat(fooService.fooWithFallback(1)).isEqualTo("Hello for 1");
String resourceName = "apiFooWithFallback";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertThat(cn).isNotNull();
assertThat(cn.passQps()).isPositive();
// Fallback should be ignored for this.
try {
fooService.fooWithFallback(5758);
fail("should not reach here");
} catch (IllegalAccessException e) {
assertThat(cn.exceptionQps()).isZero();
}
// Fallback should take effect.
assertThat(fooService.fooWithFallback(5763)).isEqualTo("eee...");
assertThat(cn.exceptionQps()).isPositive();
assertThat(cn.blockQps()).isZero();
FlowRuleManager.loadRules(Collections.singletonList(new FlowRule(resourceName).setCount(0)));
// Fallback should not take effect for BlockException, as blockHandler is configured.
assertThat(fooService.fooWithFallback(2221)).isEqualTo("Oops, 2221");
assertThat(cn.blockQps()).isPositive();
}
Aggregations