test: fix timeout decorator (#25489)

This commit is contained in:
Ankush Menat 2024-03-16 15:59:04 +05:30 committed by GitHub
parent 21cc09e28a
commit 2f87a09ad9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View file

@ -58,6 +58,7 @@ class TestRQJob(FrappeTestCase):
rq_job = frappe.get_doc("RQ Job", job.id)
self.assertEqual(rq_job.job_name, "test_func")
@timeout
def test_get_list_filtering(self):
# Check failed job clearning and filtering
remove_failed_jobs()

View file

@ -307,13 +307,18 @@ def timeout(seconds=30, error_message="Test timed out."):
adapted from https://stackoverflow.com/a/2282656"""
# Support @timeout (without function call)
no_args = bool(callable(seconds))
actual_timeout = 30 if no_args else seconds
actual_error_message = "Test timed out" if no_args else error_message
def decorator(func):
def _handle_timeout(signum, frame):
raise Exception(error_message)
raise Exception(actual_error_message)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds)
signal.alarm(actual_timeout)
try:
result = func(*args, **kwargs)
finally:
@ -322,6 +327,9 @@ def timeout(seconds=30, error_message="Test timed out."):
return wrapper
if no_args:
return decorator(seconds)
return decorator