Skip to content

Commit 2298ee8

Browse files
Make sigterm tests less flaky
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent b8e885f commit 2298ee8

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

tests/test_coverage.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,8 @@ def test_sigterm_top_level_writes_single_correct_report(tmp_path, monkeypatch):
18201820
script.write_text(dedent("""\
18211821
import time
18221822
x = 1
1823+
with open("started.txt", "w") as f:
1824+
f.write("1")
18231825
time.sleep(10)
18241826
y = 2 # must never execute -- process is killed during sleep
18251827
"""))
@@ -1829,9 +1831,21 @@ def test_sigterm_top_level_writes_single_correct_report(tmp_path, monkeypatch):
18291831
cwd=tmp_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
18301832
)
18311833

1832-
time.sleep(1) # let slipcover start up and reach the sleep
1834+
# Wait for the script to actually reach the sleep, rather than guessing
1835+
# a fixed duration: too short races the SIGTERM against slipcover's own
1836+
# startup, too long just wastes time -- polling for a concrete
1837+
# readiness marker adapts to whatever the environment actually needs.
1838+
started_file = tmp_path / "started.txt"
1839+
for _ in range(100): # up to ~5s
1840+
if started_file.exists():
1841+
break
1842+
time.sleep(0.05)
1843+
else:
1844+
proc.kill()
1845+
pytest.fail("script never started")
1846+
18331847
proc.send_signal(signal.SIGTERM)
1834-
stdout, stderr = proc.communicate(timeout=10)
1848+
stdout, stderr = proc.communicate(timeout=30)
18351849

18361850
assert proc.returncode == 0, f"stdout={stdout}\nstderr={stderr}"
18371851
# the report table's header appears exactly once per report -- the
@@ -1886,7 +1900,7 @@ def test_sigterm_forked_child_writes_partial_coverage_safely(tmp_path, monkeypat
18861900
child_pid = int(pid_file.read_text())
18871901
os.kill(child_pid, signal.SIGTERM)
18881902

1889-
stdout, stderr = proc.communicate(timeout=10)
1903+
stdout, stderr = proc.communicate(timeout=30)
18901904
assert proc.returncode == 0, f"stdout={stdout}\nstderr={stderr}"
18911905

18921906
cov = json.loads((tmp_path / "out.json").read_text())

0 commit comments

Comments
 (0)