如何解决Hystrix Javanica 不会超时并调用回退方法
我的服务中有一个方法可以访问下游服务以获取一些详细信息。我从来没有观察到 hystrix 超时,当下游花费更长的配置的 hystrix 超时时调用 fallback 方法。
所以,尝试编写一个单元测试用例来测试这个用例,但我很难这样做。我可以知道如何使用 JUnit 对 hystrix 超时进行单元测试吗?客户端使用 okhttp 进行改造。
这是我的示例代码
费用服务.java
public class FeesService {
@HystrixCommand(commandKey = "feesinfo",fallbackMethod="postFeesFallback",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")})
private ResponseData getResponse(Tracer clientTracer,RequestParam param,RequestContext requestContext) {
return retrofitHelper.syncCall(feesClient.postFees(getHeaders(clientTracer,requestContext),param),clientTracer);
}
private BisResponseData postFeesFallback(@HeaderMap Map<String,String> headers,@Body RequestParam param,RequestContext requestContext,Throwable e) throws Throwable {
// return somedata;
}
}
费用客户端.java
public interface BaggageFeesClient {
@POST("/fees/v4/getFees")
Call<BisResponseData> postFees(@HeaderMap Map<String,@Body RequestParam param);
}
public class FeesServiceTest {
@Test
public void testHystrixTimeoutIfDownstreamTookLongToRespond() {
final CountDownLatch waiter = new CountDownLatch(1);
when(retrofitHelper.syncCall(isA(Call.class),isA(Tracer.class)))
.thenAnswer(it -> {
try {
waiter.await(5,TimeUnit.SECONDS); // Simulating long Wait
} catch (InterruptedException e) {
e.printstacktrace();
}
// return responsedata;
});
ConfigurationManager.getConfigInstance().setProperty("hystrix.threadpool.default.coreSize",8);
ConfigurationManager.getConfigInstance()
.setProperty(
"hystrix.command.feesinfo.execution.isolation.thread.timeoutInMilliseconds",200);
System.setProperty("hystrix.command.feesinfo.execution.timeout.enabled","true");
Map<Long,ResponseData> data = FeesService.postFeesRequest(requestContext,airProductBookedType);
assertEquals(responseDataMap,data);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。