|
| 1 | +import errno |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import threading |
| 7 | +import time |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from coding_study.adapters import busy_binary |
| 13 | +from coding_study.adapters.busy_binary import MAX_ATTEMPTS, start_when_not_busy |
| 14 | +from coding_study.adapters.languages.bounded_process import BoundedProcess |
| 15 | +from coding_study.adapters.languages.rust import project_suite |
| 16 | +from coding_study.domain.errors import RunnerStartError |
| 17 | + |
| 18 | + |
| 19 | +def busy() -> OSError: |
| 20 | + return OSError(errno.ETXTBSY, "Text file busy") |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def sleeps(monkeypatch) -> list[float]: |
| 25 | + waited: list[float] = [] |
| 26 | + monkeypatch.setattr(busy_binary.time, "sleep", waited.append) |
| 27 | + return waited |
| 28 | + |
| 29 | + |
| 30 | +def failing(errors: list[OSError], result: str = "started"): |
| 31 | + attempts: list[int] = [] |
| 32 | + |
| 33 | + def start() -> str: |
| 34 | + attempts.append(1) |
| 35 | + if errors: |
| 36 | + raise errors.pop(0) |
| 37 | + return result |
| 38 | + |
| 39 | + return start, attempts |
| 40 | + |
| 41 | + |
| 42 | +def test_a_busy_binary_is_started_once_it_is_free(sleeps): |
| 43 | + start, attempts = failing([busy(), busy()]) |
| 44 | + assert start_when_not_busy(start) == "started" |
| 45 | + assert (len(attempts), sleeps) == (3, [0.01, 0.02]) |
| 46 | + |
| 47 | + |
| 48 | +def test_a_binary_that_stays_busy_fails_after_the_last_attempt(sleeps): |
| 49 | + start, attempts = failing([busy()] * 20) |
| 50 | + with pytest.raises(OSError, match="Text file busy") as raised: |
| 51 | + start_when_not_busy(start) |
| 52 | + assert raised.value.errno == errno.ETXTBSY |
| 53 | + assert len(attempts) == MAX_ATTEMPTS |
| 54 | + assert len(sleeps) == MAX_ATTEMPTS - 1 |
| 55 | + assert max(sleeps) == busy_binary.MAX_DELAY_SECONDS |
| 56 | + assert 1.5 < sum(sleeps) < 2.5 |
| 57 | + |
| 58 | + |
| 59 | +def test_any_other_error_is_raised_at_once(sleeps): |
| 60 | + start, attempts = failing([OSError(errno.ENOENT, "No such file or directory")]) |
| 61 | + with pytest.raises(FileNotFoundError): |
| 62 | + start_when_not_busy(start) |
| 63 | + assert (len(attempts), sleeps) == (1, []) |
| 64 | + |
| 65 | + |
| 66 | +def test_the_runner_start_retries_a_busy_binary(monkeypatch, sleeps): |
| 67 | + started = [] |
| 68 | + real = subprocess.Popen |
| 69 | + |
| 70 | + def popen(command, **options): |
| 71 | + started.append(command) |
| 72 | + if len(started) < 3: |
| 73 | + raise busy() |
| 74 | + return real(command, **options) |
| 75 | + |
| 76 | + monkeypatch.setattr(subprocess, "Popen", popen) |
| 77 | + completed = BoundedProcess("build", 10).run([sys.executable, "--version"], Path.cwd(), None) |
| 78 | + assert (completed.returncode, len(started)) == (0, 3) |
| 79 | + |
| 80 | + |
| 81 | +def test_a_rust_test_binary_that_stays_busy_is_a_start_error(monkeypatch, sleeps): |
| 82 | + calls = [] |
| 83 | + |
| 84 | + def popen(command, **options): |
| 85 | + calls.append(command) |
| 86 | + raise busy() |
| 87 | + |
| 88 | + monkeypatch.setattr(subprocess, "Popen", popen) |
| 89 | + with pytest.raises(RunnerStartError, match="Text file busy"): |
| 90 | + project_suite._start( |
| 91 | + ["./tests"], Path.cwd(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL |
| 92 | + ) |
| 93 | + assert len(calls) == MAX_ATTEMPTS |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="ETXTBSY is how Linux refuses") |
| 97 | +def test_a_binary_held_open_by_a_forked_child_starts_once_the_child_has_executed(tmp_path): |
| 98 | + binary = tmp_path / "python-copy" |
| 99 | + shutil.copyfile(sys.executable, binary) |
| 100 | + binary.chmod(0o755) |
| 101 | + forked, announce = os.pipe() |
| 102 | + |
| 103 | + def hold_until_exec() -> None: |
| 104 | + os.write(announce, b"x") |
| 105 | + time.sleep(0.5) |
| 106 | + |
| 107 | + holder = threading.Thread( |
| 108 | + target=subprocess.run, |
| 109 | + args=([sys.executable, "--version"],), |
| 110 | + kwargs={"preexec_fn": hold_until_exec, "pass_fds": (announce,), "capture_output": True}, |
| 111 | + ) |
| 112 | + with binary.open("r+b"): |
| 113 | + holder.start() |
| 114 | + assert os.read(forked, 1) == b"x" |
| 115 | + os.close(forked) |
| 116 | + os.close(announce) |
| 117 | + # the forked child still holds the copy's write descriptor: executing it fails until it execs |
| 118 | + with pytest.raises(OSError, match="Text file busy") as direct: |
| 119 | + subprocess.run([str(binary), "--version"], capture_output=True) |
| 120 | + assert direct.value.errno == errno.ETXTBSY |
| 121 | + process = start_when_not_busy( |
| 122 | + lambda: subprocess.Popen([str(binary), "--version"], stdout=subprocess.PIPE) |
| 123 | + ) |
| 124 | + output, _ = process.communicate() |
| 125 | + assert (process.returncode, output.startswith(b"Python")) == (0, True) |
| 126 | + holder.join() |
0 commit comments