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 Aug 29, 2024
1 parent 20053f6 commit 2e8a9e8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 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
21 changes: 20 additions & 1 deletion examples/tests/timeouttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@ class TimeoutTest(Test):

timeout = 3

def setUp(self):
sleep_time = float(self.params.get("sleep_time_setup", default=0.0))
self.log.info(
"Sleeping for %.2f seconds during setUp (2 more than the timeout)",
sleep_time,
)
time.sleep(sleep_time)

def test(self):
"""
This should throw a TestTimeoutError.
"""
sleep_time = float(self.params.get("sleep_time", default=5.0))
self.log.info("Sleeping for %.2f seconds (2 more than the timeout)", sleep_time)
self.log.info(
"Sleeping for %.2f seconds during Test (2 more than the timeout)",
sleep_time,
)
time.sleep(sleep_time)

def tearDown(self):
sleep_time = float(self.params.get("sleep_time_teardown", default=0.0))
self.log.info(
"Sleeping for %.2f seconds during tearDown (2 more than the timeout)",
sleep_time,
)
time.sleep(sleep_time)
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
36 changes: 36 additions & 0 deletions selftests/functional/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,42 @@ def test_runner_timeout_factor(self):
)
self.assertNotIn("timeout", result_json["tests"][0]["fail_reason"])

def test_runner_timeout_setup(self):
cmd_line = (
f"{AVOCADO} run --disable-sysinfo --job-results-dir "
f"{self.tmpdir.name} -p sleep_time= -p sleep_time_setup=5"
" -- examples/tests/timeouttest.py"
)
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):
cmd_line = (
f"{AVOCADO} run --disable-sysinfo --job-results-dir "
f"{self.tmpdir.name} -p sleep_time= -p sleep_time_teardown=5"
" -- examples/tests/timeouttest.py"
)
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 2e8a9e8

Please sign in to comment.