Files
alzeo/tests/test_needle_engine_down.py
T
Hasan BasbunarandClaude Opus 5.5 8bb32a2d17 tests: the port a test starts a server on stays its own beside the other jobs
ci-local.sh runs the six jobs side by side on one machine, and three of
them run the same test files. Eleven files took a port number out of a
socket they then closed, to start a proxy on it or to have a port where
nothing listens. Anyone could be handed that port before the server bound
it: a stranger could then answer in its place, and a dead port could come
alive. run_suite.py ran those files one at a time, which kept them apart
inside the suite job and not from the other jobs (code review,
2026-10-28, never seen in three runs).

tests/localports.py gives three answers. held() keeps a port bound and
never listening: a connection to it is refused and no other program can
bind it. serve() starts a server of the test's process on a held port,
beside the socket that keeps it (SO_REUSEPORT), so an engine that stops
and starts again keeps its port in between. spawn() starts a proxy in a
process of its own and keeps it once the socket listening on its port is
its own, read in /proc, else starts it again on another port. The
certificate test passes port 0, since nobody connects to a proxy that
refuses to start, and now checks that the refusal is the certificate's.

test_localports.py proves each answer on the machine, and fails on a test
file that takes a port the old way: on the tree before this commit it
names the eleven. With none left, the suite runs them four at a time.

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()