Skip to content

[3.14] gh-137179: Fix flaky test_history_survive_crash test (gh-137180) #137216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.14
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def run_repl(
cwd: str | None = None,
skip: bool = False,
timeout: float = SHORT_TIMEOUT,
exit_on_output: str | None = None,
) -> tuple[str, int]:
temp_dir = None
if cwd is None:
Expand All @@ -64,6 +65,7 @@ def run_repl(
cwd=cwd,
skip=skip,
timeout=timeout,
exit_on_output=exit_on_output,
)
finally:
if temp_dir is not None:
Expand All @@ -78,6 +80,7 @@ def _run_repl(
cwd: str,
skip: bool,
timeout: float,
exit_on_output: str | None,
) -> tuple[str, int]:
assert pty
master_fd, slave_fd = pty.openpty()
Expand Down Expand Up @@ -123,6 +126,11 @@ def _run_repl(
except OSError:
break
output.append(data)
if exit_on_output is not None:
output = ["".join(output)]
if exit_on_output in output[0]:
process.kill()
break
else:
os.close(master_fd)
process.kill()
Expand Down Expand Up @@ -1718,18 +1726,24 @@ def test_history_survive_crash(self):

commands = "1\n2\n3\nexit()\n"
output, exit_code = self.run_repl(commands, env=env, skip=True)
self.assertEqual(exit_code, 0)

commands = "spam\nimport time\ntime.sleep(1000)\nquit\n"
try:
self.run_repl(commands, env=env, timeout=3)
except AssertionError:
pass
# Run until "0xcafe" is printed (as "51966") and then kill the
# process to simulate a crash. Note that the output also includes
# the echoed input commands.
commands = "spam\nimport time\n0xcafe\ntime.sleep(1000)\nquit\n"
output, exit_code = self.run_repl(commands, env=env,
exit_on_output="51966")
self.assertNotEqual(exit_code, 0)

history = pathlib.Path(hfile.name).read_text()
self.assertIn("2", history)
self.assertIn("exit()", history)
self.assertIn("spam", history)
self.assertIn("import time", history)
# History is written after each command's output is printed to the
# console, so depending on how quickly the process is killed,
# the last command may or may not be written to the history file.
self.assertNotIn("sleep", history)
self.assertNotIn("quit", history)

Expand Down
Loading