From 2e8a9e8465e70bd79d9cc6ca008a5a9e408cac6a Mon Sep 17 00:00:00 2001 From: Jan Richter Date: Thu, 29 Aug 2024 17:31:55 +0200 Subject: [PATCH] Avocado instrumented timeout in setUp and tearDown fix 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: #6013 Signed-off-by: Jan Richter --- avocado/core/test.py | 4 ++-- examples/tests/timeouttest.py | 21 +++++++++++++++++++- selftests/check.py | 2 +- selftests/functional/basic.py | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/avocado/core/test.py b/avocado/core/test.py index eb79e2960e..763cf705bb 100644 --- a/avocado/core/test.py +++ b/avocado/core/test.py @@ -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() @@ -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 diff --git a/examples/tests/timeouttest.py b/examples/tests/timeouttest.py index 2a5402c552..3a8f2dd064 100644 --- a/examples/tests/timeouttest.py +++ b/examples/tests/timeouttest.py @@ -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) diff --git a/selftests/check.py b/selftests/check.py index ea81932dfa..5fc19d20e0 100755 --- a/selftests/check.py +++ b/selftests/check.py @@ -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, diff --git a/selftests/functional/basic.py b/selftests/functional/basic.py index cf47d72f10..3dd9cdd710 100644 --- a/selftests/functional/basic.py +++ b/selftests/functional/basic.py @@ -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 "