getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.14k stars 354 forks source link

[Feature request] Add response delay option #666

Closed theoutsider24 closed 1 year ago

theoutsider24 commented 1 year ago

I'd like the option to introduce an artificial delay before returning a response to simulate services which are slow

This is particularly useful in testing asynchronous operations to control responses coming out of order

markstory commented 1 year ago

I have a few questions on how this would work:

  1. Wouldn't adding delays slow down tests as you'll have to block on timers completing?
  2. How would you indicate to responses that some mocks should have delays applied?
theoutsider24 commented 1 year ago

Hey @markstory

Yes it would slow the tests, the use case here is related to asynchronous requests (particularly using requests-futures) where I want to demonstrate a) that if the responses come out of order it's fine and b) that if two async responses both have a delay, the runtime is less than the sum of the delays

a is the kind of thing I may have in my unit test suite, with very small delays (eg. 20-50ms) - just enough to make responses come in out of order b would more likely be for manual testing and PoCs Another use case could be forcing one request to hang while another succeeds/fails with a short delay to demonstrate that the hanging request gets cancelled

Maybe something like this

 responses.add(
        responses.GET,
        "http://twitter.com/api/1/foobar",
        json={"error": "not found"},
        status=404,
        latency=1000 # add 1s of latency
    )
markstory commented 1 year ago

A latency parameter could work well. If the host application isn't using asyncio, I guess we would have to block the main thread for the duration of latency?

beliaev-maksim commented 1 year ago

@markstory if we consider that the delay is artificially added, then it should not play role where it is added in responses

Will in such case matcher work as well?

markstory commented 1 year ago

Yeah, a matcher could be used to add the latency as well. That would save us having to expand the API of responses.add which is nice.

beliaev-maksim commented 1 year ago

@theoutsider24 please try the approach with matcher. We are here to support if you will need help. However, should be pretty straightforward

beliaev-maksim commented 1 year ago

@theoutsider24 were you able to achieve it with matcher?