2018-06-07 10:00:50 +02:00
|
|
|
"""
|
|
|
|
Helpers for testing.
|
|
|
|
"""
|
2017-12-30 16:30:35 +01:00
|
|
|
|
2024-01-01 11:14:04 +01:00
|
|
|
import time
|
2024-04-06 15:05:47 +02:00
|
|
|
from typing import Callable, TypeVar
|
|
|
|
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
2024-01-01 11:14:04 +01:00
|
|
|
|
2017-04-15 14:46:22 +02:00
|
|
|
|
2024-04-06 15:05:47 +02:00
|
|
|
def run_with_retries(fn: Callable[..., T]) -> T:
|
2024-01-01 11:14:04 +01:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
2024-04-06 15:05:47 +02:00
|
|
|
return fn()
|