vendredi 1 novembre 2019

Unit test for Request Timeout

I have a need to write an integration between three applications. Two of these applications I don’t have access to the code for because they are third party applications. I’ll refer to these apps as third party app #1 and third party app #2. The integration between these two apps and my application should look like the following on a high level.

enter image description here

However, I’m currently seeing the following behavior:

enter image description here

My application is a spring boot app with an endpoint that, when called, synchronously makes a PATCH request using a rest template to a third party application #2’s rest API. However, I am occasionally seeing an issue where the request to the third party application takes an unacceptable amount of time to fail (up to 8 hours). When this occurs, the logs for application #1 show it received “No Response”. I intend to write a fix for this problem by adding a timeout to the request from my application to third party application #2 (as described here https://howtodoinjava.com/spring-boot2/resttemplate/resttemplate-timeout-example/). Doing this should at least guarantee a status code is returned to third party application #1.

To summarize, I want to write a test simulate the request to the third party application #2 failing after 5 seconds. When the request fails, I want to return a specific status code to the original application which called the endpoint. I’m going to try to write a fix for this issue by implementing a timeout for the request represented by arrow #2. However, Im unsure how to adequately write a test that simulates this issue and verifies that we are seeing the correct behaviour. My goal is to write a test that checks that my application returns an error with a response code to third party application #1 on encountering the scenario where the request to third party application #2 lasts longer than a given time period (maybe 5 seconds). That said, I have seen people suggest things like this to ensure a test request takes a specified amount of time.

this.mockMvc.perform(put(expectedUrl))
    .andDo((result) -> {
        Thread.sleep(6000);
    }).andReturn();

However my fix for this issue will be to set several properties on the request factory being used by the rest template itself (as described in the provided link) and I don’t feel like doing the above is an adequate test of this behavior as we never actually verify that (though I could be wrong). In short how can I write a test that checks that a request times out after a set period of time? Thanks in advance for any guidance or assistance.

Aucun commentaire:

Enregistrer un commentaire