2018-06-07 04:00:50 -04:00
|
|
|
"""
|
|
|
|
Helpers for testing.
|
|
|
|
"""
|
2017-12-30 10:30:35 -05:00
|
|
|
|
2024-01-01 05:14:04 -05:00
|
|
|
import time
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
class MockResponse:
|
2017-04-18 10:16:24 -04:00
|
|
|
def __init__(self, response_data={}, ok=True, is_redirect=False):
|
2017-04-15 08:46:22 -04:00
|
|
|
self.response_data = response_data
|
2018-01-02 04:44:32 -05:00
|
|
|
self.content = response_data
|
2017-04-18 10:16:24 -04:00
|
|
|
self.ok = ok
|
|
|
|
self.is_redirect = is_redirect
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
def raise_for_status(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
return self.response_data
|
2017-12-30 06:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
def retval(val):
|
|
|
|
return lambda *args, **kwargs: val
|
2024-01-01 05:14:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
def run_with_retries(fn: Callable[..., Any]):
|
|
|
|
"""
|
|
|
|
Run the the given function repeatedly until it finishes without raising an
|
|
|
|
AssertionError. Sleep a bit between attempts. If the function doesn't
|
|
|
|
succeed in the given number of tries raises the AssertionError. Used for
|
|
|
|
tests which should eventually succeed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Wait upto 6 seconds with incrementally longer sleeps
|
|
|
|
delays = [0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
|
|
|
|
|
|
|
|
for delay in delays:
|
|
|
|
try:
|
|
|
|
return fn()
|
|
|
|
except AssertionError:
|
|
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
fn()
|