mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Update test cases of spring-cloud-circuitbreaker-sentinel
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
b03cd7efb5
commit
5b2f68e292
@ -82,7 +82,7 @@ public class ReactiveSentinelCircuitBreakerIntegrationTest {
|
|||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recovered.
|
// Half-open recovery (will re-open the circuit breaker).
|
||||||
StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
|
StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
|
||||||
|
|
||||||
StepVerifier.create(service.normalFlux()).expectNext("normalflux")
|
StepVerifier.create(service.normalFlux()).expectNext("normalflux")
|
||||||
@ -99,7 +99,7 @@ public class ReactiveSentinelCircuitBreakerIntegrationTest {
|
|||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recovered.
|
// Half-open recovery (will re-open the circuit breaker).
|
||||||
StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete();
|
StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ public class ReactiveSentinelCircuitBreakerIntegrationTest {
|
|||||||
|
|
||||||
@GetMapping("/slow")
|
@GetMapping("/slow")
|
||||||
public Mono<String> slow() {
|
public Mono<String> slow() {
|
||||||
return Mono.just("slow").delayElement(Duration.ofMillis(500));
|
return Mono.just("slow").delayElement(Duration.ofMillis(80));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/normal")
|
@GetMapping("/normal")
|
||||||
@ -120,7 +120,7 @@ public class ReactiveSentinelCircuitBreakerIntegrationTest {
|
|||||||
|
|
||||||
@GetMapping("/slow_flux")
|
@GetMapping("/slow_flux")
|
||||||
public Flux<String> slowFlux() {
|
public Flux<String> slowFlux() {
|
||||||
return Flux.just("slow", "flux").delayElements(Duration.ofMillis(500));
|
return Flux.just("slow", "flux").delayElements(Duration.ofMillis(80));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("normal_flux")
|
@GetMapping("normal_flux")
|
||||||
@ -133,19 +133,19 @@ public class ReactiveSentinelCircuitBreakerIntegrationTest {
|
|||||||
return factory -> {
|
return factory -> {
|
||||||
factory.configure(builder -> builder
|
factory.configure(builder -> builder
|
||||||
.rules(Collections.singletonList(new DegradeRule("slow_mono")
|
.rules(Collections.singletonList(new DegradeRule("slow_mono")
|
||||||
.setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
|
.setCount(50).setSlowRatioThreshold(0.7).setMinRequestAmount(5)
|
||||||
.setTimeWindow(5))),
|
.setStatIntervalMs(30000).setTimeWindow(5))),
|
||||||
"slow_mono");
|
"slow_mono");
|
||||||
factory.configure(builder -> builder
|
factory.configure(builder -> builder
|
||||||
.rules(Collections.singletonList(new DegradeRule("slow_flux")
|
.rules(Collections.singletonList(new DegradeRule("slow_mono")
|
||||||
.setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
|
.setCount(50).setSlowRatioThreshold(0.7).setMinRequestAmount(5)
|
||||||
.setTimeWindow(5))),
|
.setStatIntervalMs(30000).setTimeWindow(5))),
|
||||||
"slow_flux");
|
"slow_flux");
|
||||||
factory.configureDefault(id -> new SentinelConfigBuilder()
|
factory.configureDefault(id -> new SentinelConfigBuilder()
|
||||||
.resourceName(id)
|
.resourceName(id)
|
||||||
.rules(Collections.singletonList(new DegradeRule(id)
|
.rules(Collections.singletonList(new DegradeRule(id)
|
||||||
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
|
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
|
||||||
.setCount(0.5).setTimeWindow(10)))
|
.setCount(5).setTimeWindow(10)))
|
||||||
.build());
|
.build());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@ -59,21 +60,24 @@ public class SentinelCircuitBreakerIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSlow() throws Exception {
|
public void testSlow() throws Exception {
|
||||||
// The first 5 requests should pass.
|
assertThat(service.slow(true)).isEqualTo("slow");
|
||||||
assertThat(service.slow()).isEqualTo("slow");
|
assertThat(service.slow(true)).isEqualTo("slow");
|
||||||
assertThat(service.slow()).isEqualTo("slow");
|
assertThat(service.slow(true)).isEqualTo("slow");
|
||||||
assertThat(service.slow()).isEqualTo("slow");
|
assertThat(service.slow(false)).isEqualTo("slow");
|
||||||
assertThat(service.slow()).isEqualTo("slow");
|
assertThat(service.slow(false)).isEqualTo("slow");
|
||||||
assertThat(service.slow()).isEqualTo("slow");
|
assertThat(service.slow(true)).isEqualTo("slow");
|
||||||
|
assertThat(service.slow(true)).isEqualTo("slow");
|
||||||
|
|
||||||
// Then in the next 10s, the fallback method should be called.
|
// Then in the next 10s, the fallback method should be called.
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
assertThat(service.slow()).isEqualTo("fallback");
|
assertThat(service.slow(true)).isEqualTo("fallback");
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try a normal request.
|
||||||
|
assertThat(service.slow(false)).isEqualTo("slow");
|
||||||
// Recovered.
|
// Recovered.
|
||||||
assertThat(service.slow()).isEqualTo("slow");
|
assertThat(service.slow(true)).isEqualTo("slow");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -97,8 +101,10 @@ public class SentinelCircuitBreakerIntegrationTest {
|
|||||||
protected static class Application {
|
protected static class Application {
|
||||||
|
|
||||||
@GetMapping("/slow")
|
@GetMapping("/slow")
|
||||||
public String slow() throws InterruptedException {
|
public String slow(@RequestParam(required = false) Boolean slow) throws InterruptedException {
|
||||||
Thread.sleep(500);
|
if (slow == null || slow) {
|
||||||
|
Thread.sleep(80);
|
||||||
|
}
|
||||||
return "slow";
|
return "slow";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,14 +118,15 @@ public class SentinelCircuitBreakerIntegrationTest {
|
|||||||
String slowId = "slow";
|
String slowId = "slow";
|
||||||
List<DegradeRule> rules = Collections.singletonList(
|
List<DegradeRule> rules = Collections.singletonList(
|
||||||
new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
|
new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
|
||||||
.setCount(100).setTimeWindow(10));
|
.setCount(50).setSlowRatioThreshold(0.7).setMinRequestAmount(5)
|
||||||
|
.setStatIntervalMs(30000).setTimeWindow(5));
|
||||||
return factory -> {
|
return factory -> {
|
||||||
factory.configure(builder -> builder.rules(rules), slowId);
|
factory.configure(builder -> builder.rules(rules), slowId);
|
||||||
factory.configureDefault(id -> new SentinelConfigBuilder()
|
factory.configureDefault(id -> new SentinelConfigBuilder()
|
||||||
.resourceName(id)
|
.resourceName(id)
|
||||||
.rules(Collections.singletonList(new DegradeRule(id)
|
.rules(Collections.singletonList(new DegradeRule(id)
|
||||||
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
|
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
|
||||||
.setCount(0.5).setTimeWindow(10)))
|
.setCount(10).setStatIntervalMs(10000).setTimeWindow(10)))
|
||||||
.build());
|
.build());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -137,9 +144,9 @@ public class SentinelCircuitBreakerIntegrationTest {
|
|||||||
this.cbFactory = cbFactory;
|
this.cbFactory = cbFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String slow() {
|
public String slow(boolean slow) {
|
||||||
return cbFactory.create("slow").run(
|
return cbFactory.create("slow").run(
|
||||||
() -> rest.getForObject("/slow", String.class), t -> "fallback");
|
() -> rest.getForObject("/slow?slow=" + slow, String.class), t -> "fallback");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String normal() {
|
public String normal() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user