【发布时间】:2018-09-16 10:28:50
【问题描述】:
我正在测试 Hystrix 断路器的实现。这是命令类的样子:
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) 的调用。为什么会这样?我做错了什么?
【问题讨论】:
标签: java junit hystrix circuit-breaker