okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Add default timeout to MockWebServer.takeRequest()

Open jhyot opened this issue 3 years ago • 2 comments

MockWebServer.takeRequest() without parameters will block indefinitely if there is no request coming. In my opinion this is a bit unfortunate, because in case of e.g. a bug in our code which results in no calls being made, the complete test suite stalls. Another use case is when writing test-first code, then there is often no request yet programmed, yet I cannot run the test to assert that it is failing.

The solution is to use the overloaded method with an explicit timeout. But this makes the code less readable, and there are potentially many repetitions of the MockWebServer.takeRequest(xxx, SECONDS) (and similar) calls, which are basically noise.

This is why I want to suggest a feature to be able to set a default timeout, either per MockWebServer instance, or globally. Many test libraries like Awaitility or Mockito use static global variables for some baseline settings.

jhyot avatar Aug 10 '22 10:08 jhyot

There is a good opportunity with OkHttp 5 to look at this now.

yschimke avatar Aug 10 '22 11:08 yschimke