Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an integration test to check if my circuit breaker fallback is called?

In my application, I need to call an external endpoint and if it is too slow a fallback is activated.

The following code is an example of how my app looks like:

@FeignClient(name = "${config.name}", url = "${config.url:}", fallback = ExampleFallback.class)
public interface Example {
@RequestMapping(method = RequestMethod.GET, value = "/endpoint", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    MyReturnObject find(@RequestParam("myParam") String myParam);
}

And its fallback implementation:

@Component
public Class ExampleFallback implements Example {

    private final FallbackService fallback;

    @Autowired
    public ExampleFallback(final FallbackService fallback) {
        this.fallback = fallback;
    }

    @Override
    public MyReturnObject find(final String myParam) {
        return fallback.find(myParam);
    }

Also, a configured timeout for circuit breaker: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

How can I implement an integration test to check if my circuit break is working, i.e, if my endpoint (mocked in that case) is slow or if it returns an error like 4xx or 5xx?

I'm using Spring Boot 1.5.3 with Spring Cloud (Feign + Hystrix)

like image 237
leonnardo Avatar asked Nov 28 '17 13:11

leonnardo


1 Answers

Note i donot know Feign or Hystrix.

In my opinion it is problematic to implement an automated integrationtest that simulates different implementatondetails of Feign+Hystrix - this implementation detail can change at any time. There are many different types of failure: primary-Endpoint not reachable, illegal data (i.e. receiving a html-errormessage, when exprecting xml data in a special format), disk-full, .....

if you mock an endpoint you make an assumption of implementationdetail of Feign+Hystrix how the endpoint behaves in a errorsituation (i.e. return null, return some specific errorcode, throw an exception of type Xyz....)

i would create only one automated integration test with a real primary-enpoint that has a never reachable url and a mocked-fallback-endpoint where you verify that the processed data comes from the mock. This automated test assumes that handling of "networkconnection too slow" is the same as "url-notfound" from your app-s point of view.

For all other tests i would create a thin wrapper interface around Feign+Hystrix where you mock Feign+Hystrix. This way you can automatically test for example what happens if you receive 200bytes from primary interface and then get an expetion.

For details about hiding external dependencies see onion-architecture

like image 62
k3b Avatar answered Sep 28 '22 06:09

k3b