Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulate server down then coming online #445

Open
udalrich opened this issue Oct 29, 2021 · 1 comment
Open

Emulate server down then coming online #445

udalrich opened this issue Oct 29, 2021 · 1 comment

Comments

@udalrich
Copy link

Is there a way to emulate the server being down and then coming up in the middle of a test? The code that I am testing does something like

def get_data():
   result = requests.post(...)
   while is_error(result):
      sleep(5)
      result = requests.post(...)
  return result.json()

I would like to write a test that does something like

httpretty.register(httpretty.POST, 'http://example.com/request', body='{"foo": "bar"},
               ready_after_milliseconds=500)
result = get_data()

Ideally, I could check that httpretty rejected one request and then satisfied a second.

I can't register the url after the failed call, because control won't return to the test driver.

@udalrich
Copy link
Author

udalrich commented Nov 2, 2021

It appears that one way to emulate this is

raise MaxRetryError("Mock server not ready yet", url=uri)

inside the callback function to generate the body. You can also increment a variable there to know how many tries have been attempted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant