Akka-http: ScalatestRouteTest ignoring withRequestTimeout in Route

Created on 9 Mar 2017  路  6Comments  路  Source: akka/akka-http

Hi,

In this code for example, similar to the code in the akka-http documentation:

path("test") {
  withRequestTimeout(100 millis) {
    onSuccess(Future { Thread.sleep(300) }) {
      complete(OK)
    }
  }
}

If I then write a test like such:

Get("/test") ~> route ~> check {
  status shouldBe ServiceUnavailable
}

I get a log WARN saying "withRequestTimeout was used in route however no request-timeout is set!"
and the test fails with "200 OK was not equal to 503 Service Unavailable"

However if I test this manually, say with Postman, it works as expected.

1 - triaged

Most helpful comment

This is because a route test like this avoids starting the real http server, the ticket that would solve it is: https://github.com/akka/akka-http/issues/39

Basically, if you want to test timeouts or "the entire http stack" you should start the full http server in your test nowadays.

I'll keep the issue open but the solution is the linked Issue I believe.

All 6 comments

Needless to say, I didn't find documentation for this or comments of this happening to someone else anywhere on the internet.

This is because a route test like this avoids starting the real http server, the ticket that would solve it is: https://github.com/akka/akka-http/issues/39

Basically, if you want to test timeouts or "the entire http stack" you should start the full http server in your test nowadays.

I'll keep the issue open but the solution is the linked Issue I believe.

Will be solved in 10.2.0 with the new route integration testing merged in #3014. Use ~!> check {} in tests instead of ~> check.

The warning is logged even with ~!> ... (it can only be used before route and not check though :

    "add headers to a timed out response" in {
      Get("/api/valid").withHeaders(Origin("http://www.example.com")) ~!> route ~> check {
        status shouldBe StatusCodes.ServiceUnavailable
        headers should contain(`Access-Control-Allow-Origin`(HttpOrigin("http://www.example.com")))
      }
    }

I'm troubleshooting why cors does not set the response headers if timeout happens.

I'm troubleshooting why cors does not set the response headers if timeout happens.

The timeout is generated outside of the route logic. You can use the withRequestTimeoutResponse directive to change the generated response.

Yeah, I just did that, I figured it is not a coincidence that the handler is there :-) thanks

Was this page helpful?
0 / 5 - 0 ratings