我正在测试 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)。为什么会发生这种情况?我做错了什么?