Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish suppress decorator in tests #684

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def credentials_from_test_param(request: pytest.FixtureRequest) -> Credentials:
T = TypeVar("T", bound=Callable[..., Any])


def suppress(exception: type[BaseException], *, retries: Optional[int] = None, timeout: int = 60) -> Callable[[T], T]:
def suppress(exception: type[BaseException], *, retries: int = 0, timeout: int = 60) -> Callable[[T], T]:
"""Suppresses the specified exception and retries the function a specified number of times.

Args:
Expand All @@ -428,58 +428,44 @@ def suppress(exception: type[BaseException], *, retries: Optional[int] = None, t
"""

def decorator(
func: T, exception: type[BaseException] = exception, retries: Optional[int] = retries, timeout: int = timeout
func: T, exception: type[BaseException] = exception, retries: int = retries, timeout: int = timeout
) -> T:
if inspect.iscoroutinefunction(func):

@functools.wraps(func)
async def wrapper(
*args: Any,
exception: type[BaseException] = exception,
retries: Optional[int] = retries,
retries: int = retries,
timeout: int = timeout,
**kwargs: Any,
) -> Any:
if retries is None:
for i in range(retries + 1):
try:
return await func(*args, **kwargs)
except exception:
pytest.xfail(f"Suppressed '{exception}' raised")
raise
else:
for i in range(retries):
try:
return await func(*args, **kwargs)
except exception:
if i >= retries - 1:
pytest.xfail(f"Suppressed '{exception}' raised {i + 1} times")
raise
await asyncio.sleep(timeout)
if i >= retries - 1:
pytest.xfail(f"Suppressed '{exception}' raised {i + 1} times")
raise
await asyncio.sleep(timeout)
else:

@functools.wraps(func)
def wrapper(
*args: Any,
exception: type[BaseException] = exception,
retries: Optional[int] = retries,
retries: int = retries,
timeout: int = timeout,
**kwargs: Any,
) -> Any:
if retries is None:
for i in range(retries + 1):
try:
return func(*args, **kwargs)
except exception:
pytest.xfail(f"Suppressed '{exception}' raised")
raise
else:
for i in range(retries):
try:
return func(*args, **kwargs)
except exception:
if i >= retries - 1:
pytest.xfail(f"Suppressed '{exception}' raised {i + 1} times")
raise
time.sleep(timeout)
if i >= retries - 1:
pytest.xfail(f"Suppressed '{exception}' raised {i + 1} times")
raise
time.sleep(timeout)

return wrapper # type: ignore[return-value]

Expand Down
Loading