|
| 1 | +#! /usr/bin/python3 |
| 2 | + |
| 3 | +##--------------------------------------------------------------------\\ |
| 4 | +# hackrfpy 'tests/test_interrupt_clean.py' |
| 5 | +# The CLEAN-interrupt contract, distinguished from "the child died". |
| 6 | +# stop()'s whole point is that hackrf_transfer flushes + closes on the |
| 7 | +# interrupt signal so a capture file is never truncated; the older |
| 8 | +# lifecycle tests proved the child EXITS, but their stub treated the |
| 9 | +# interrupt and the terminate() escalation identically, so a broken |
| 10 | +# CTRL_BREAK path could hide behind a working escalation. |
| 11 | +# |
| 12 | +# These tests assert, on BOTH platforms (no Windows skips -- Windows is |
| 13 | +# the target platform, and on Windows the signal must also traverse the |
| 14 | +# .bat -> python launcher layer, exactly like the real .bat-wrapped |
| 15 | +# tools): |
| 16 | +# 1. the interrupt SIGNAL ITSELF arrives (SIGINT on POSIX, SIGBREAK |
| 17 | +# from CTRL_BREAK_EVENT on Windows) -- the stub records the signal |
| 18 | +# NAME it caught, and the escalation would record a different one |
| 19 | +# (or none: TerminateProcess runs no handler) |
| 20 | +# 2. output written by the child's handler AFTER the interrupt is |
| 21 | +# drained into stop()'s result -- the no-truncation contract |
| 22 | +# 3. a child that ignores the interrupt is still reaped via the |
| 23 | +# grace-timeout terminate() escalation, reported as unclean |
| 24 | +# |
| 25 | +# |
| 26 | +# Author(s): Lauren Linkous |
| 27 | +##--------------------------------------------------------------------\\ |
| 28 | + |
| 29 | +import os |
| 30 | +import sys |
| 31 | +import time |
| 32 | + |
| 33 | + |
| 34 | +_EXPECTED_INTERRUPT = "SIGBREAK" if sys.platform == "win32" else "SIGINT" |
| 35 | + |
| 36 | + |
| 37 | +def _wait_for_output(proc, needle, timeout=5.0): |
| 38 | + deadline = time.monotonic() + timeout |
| 39 | + while time.monotonic() < deadline: |
| 40 | + if needle in b"".join(proc._out_chunks): |
| 41 | + return True |
| 42 | + time.sleep(0.05) |
| 43 | + return False |
| 44 | + |
| 45 | + |
| 46 | +# ---- 1. the clean signal itself is delivered ------------------------------- |
| 47 | +def test_stop_delivers_interrupt_signal_not_escalation(stub_device, tmp_path): |
| 48 | + marker = str(tmp_path / "sig") |
| 49 | + h = stub_device(transfer=dict( |
| 50 | + stdout_lines=["started"], idle=True, marker=marker)) |
| 51 | + proc = h._run(["transfer", "-r", "x.iq"], mode="handle") |
| 52 | + assert _wait_for_output(proc, b"started") |
| 53 | + out, err, rc = proc.stop() |
| 54 | + assert os.path.exists(marker), "no signal reached the child at all" |
| 55 | + with open(marker) as f: |
| 56 | + caught = f.read().strip() |
| 57 | + # SIGTERM here would mean the child only died via the terminate() |
| 58 | + # escalation; empty would mean a pre-signal-name stub. Both are failures |
| 59 | + # of the CLEAN path this test exists to pin. |
| 60 | + assert caught == _EXPECTED_INTERRUPT, ( |
| 61 | + f"child caught {caught!r}, expected {_EXPECTED_INTERRUPT!r} -- the " |
| 62 | + f"clean interrupt path is not the one that ended the child") |
| 63 | + assert rc == 0 # handler exit, not a kill status |
| 64 | + |
| 65 | + |
| 66 | +# ---- 2. the post-interrupt flush survives into the result ------------------ |
| 67 | +def test_interrupt_flush_is_drained_not_truncated(stub_device): |
| 68 | + sentinel = "FINAL_FLUSH_8f3a" |
| 69 | + h = stub_device(transfer=dict( |
| 70 | + stdout_lines=["started"], idle=True, tail_on_interrupt=sentinel)) |
| 71 | + proc = h._run(["transfer", "-r", "x.iq"], mode="handle") |
| 72 | + assert _wait_for_output(proc, b"started") |
| 73 | + out, err, rc = proc.stop() |
| 74 | + # the sentinel is written by the child's signal handler AFTER stop() |
| 75 | + # fires the interrupt; finding it in the drained result proves the |
| 76 | + # interrupt -> handler -> flush -> drain chain end to end |
| 77 | + assert sentinel.encode() in out, ( |
| 78 | + "output written during interrupt handling was lost -- this is the " |
| 79 | + "truncated-capture failure mode stop() exists to prevent") |
| 80 | + assert rc == 0 |
| 81 | + |
| 82 | + |
| 83 | +# ---- 3. a deaf child is still reaped, and reported as unclean -------------- |
| 84 | +def test_stop_escalates_on_ignored_interrupt(stub_device, tmp_path): |
| 85 | + marker = str(tmp_path / "sig") |
| 86 | + h = stub_device(transfer=dict( |
| 87 | + stdout_lines=["started"], idle=True, marker=marker, |
| 88 | + ignore_interrupt=True)) |
| 89 | + proc = h._run(["transfer", "-r", "x.iq"], mode="handle") |
| 90 | + assert _wait_for_output(proc, b"started") |
| 91 | + t0 = time.monotonic() |
| 92 | + out, err, rc = proc.stop(grace=0.5) |
| 93 | + assert time.monotonic() - t0 < 10.0 # bounded by grace + terminate |
| 94 | + assert not proc.is_alive() |
| 95 | + # no handler ran (SIG_IGN), so no marker content and a non-zero status: |
| 96 | + # the caller can tell this reap was NOT the clean flush path |
| 97 | + assert not os.path.exists(marker) or not open(marker).read().strip() |
| 98 | + assert rc != 0 |
0 commit comments