Skip to content

Commit

Permalink
Rerun time based retry tests to avoid flaky failures (#12869)
Browse files Browse the repository at this point in the history
Also increase the time tolerance to account for more extreme variation.
  • Loading branch information
ichard26 authored Dec 16, 2024
1 parent 90add48 commit 3b91f42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Empty file.
12 changes: 7 additions & 5 deletions tests/unit/test_utils_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,24 @@ def _raise_error() -> NoReturn:
@pytest.mark.skipif(
sys.platform == "win32", reason="Too flaky on Windows due to poor timer resolution"
)
@pytest.mark.flaky(reruns=3, reruns_delay=1)
@pytest.mark.parametrize("wait_duration", [0.015, 0.045, 0.15])
def test_retry_wait(wait_duration: float) -> None:
function, timestamps = create_timestamped_callable()
# Only the first retry will be scheduled before the time limit is exceeded.
wrapped = retry(wait=wait_duration, stop_after_delay=0.01)(function)
wrapped = retry(wait=wait_duration, stop_after_delay=0.1)(function)
start_time = perf_counter()
with pytest.raises(RuntimeError):
wrapped()
assert len(timestamps) == 2
# Add a margin of 10% to permit for unavoidable variation.
assert len(timestamps) >= 2
# Just check the first retry, with a margin of 10% to permit for
# unavoidable variation.
assert timestamps[1] - start_time >= (wait_duration * 0.9)


@pytest.mark.skipif(
sys.platform == "win32", reason="Too flaky on Windows due to poor timer resolution"
)
@pytest.mark.flaky(reruns=3, reruns_delay=1)
@pytest.mark.parametrize(
"call_duration, max_allowed_calls", [(0.01, 11), (0.04, 3), (0.15, 1)]
)
Expand All @@ -109,7 +111,7 @@ class MyClass:
def __init__(self) -> None:
self.calls = 0

@retry(wait=0, stop_after_delay=0.01)
@retry(wait=0, stop_after_delay=3)
def method(self, string: str) -> str:
self.calls += 1
if self.calls >= 5:
Expand Down

0 comments on commit 3b91f42

Please sign in to comment.