HTTPretty icon indicating copy to clipboard operation
HTTPretty copied to clipboard

Emulate server down then coming online

Open udalrich opened this issue 2 years ago • 1 comments

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 avatar Oct 29 '21 22:10 udalrich

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.

udalrich avatar Nov 02 '21 14:11 udalrich