Browse Source

test: Update test cases with new degrade mechanism in sentinel-demo-quarkus

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 4 years ago
parent
commit
a5fe23ad63
3 changed files with 65 additions and 78 deletions
  1. +21
    -23
      sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/AppLifecycleBean.java
  2. +2
    -2
      sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingService.java
  3. +42
    -53
      sentinel-demo/sentinel-demo-quarkus/src/test/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingResourceTest.java

+ 21
- 23
sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/AppLifecycleBean.java View File

@@ -18,15 +18,16 @@ package com.alibaba.csp.sentinel.demo.quarkus;
import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;

import io.quarkus.runtime.StartupEvent; import io.quarkus.runtime.StartupEvent;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;


import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes; import javax.enterprise.event.Observes;

import java.util.Arrays; import java.util.Arrays;


/** /**
@@ -39,31 +40,28 @@ public class AppLifecycleBean {


void onStart(@Observes StartupEvent ev) { void onStart(@Observes StartupEvent ev) {
LOGGER.info("The application is starting..."); LOGGER.info("The application is starting...");
FlowRule rule = new FlowRule()
.setCount(1)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setResource("GET:/hello/txt")
.setLimitApp("default")
.as(FlowRule.class);
FlowRuleManager.loadRules(Arrays.asList(rule));


SystemRule systemRule = new SystemRule();
systemRule.setLimitApp("default");
systemRule.setAvgRt(3000);
SystemRuleManager.loadRules(Arrays.asList(systemRule));
// Only for test here. Actually it's recommended to configure rules via data-source.
FlowRule rule1 = new FlowRule()
.setCount(1)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setResource("GET:/hello/txt")
.setLimitApp("default")
.as(FlowRule.class);
FlowRule rule2 = new FlowRule("greeting2")
.setCount(1)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.as(FlowRule.class);
FlowRuleManager.loadRules(Arrays.asList(rule1, rule2));


DegradeRule degradeRule1 = new DegradeRule("greeting1") DegradeRule degradeRule1 = new DegradeRule("greeting1")
.setCount(1)
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
.setTimeWindow(10)
.setMinRequestAmount(1);
.setCount(1)
.setGrade(CircuitBreakerStrategy.ERROR_COUNT.getType())
.setTimeWindow(5)
.setStatIntervalMs(10000)
.setMinRequestAmount(1);


DegradeRule degradeRule2 = new DegradeRule("greeting2")
.setCount(1)
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
.setTimeWindow(10)
.setMinRequestAmount(1);
DegradeRuleManager.loadRules(Arrays.asList(degradeRule1, degradeRule2));
DegradeRuleManager.loadRules(Arrays.asList(degradeRule1));
} }


} }

+ 2
- 2
sentinel-demo/sentinel-demo-quarkus/src/main/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingService.java View File

@@ -43,7 +43,7 @@ public class GreetingService {
return "hello " + name; return "hello " + name;
} }


public String greetingFallback(String name) {
return "greetingFallback:" + name;
public String greetingFallback(String name, Throwable t) {
return "greetingFallback: " + t.getClass().getSimpleName();
} }
} }

+ 42
- 53
sentinel-demo/sentinel-demo-quarkus/src/test/java/com/alibaba/csp/sentinel/demo/quarkus/GreetingResourceTest.java View File

@@ -15,8 +15,8 @@
*/ */
package com.alibaba.csp.sentinel.demo.quarkus; package com.alibaba.csp.sentinel.demo.quarkus;


import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;

import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -35,71 +35,60 @@ public class GreetingResourceTest {
@Test @Test
public void testSentinelJaxRsQuarkusAdapter() { public void testSentinelJaxRsQuarkusAdapter() {
given() given()
.when().get("/hello/txt")
.then()
.statusCode(200)
.body(is("hello"));
.when().get("/hello/txt")
.then()
.statusCode(200)
.body(is("hello"));
given() given()
.when().get("/hello/txt")
.then()
.statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode())
.body(is("Blocked by Sentinel (flow limiting)"));
.when().get("/hello/txt")
.then()
.statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode())
.body(is("Blocked by Sentinel (flow limiting)"));
} }


@Test @Test
public void testSentinelAnnotationQuarkusAdapter() { public void testSentinelAnnotationQuarkusAdapter() {
given() given()
.when().get("/hello/fallback/a")
.then()
.statusCode(200)
.body(is("hello a"));
given()
.when().get("/hello/fallback/b")
.then()
.statusCode(200)
.body(is("hello b"));
given()
.when().get("/hello/fallback/degrade")
.then()
.statusCode(200)
.body(is("globalDefaultFallback, ex:test sentinel fallback"));
.when().get("/hello/fallback/a")
.then()
.statusCode(200)
.body(is("hello a"));
given() given()
.when().get("/hello/fallback/degrade")
.then()
.statusCode(200)
.body(is("globalBlockHandler, ex:null"));
.when().get("/hello/fallback/b")
.then()
.statusCode(200)
.body(is("hello b"));
given() given()
.when().get("/hello/fallback/a")
.then()
.statusCode(200)
.body(is("globalBlockHandler, ex:null"));

.when().get("/hello/fallback/degrade")
.then()
.statusCode(200)
.body(is("globalDefaultFallback, ex:test sentinel fallback"));
given() given()
.when().get("/hello/fallback2/a")
.then()
.statusCode(200)
.body(is("hello a"));
.when().get("/hello/fallback/degrade")
.then()
.statusCode(200)
.body(is("globalDefaultFallback, ex:test sentinel fallback"));
given() given()
.when().get("/hello/fallback2/b")
.then()
.statusCode(200)
.body(is("hello b"));
.when().get("/hello/fallback/degrade")
.then()
.statusCode(200)
.body(is("globalBlockHandler, ex:null"));
given() given()
.when().get("/hello/fallback2/degrade")
.then()
.statusCode(200)
.body(is("greetingFallback:degrade"));
.when().get("/hello/fallback/a")
.then()
.statusCode(200)
.body(is("globalBlockHandler, ex:null"));

given() given()
.when().get("/hello/fallback2/degrade")
.then()
.statusCode(200)
.body(is("greetingFallback:degrade"));
.when().get("/hello/fallback2/a")
.then()
.statusCode(200)
.body(is("hello a"));
given() given()
.when().get("/hello/fallback2/a")
.then()
.statusCode(200)
.body(is("greetingFallback:a"));

.when().get("/hello/fallback2/b")
.then()
.statusCode(200)
.body(is("greetingFallback: FlowException"));
} }


} }

Loading…
Cancel
Save