Skip to content

Commit 2e945fe

Browse files
committed
Replace sleep() with timeout in test case
1 parent d8cc913 commit 2e945fe

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

Lib/test/test_pyrepl/test_windows_console.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -589,32 +589,35 @@ def test_for_crash_traceback_with_redirected_stdout(self):
589589
with TemporaryDirectory() as tmp_dir:
590590
stdout_path = os.path.join(tmp_dir, "WinCMDLineTests.txt")
591591

592-
with open(stdout_path, "w", encoding="utf-8") as stdout_file, \
593-
subprocess.Popen(
592+
with open(stdout_path, "w") as stdout_file, \
593+
subprocess.Popen(
594594
[sys.executable, '-i', '-c', script_command],
595595
stdin=None,
596596
stdout=stdout_file,
597597
stderr=subprocess.PIPE,
598-
text=True, encoding='utf-8', errors='replace'
599-
) as process:
600-
601-
time.sleep(3)
602-
603-
if process.poll() is None:
598+
text=True, errors='replace'
599+
) as process:
600+
601+
stderr_output = ""
602+
603+
try:
604+
process.wait(timeout=3)
605+
self.fail(
606+
"Process exited unexpectedly within the timeout."
607+
)
608+
except subprocess.TimeoutExpired:
604609
process.kill()
610+
_, stderr_output = process.communicate()
605611

606-
stderr_output = process.stderr.read()
607-
608-
has_crash_traceback = (
609-
"OSError" in stderr_output
610-
and len(stderr_output) > 1200
611-
)
612+
has_crash_traceback = (
613+
"OSError" in stderr_output and
614+
len(stderr_output) > 1200
615+
)
616+
617+
if has_crash_traceback:
618+
self.fail("Detected endless OSError traceback."
619+
f"\n--- stderr ---\n{stderr_output[:1200]}")
612620

613-
if has_crash_traceback:
614-
self.fail(
615-
"Detected the endless OSError traceback.\n"
616-
f"Stderr was:\n{stderr_output[:1200]}"
617-
)
618621

619622
if __name__ == "__main__":
620623
unittest.main()

0 commit comments

Comments
 (0)