Files
alzeo/tests/test_needle_engine_down.py
Hasan BasbunarandClaude Opus 5.5 2c7d4f61da tests/localports.py: what the second review found, no wrappers
_end() walked the tree under a process it might already have reaped, and
a pid used again by then would have had another program's children
signalled, on a box that runs other work: a server is the process start()
returns now, a wrapper execs it, and only that process is ended. With no
tree walk, a wrapper's child is no longer left running, and a poll reads
the process's own sockets first, the machine's tables only when it has
one. A server that listens on two ports is refused, since which one is
meant cannot be guessed, and one that ends before it listens says what
it wrote on its stderr when start() kept it in a pipe. serve() looked at
every listener of the machine, IPv6-only ones included, which cannot
take an IPv4 port: it reads the IPv4 ones on 127.0.0.1 or any. held()
returns the port alone, the test's cleanup keeping the socket. The
witness that a closed port was anyone's could itself be beaten to it:
the guard's failure on the old tree is the witness.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 11:51:55 +01:00

51 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""needle.sh says the engine is down when it is, instead of exiting 7 with nothing on screen.
Without --model it asks /v1/models first, through a `curl | python3` pipe inside `$(...)`
under `set -euo pipefail`: a curl that cannot connect exits 7, the assignment carries that
status, and set -e ended the script before the line that explains the failure (found in
review, 2026-09-24). Run against a port nothing listens on, with a HOME of its own.
"""
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import localports # noqa: E402
NEEDLE = Path(__file__).resolve().parents[1] / "needle.sh"
class AnEngineThatIsDown(unittest.TestCase):
def setUp(self):
self.home = tempfile.mkdtemp(prefix="needle-down-")
self.addCleanup(shutil.rmtree, self.home, ignore_errors=True)
os.makedirs(os.path.join(self.home, ".config", "qwen38"))
with open(os.path.join(self.home, ".config", "qwen38", "api-key"), "w") as f:
f.write("test-key\n")
self.port = localports.held(self) # nothing listens there, and no other program can
def run_needle(self, *args):
r = subprocess.run(["bash", str(NEEDLE), *args, "--trials", "1", "--depths", "1000", "--no-flush"],
capture_output=True, text=True, timeout=120,
env={**os.environ, "PORT": str(self.port), "HOME": self.home})
return r.returncode, r.stdout + r.stderr
def test_without_a_model_it_says_why_and_exits_2(self):
rc, out = self.run_needle()
self.assertEqual(rc, 2, out)
self.assertIn("no model served", out)
def test_with_a_model_the_calibration_says_why(self):
rc, out = self.run_needle("--model", "m")
self.assertEqual(rc, 2, out)
self.assertIn("calibration request failed", out)
if __name__ == "__main__":
unittest.main()