Closed theoutsider24 closed 1 year ago
I have a few questions on how this would work:
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
)
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
?
@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?
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.
@theoutsider24 please try the approach with matcher. We are here to support if you will need help. However, should be pretty straightforward
@theoutsider24 were you able to achieve it with matcher?
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