From: Frantisek Sumsal Date: Fri, 2 Sep 2022 18:06:12 +0000 (+0200) Subject: test: check for the output file in a loop X-Git-Tag: v252-rc1~267^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4aa84ef9a3c4780647e7deefa1b288585151346f;p=thirdparty%2Fsystemd.git test: check for the output file in a loop This should make the test faster on fast machines and more reliable on slower/under-load machines, where the 4 sec sleep wasn't sometimes enough. Spotted on C8S machines under load: ``` test_added_after (__main__.ExecutionResumeTest) ... FAIL test_added_before (__main__.ExecutionResumeTest) ... ok test_interleaved (__main__.ExecutionResumeTest) ... ok test_issue_6533 (__main__.ExecutionResumeTest) ... ok test_no_change (__main__.ExecutionResumeTest) ... ok test_removal (__main__.ExecutionResumeTest) ... ok test_swapped (__main__.ExecutionResumeTest) ... ok ====================================================================== FAIL: test_added_after (__main__.ExecutionResumeTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "./test/test-exec-deserialization.py", line 101, in check_output with open(self.output_file, 'r') as log: FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpjnec1dj4' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "./test/test-exec-deserialization.py", line 150, in test_added_after self.check_output(expected_output) File "./test/test-exec-deserialization.py", line 104, in check_output self.fail() AssertionError: None ---------------------------------------------------------------------- Ran 7 tests in 44.270s ``` --- diff --git a/test/test-exec-deserialization.py b/test/test-exec-deserialization.py index 9491e4081e4..ce118c8add6 100755 --- a/test/test-exec-deserialization.py +++ b/test/test-exec-deserialization.py @@ -97,13 +97,18 @@ class ExecutionResumeTest(unittest.TestCase): self.reload() def check_output(self, expected_output): - try: - with open(self.output_file, 'r') as log: - output = log.read() - except IOError: - self.fail() + for _ in range(15): + try: + with open(self.output_file, 'r') as log: + output = log.read() + self.assertEqual(output, expected_output) + return + except IOError: + pass + + time.sleep(1) - self.assertEqual(output, expected_output) + self.fail("Time out while waiting for the output file {} to appear".format(self.output_file)) def setup_unit(self): self.write_unit_file(UnitFileChange.NO_CHANGE) @@ -115,7 +120,6 @@ class ExecutionResumeTest(unittest.TestCase): self.setup_unit() self.reload() - time.sleep(4) self.check_output(expected_output) @@ -125,7 +129,6 @@ class ExecutionResumeTest(unittest.TestCase): self.setup_unit() self.write_unit_file(UnitFileChange.LINES_SWAPPED) self.reload() - time.sleep(4) self.assertTrue(not os.path.exists(self.output_file)) @@ -135,7 +138,6 @@ class ExecutionResumeTest(unittest.TestCase): self.setup_unit() self.write_unit_file(UnitFileChange.COMMAND_ADDED_BEFORE) self.reload() - time.sleep(4) self.check_output(expected_output) @@ -145,7 +147,6 @@ class ExecutionResumeTest(unittest.TestCase): self.setup_unit() self.write_unit_file(UnitFileChange.COMMAND_ADDED_AFTER) self.reload() - time.sleep(4) self.check_output(expected_output) @@ -155,7 +156,6 @@ class ExecutionResumeTest(unittest.TestCase): self.setup_unit() self.write_unit_file(UnitFileChange.COMMAND_INTERLEAVED) self.reload() - time.sleep(4) self.check_output(expected_output) @@ -163,7 +163,6 @@ class ExecutionResumeTest(unittest.TestCase): self.setup_unit() self.write_unit_file(UnitFileChange.REMOVAL) self.reload() - time.sleep(4) self.assertTrue(not os.path.exists(self.output_file)) @@ -196,7 +195,7 @@ class ExecutionResumeTest(unittest.TestCase): self.reload() time.sleep(5) - self.assertTrue(subprocess.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell=True) != 0) + self.assertTrue(subprocess.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell=True) != 0) def tearDown(self): for f in [self.output_file, self.unitfile_path]: