test: fix timeout decorator (#25489)
This commit is contained in:
parent
21cc09e28a
commit
2f87a09ad9
2 changed files with 11 additions and 2 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue