我正在测试 Hystrix CircuitBreaker 实现。命令类如下所示:
public class CommandOne extends HystrixCommand<String>
{
private MyExternalService service;
public static int runCount = 0;
public CommandGetPunterUnpayoutExternalBets(MyExternalServoce service)
{
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("AAA"))
.andThreadPoolPropertiesDefaults(
HystrixThreadPoolProperties.Setter().
.withMetricsRollingStatisticalWindowInMilliseconds(10000))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerEnabled(true)
.withCircuitBreakerErrorThresholdPercentage(20)
.withCircuitBreakerRequestVolumeThreshold(10)
.withExecutionTimeoutInMilliseconds(30)
.withCircuitBreakerSleepWindowInMilliseconds(100000)));
this.service = service;
}
@Override
protected String run()
{
run++;
return service.callMethod();
}
@Override
protected String getFallback()
{
return "default;
}
}
命令的调用方式如下:
public class AnotherClass
{
private MyExternalServoce service;
public String callCmd()
{
CommandOne command = new CommandOne(service);
return command.execute();
}
}
在测试中我执行以下步骤:
@Test
public void test()
{
AnotherClass anotherClass = new AnotherClass();
// stubbing exception on my service
when(service.callMethod()).thenThrow(new RuntimeException());
for (int i = 0; i < 1000; i++)
{
anotherClass.callCmd();
}
System.out.println("Run method was called times = " + CommandOne.runCount);
}
我对给出的命令配置的期望是:MyExternalService.callMethod() 应该被调用 10 次(RequestVolumeThreshold),之后 100000 毫秒(长时间)不会被调用。在我的测试用例中,我期望 CommandOne.runCount = 10。
但实际上,我收到了 150 到 200 次 MyExternalService.callMethod() 调用(CommandOne.runCount = (150-200)。为什么会发生这种情况?我做错了什么?
根据 Hystrixdocs https://github.com/Netflix/Hystrix/wiki/Configuration#metricshealthsnapshotintervalinmilliseconds健康快照将每 500 毫秒拍摄一次(默认情况下)。这意味着 hystrix 在前 500 毫秒内发生的所有事情都不会影响断路器状态。在你的例子中你得到了随机值runCount
因为每次你的机器每 500 毫秒执行随机值的请求,并且只有在该时间间隔之后电路状态才会更新并关闭。
请看一个稍微简化的例子:
public class CommandOne extends HystrixCommand<String> {
private String content;
public static int runCount = 0;
public CommandOne(String s) {
super(Setter.withGroupKey
(HystrixCommandGroupKey.Factory.asKey("SnapshotIntervalTest"))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withCircuitBreakerSleepWindowInMilliseconds(500000)
.withCircuitBreakerRequestVolumeThreshold(9)
.withMetricsHealthSnapshotIntervalInMilliseconds(50)
.withMetricsRollingStatisticalWindowInMilliseconds(100000)
)
);
this.content = s;
}
@Override
public String run() throws Exception {
Thread.sleep(100);
runCount++;
if ("".equals(content)) {
throw new Exception();
}
return content;
}
@Override
protected String getFallback() {
return "FAILURE-" + content;
}
}
@Test
void test() {
for (int i = 0; i < 100; i++) {
CommandOne commandOne = new CommandOne();
commandOne.execute();
}
Assertions.assertEquals(10, CommandOne.runCount);
}
在此示例中我添加了:
-
withMetricsHealthSnapshotIntervalInMilliseconds(50)
允许 hystrix 每 50 毫秒拍摄一次快照。
-
Thread.sleep(100);
让请求慢一点,如果没有它,它们将快于 50 毫秒,我们将面临最初的问题。
尽管进行了所有这些修改,我还是看到了一些随机失败。之后我得出结论,像这样测试 hystrix 不是一个好主意。我们可以使用:
1) 通过手动设置开路/闭路状态实现回退/成功流程行为 https://stackoverflow.com/questions/29039360/any-samples-to-unit-test-fallback-using-hystrix-spring-cloud#answer-39378236.
2) 配置测试 https://stackoverflow.com/questions/38781398/test-drive-hystrix-circuit-breaker-configuration
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)