0

My rest microservice (spring boot) invokes a call on third party api service that may take long time to return hence I want to implement timeout so that the long running calls to this third party service does not cause app crashes. I have looked at various examples. Most say of using spring.mvc.async.request-timeout that can be set in properties file and the conroller end point returning callable. I am using cassandra DB and I noticed that when I run the test case to test the scenario , I get cassandra connection issue. When I remove the callable the JUnit test case runs but with no timeout.

Here is my controller end point and Junit test

@PostMapping("/deposit-accounts")
public Callable<ResponseEntity<DepositResponse>> 
getAccountCapabilities(@Valid @RequestBody DepositRequest request) {
           return () -> {
        final DepositResponse response = 
depositService.getCapabilities(request);
        if (response.getCapabilities().size() > 1 &&
                response.getCapabilities().stream()
                        .filter(c -> 
  ResponseCodes.INTERNAL_HOST_ERROR.equals(c.getResponseCode())).count() > 0) {
            return ResponseEntity.status(207).body(response);
        }
        return ResponseEntity.ok(response);
    };
}


@Test
@DisplayName("testResponseDelay")
void testResponseDelay() throws Exception
{
    String request = 
    readResourceFileToString("payloads/depositaccount/request.json");
    String casAPIResponse =
    readResourceFileToString
    ("payloads/depositaccount/cas/response.xml");
   
    mockCASApi.enqueue(new MockResponse()
    .setBody(casAPIResponse)
    .addHeader("Content-Type", TEXT_XML_VALUE));

    this.mockMvc
    .perform(post("/payments/capabilities/deposit")
    .contentType(MediaType.APPLICATION_JSON).content(request))
    .andExpect(status().is(200));
}

I am unable to figure out why callable causes the cassandra connection issue? Is above code right way of invoking callable?

For third party service invocation I use RestTemplate. Any pointers why the cassandra connection issue ? Is it related to using callable? is my usage of callable correct? Cassandra is run locally for Junit test.

The spring.mvc.async.request-timeout property is set in application yaml file.

mvc: async: request-timeout: 5000

My JUnit test case is as below:

thanks

pash
  • 15
  • 2
  • 7
  • Do you have a log of the cassandra connection issue? I don't have experience with Callable, but according to [Baeldung](spring.mvc.async.request-timeout) your code is fine. Maybe you can also post an MVP for testing. – Nick Aug 16 '23 at 18:17
  • Link was supposed to point here: https://www.baeldung.com/spring-rest-timeout#spring-mvc-request-timeout – Nick Aug 16 '23 at 18:24
  • I do not see any stack trace for cassandra connection issue. The test case fails but when I debug the test case , I do see it throws cassandra connection issue and error is "No Node was available to execute the query". What is MVP? – pash Aug 16 '23 at 18:27
  • An MVP is "Minimal Viable Product", in this case a the minimal code required to reproduce the issue(e.g. I don't know what MDC is, is it related to your question? If not, then you probably don't want it cluttering your code here). When you say test case do you mean literal @Test cases or firing up the application and sending some requests? – Nick Aug 16 '23 at 18:32
  • Updated the original question. The test is literal @test (JUnit) test case. – pash Aug 16 '23 at 18:42
  • Can you tell me which dependencies you are using and the source code for your test? E.g. Spring Boot, Spring Web, Cassandra Sync or Async. I'll try to build it locally and test. I'm assuming there might be a discrepancy between synchronous and asynchronous dependencies, but that's just a guess. – Nick Aug 16 '23 at 18:47
  • I have updated original post with test case code base.spring-boot-starter-actuator, spring-boot-starter-web, spring-boot-starter-webflux, spring-cloud-starter-config, spring-boot-starter-cache, spring-boot-starter-data-redis, spring-boot-starter-oauth2-resource-server, spring-boot-starter-oauth2-client, spring-boot-starter-data-cassandra, spring-boot-starter-validation, spring-boot-starter-security, spring-boot-actuator-autoconfigure, spring-boot-starter-aop, spring-boot-starter-test, cassandra:1.17.6 – pash Aug 16 '23 at 19:28
  • Sorry for the late reply. I tried recreating your project, but I don't know whats the structure of your domain objects - e.g. DepositRequest. Anyway I notice you are trying to make an asynchronous call using a synchronous Cassandra instance and maybe that's the issue. Can you try using `spring-boot-starter-data-cassandra-reactive` instead? Also I am not sure where you place the 1.17.6 version, but if you have an additional Cass dependency you should remove it. It may interfere with the one brought in from Spring Starter – Nick Aug 22 '23 at 09:10

0 Answers0