我正在与Javanica保持联系。我想测试断路器。关于我的条件:JBoss,SpringFramework(但不是Springboot!)。我已经配置了Javanica并且它工作,通过一个简单的方法调用测试,我强制打开断路器。我得到了正确的例外:
短路和后备失败
我正在尝试创建一个断路器测试,它在10个方法调用中给我“短路和后退失败”。我需要在哪里修复我的mockito测试?
我设置circuitBreaker.forceOpen =“true”并模拟我的服务。
import static org.mockito.Mockito.when;
public class HystrixCircleBreakerTest extends AbstractMockitoTest {
@Bean
private ServiceAdapter serviceAdapter;
@Mock
private Service service;
@Test
public void circuitBreakerTest() {
String errorMsg = "Timeout error";
final RuntimeException timeOutException = new RuntimeException(errorMsg);
when(service.getMediatorForContract(99177661)).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Thread.sleep(1000L);
throw timeOutException;
}
});
Exception circleBreaker = new Exception();
final String errorMsgCircuit = "Hystrix circuit short-circuited and is OPEN";
RuntimeException runtimeException = new RuntimeException(errorMsgCircuit);
for (int t = 0; t <= 10; t++) {
System.out.println("Servicecall: " + t);
try {
serviceAdapter.getMediatorForContract("99177661");
} catch (RuntimeException e) {
System.out.println("Exception: " + e.getMessage());
circleBreaker = e;
}
}
}
}
目前的结果:
Servicecall: 0
Exception: Timeout error
Servicecall: 1
Exception: Timeout error
Servicecall: 2
Exception: Timeout error
Servicecall: 3
Exception: Timeout error
Servicecall: 4
Exception: Timeout error
Servicecall: 5
Exception: Timeout error
Servicecall: 6
Exception: Timeout error
Servicecall: 7
Exception: Timeout error
Servicecall: 8
Exception: Timeout error
Servicecall: 9
Exception: Timeout error
Servicecall: 10
Exception: Timeout error
通常情况下,我应该在每次通话中得到“短路和后退失败”
这是因为servlet的概念。
当您使用mockito进行测试时,它将使用非servlet。那只是在内部应用环境下工作。
但是,您可能还记得在应用程序中设置已配置的servlet的Hystrix。
Hystrix需要servlet概念,它是从其他客户端请求的。 servlet捕获请求并暂停哪个超时或错误请求...
我建议您编写python脚本或其他脚本,然后请求与Hystrix一起安装的应用程序。