How can I use mocket to test retry logic? #240
-
I'm currently trying to convert from httpretty to mocket. Most of the conversions have been straightforward so far, but I have one unusual use case using a callback function, which I think mocket doesn't support. from threading import Event
import httpretty
with httpretty.enabled():
with open("tests/api_responses/body.json") as _body:
body = _body.read()
with open("tests/api_responses/headers.json") as headers:
headers = json.load(headers)
event = Event()
first = True
def callback(request, url, headers):
nonlocal first
if first:
first = False
event.wait(1)
return 200, headers, "{}"
return 200, headers, body
httpretty.register_uri(
httpretty.GET, "https://api.github.com/users/lilyfoote", body=callback
) What I want here is the first response to be delayed (by a second) so the http library attempts a retry and successfully gets the second response immediately. If there a way to achieve this? |
Beta Was this translation helpful? Give feedback.
Answered by
mindflayer
Jun 6, 2024
Replies: 1 comment 13 replies
-
Hi @LilyFoote, |
Beta Was this translation helpful? Give feedback.
13 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If that's the case, Mocket gives you a way for raising errors:
https://github.com/mindflayer/python-mocket?tab=readme-ov-file#example-of-how-to-fake-socket-errors