1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

update sentinel test case

This commit is contained in:
fangjian0423 2019-03-26 17:37:36 +08:00
parent 5995687dd5
commit 46c888f878

View File

@ -106,14 +106,16 @@ public class SentinelAutoConfigurationTests {
@LocalServerPort @LocalServerPort
private int port; private int port;
private String url = "http://localhost:" + port; private String flowUrl = "http://localhost:" + port + "/flow";
private String degradeUrl = "http://localhost:" + port + "/degrade";
@Before @Before
public void setUp() { public void setUp() {
FlowRule rule = new FlowRule(); FlowRule rule = new FlowRule();
rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(0); rule.setCount(0);
rule.setResource("GET:" + url); rule.setResource("GET:" + flowUrl);
rule.setLimitApp("default"); rule.setLimitApp("default");
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT); rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
rule.setStrategy(RuleConstant.STRATEGY_DIRECT); rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
@ -121,7 +123,7 @@ public class SentinelAutoConfigurationTests {
DegradeRule degradeRule = new DegradeRule(); DegradeRule degradeRule = new DegradeRule();
degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT); degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
degradeRule.setResource("GET:" + url + "/test"); degradeRule.setResource("GET:" + degradeUrl);
degradeRule.setCount(0); degradeRule.setCount(0);
degradeRule.setTimeWindow(60); degradeRule.setTimeWindow(60);
DegradeRuleManager.loadRules(Arrays.asList(degradeRule)); DegradeRuleManager.loadRules(Arrays.asList(degradeRule));
@ -247,14 +249,14 @@ public class SentinelAutoConfigurationTests {
restTemplate.getInterceptors().size()); restTemplate.getInterceptors().size());
assertEquals("RestTemplateWithBlockClass interceptors size was wrong", 1, assertEquals("RestTemplateWithBlockClass interceptors size was wrong", 1,
restTemplateWithBlockClass.getInterceptors().size()); restTemplateWithBlockClass.getInterceptors().size());
ResponseEntity responseEntityBlock = restTemplateWithBlockClass.getForEntity(url, ResponseEntity responseEntityBlock = restTemplateWithBlockClass.getForEntity(flowUrl,
String.class); String.class);
assertEquals("RestTemplateWithBlockClass Sentinel Block Message was wrong", assertEquals("RestTemplateWithBlockClass Sentinel Block Message was wrong",
"Oops", responseEntityBlock.getBody()); "Oops", responseEntityBlock.getBody());
assertEquals( assertEquals(
"RestTemplateWithBlockClass Sentinel Block Http Status Code was wrong", "RestTemplateWithBlockClass Sentinel Block Http Status Code was wrong",
HttpStatus.OK, responseEntityBlock.getStatusCode()); HttpStatus.OK, responseEntityBlock.getStatusCode());
ResponseEntity responseEntityRaw = restTemplate.getForEntity(url, String.class); ResponseEntity responseEntityRaw = restTemplate.getForEntity(flowUrl, String.class);
assertEquals("RestTemplate Sentinel Block Message was wrong", assertEquals("RestTemplate Sentinel Block Message was wrong",
"RestTemplate request block by sentinel", responseEntityRaw.getBody()); "RestTemplate request block by sentinel", responseEntityRaw.getBody());
assertEquals("RestTemplate Sentinel Block Http Status Code was wrong", assertEquals("RestTemplate Sentinel Block Http Status Code was wrong",
@ -266,14 +268,14 @@ public class SentinelAutoConfigurationTests {
assertEquals("RestTemplateWithoutBlockClass interceptors size was wrong", 0, assertEquals("RestTemplateWithoutBlockClass interceptors size was wrong", 0,
restTemplateWithoutBlockClass.getInterceptors().size()); restTemplateWithoutBlockClass.getInterceptors().size());
assertThatExceptionOfType(RestClientException.class).isThrownBy(() -> { assertThatExceptionOfType(RestClientException.class).isThrownBy(() -> {
restTemplateWithoutBlockClass.getForEntity(url, String.class); restTemplateWithoutBlockClass.getForEntity(flowUrl, String.class);
}); });
} }
@Test @Test
public void testFallbackRestTemplate() { public void testFallbackRestTemplate() {
ResponseEntity responseEntity = restTemplateWithFallbackClass ResponseEntity responseEntity = restTemplateWithFallbackClass
.getForEntity(url + "/test", String.class); .getForEntity(degradeUrl, String.class);
assertEquals("RestTemplateWithFallbackClass Sentinel Message was wrong", assertEquals("RestTemplateWithFallbackClass Sentinel Message was wrong",
"Oops fallback", responseEntity.getBody()); "Oops fallback", responseEntity.getBody());
assertEquals("RestTemplateWithFallbackClass Sentinel Http Status Code was wrong", assertEquals("RestTemplateWithFallbackClass Sentinel Http Status Code was wrong",