Add a decorator for flaky tests (#19498)

* Add a decorator for flaky tests

* Quality

* Don't break the rest

* Address review comments

* Fix test name

* Fix typo and print to stderr
This commit is contained in:
Sylvain Gugger
2022-10-12 14:00:17 -04:00
committed by GitHub
parent 1973b7716b
commit 209bec4636
4 changed files with 50 additions and 3 deletions

View File

@@ -14,6 +14,7 @@
import collections
import contextlib
import functools
import inspect
import logging
import os
@@ -23,12 +24,13 @@ import shutil
import subprocess
import sys
import tempfile
import time
import unittest
from collections.abc import Mapping
from distutils.util import strtobool
from io import StringIO
from pathlib import Path
from typing import Iterator, List, Union
from typing import Iterator, List, Optional, Union
from unittest import mock
import huggingface_hub
@@ -1635,3 +1637,36 @@ class RequestCounter:
self.other_request_count += 1
return self.old_request(method=method, **kwargs)
def is_flaky(max_attempts: int = 5, wait_before_retry: Optional[float] = None):
"""
To decorate flaky tests. They will be retried on failures.
Args:
max_attempts (`int`, *optional*, defaults to 5):
The maximum number of attempts to retry the flaky test.
wait_before_retry (`float`, *optional*):
If provided, will wait that number of seconds before retrying the test.
"""
def decorator(test_func_ref):
@functools.wraps(test_func_ref)
def wrapper(*args, **kwargs):
retry_count = 1
while retry_count < max_attempts:
try:
return test_func_ref(*args, **kwargs)
except Exception as err:
print(f"Test failed with {err} at try {retry_count}/{max_attempts}.", file=sys.stderr)
if wait_before_retry is not None:
time.sleep(wait_before_retry)
retry_count += 1
return test_func_ref(*args, **kwargs)
return wrapper
return decorator