Skip to content

Commit

Permalink
Avocado instrumented timeout in setUp and tearDown fix
Browse files Browse the repository at this point in the history
This commit is a fix for avocado-instrumented timeouts. When the tests
are interrupted due to timeout during `setUp` or `tearDown` method,
those tests would result in `ERROR` instead of `INTERRUPTED`. This
commit updates the exception handling of this method to fix this issue.

Reference: avocado-framework#6013
Signed-off-by: Jan Richter <[email protected]>
  • Loading branch information
richtja committed Sep 16, 2024
1 parent 20053f6 commit f8bd119
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
4 changes: 2 additions & 2 deletions avocado/core/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def _run_test(self):
self.__skip_test = True
stacktrace.log_exc_info(sys.exc_info(), logger=self.log)
raise exceptions.TestSkipError(details)
except exceptions.TestCancel:
except (exceptions.TestCancel, exceptions.TestInterrupt):
stacktrace.log_exc_info(sys.exc_info(), logger=self.log)
raise
except: # Old-style exceptions are not inherited from Exception()
Expand Down Expand Up @@ -633,7 +633,7 @@ def _tearDown(self):
f"test. Original skip exception: {details}"
)
raise exceptions.TestError(skip_illegal_msg)
except exceptions.TestCancel:
except (exceptions.TestCancel, exceptions.TestInterrupt):
stacktrace.log_exc_info(sys.exc_info(), logger=self.log)
raise
except: # avoid old-style exception failures pylint: disable=W0702
Expand Down
2 changes: 1 addition & 1 deletion selftests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"nrunner-requirement": 28,
"unit": 678,
"jobs": 11,
"functional-parallel": 307,
"functional-parallel": 309,
"functional-serial": 7,
"optional-plugins": 0,
"optional-plugins-golang": 2,
Expand Down
64 changes: 64 additions & 0 deletions selftests/functional/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ def test(self):
fi
"""

TIMEOUT_TEST = """import time
from avocado import Test
class TimeoutTest(Test):
timeout = 3
def setUp(self):
sleep_time = float(self.params.get("sleep_time_setup", default=0.0))
time.sleep(sleep_time)
def test(self):
sleep_time = float(self.params.get("sleep_time", default=0.0))
time.sleep(sleep_time)
def tearDown(self):
sleep_time = float(self.params.get("sleep_time_teardown", default=0.0))
time.sleep(sleep_time)
"""


def probe_binary(binary):
try:
Expand Down Expand Up @@ -545,6 +567,48 @@ def test_runner_timeout_factor(self):
)
self.assertNotIn("timeout", result_json["tests"][0]["fail_reason"])

def test_runner_timeout_setup(self):
with script.TemporaryScript(
"timeout.py",
TIMEOUT_TEST,
) as tst:
cmd_line = (
f"{AVOCADO} run --disable-sysinfo --job-results-dir "
f"{self.tmpdir.name} -p sleep_time_setup=5 -- {tst}"
)
result = process.run(cmd_line, ignore_status=True)
json_path = os.path.join(self.tmpdir.name, "latest", "results.json")
with open(json_path, encoding="utf-8") as json_file:
result_json = json.load(json_file)
expected_rc = exit_codes.AVOCADO_JOB_INTERRUPTED
self.assertEqual(
result.exit_status,
expected_rc,
f"Avocado did not return rc {expected_rc}:\n{result}",
)
self.assertNotIn("timeout", result_json["tests"][0]["fail_reason"])

def test_runner_timeout_teardown(self):
with script.TemporaryScript(
"timeout.py",
TIMEOUT_TEST,
) as tst:
cmd_line = (
f"{AVOCADO} run --disable-sysinfo --job-results-dir "
f"{self.tmpdir.name} -p sleep_time_teardown=5 -- {tst}"
)
result = process.run(cmd_line, ignore_status=True)
json_path = os.path.join(self.tmpdir.name, "latest", "results.json")
with open(json_path, encoding="utf-8") as json_file:
result_json = json.load(json_file)
expected_rc = exit_codes.AVOCADO_JOB_INTERRUPTED
self.assertEqual(
result.exit_status,
expected_rc,
f"Avocado did not return rc {expected_rc}:\n{result}",
)
self.assertNotIn("timeout", result_json["tests"][0]["fail_reason"])

def test_silent_output(self):
cmd_line = (
f"{AVOCADO} --show=none run --disable-sysinfo "
Expand Down

0 comments on commit f8bd119

Please sign in to comment.