diff --git a/tests/test_claude_local.py b/tests/test_claude_local.py index f2be5a4a..4121e670 100644 --- a/tests/test_claude_local.py +++ b/tests/test_claude_local.py @@ -12,7 +12,6 @@ discussion #59's reply gave to copy there, which they keep beside and replace."" import importlib.util import json import os -import socket import pathlib import subprocess import sys @@ -21,6 +20,9 @@ import threading import unittest from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) +import localports # noqa: E402 + REPO = pathlib.Path(__file__).resolve().parents[1] SCRIPT = REPO / "claude-local.py" spec = importlib.util.spec_from_file_location("claude_local", SCRIPT) @@ -282,9 +284,7 @@ class EndToEnd(unittest.TestCase): self.assertNotIn("may be starting", r.stderr) def test_a_proxy_that_does_not_run_is_said_not_a_lane_missing(self): - with socket.socket() as s: - s.bind(("127.0.0.1", 0)) - port = s.getsockname()[1] + port = localports.held(self) r, _ = self.launch(env={"CLAUDE_LOCAL_URL": f"http://127.0.0.1:{port}"}) self.assertEqual(r.returncode, 1) self.assertIn("nothing of this box answers at", r.stderr) diff --git a/tests/test_proxy_connection_ceiling.py b/tests/test_proxy_connection_ceiling.py index bfa18194..ccf87332 100644 --- a/tests/test_proxy_connection_ceiling.py +++ b/tests/test_proxy_connection_ceiling.py @@ -32,6 +32,9 @@ import time import unittest from pathlib import Path +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import localports # noqa: E402 + def _keep_env(cls): """Put os.environ back as this class found it (tests/test_suite_isolation.py holds it).""" @@ -348,15 +351,13 @@ class NoDescriptorLeft(unittest.TestCase): threading.Thread(target=engine.serve_forever, daemon=True).start() self.addCleanup(engine.server_close) self.addCleanup(engine.shutdown) - with socket.socket() as s: - s.bind(("127.0.0.1", 0)) - port = s.getsockname()[1] with tempfile.TemporaryDirectory() as home, open(os.path.join(home, "journal"), "w+") as journal: - proc = subprocess.Popen([sys.executable, "-u", str(HERE.parents[1] / "keepalive-proxy.py"), str(port)], - env={"PATH": os.environ.get("PATH", ""), "HOME": home, "PROXY_BIND": "127.0.0.1", - "UPSTREAM": f"http://127.0.0.1:{engine.server_port}"}, - stdout=journal, stderr=journal, - preexec_fn=lambda: resource.setrlimit(resource.RLIMIT_NOFILE, (64, 64))) + proc, port = localports.spawn(lambda port: subprocess.Popen( + [sys.executable, "-u", str(HERE.parents[1] / "keepalive-proxy.py"), str(port)], + env={"PATH": os.environ.get("PATH", ""), "HOME": home, "PROXY_BIND": "127.0.0.1", + "UPSTREAM": f"http://127.0.0.1:{engine.server_port}"}, + stdout=journal, stderr=journal, + preexec_fn=lambda: resource.setrlimit(resource.RLIMIT_NOFILE, (64, 64)))) self.addCleanup(proc.wait) self.addCleanup(proc.kill) held = [] diff --git a/tests/test_proxy_engine_gone.py b/tests/test_proxy_engine_gone.py index 3787c1e9..ab524b62 100644 --- a/tests/test_proxy_engine_gone.py +++ b/tests/test_proxy_engine_gone.py @@ -8,7 +8,6 @@ import http.server import importlib.util import json import os -import socket import sys import threading import time @@ -18,6 +17,9 @@ import urllib.request from pathlib import Path from types import SimpleNamespace +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import localports # noqa: E402 + REPO = Path(__file__).resolve().parents[1] KEY = "k3y-of-this-test-0123456789" UNITS = ("qwen38-sglang.service", "qwen38-flash.service") @@ -67,9 +69,7 @@ class Base(unittest.TestCase): threading.Thread(target=cls.engine.serve_forever, daemon=True).start() cls.addClassCleanup(cls.engine.server_close) cls.addClassCleanup(cls.engine.shutdown) - with socket.socket() as s: - s.bind(("127.0.0.1", 0)) - cls.nobody = f"127.0.0.1:{s.getsockname()[1]}" + cls.nobody = f"127.0.0.1:{localports.held(SimpleNamespace(addCleanup=cls.addClassCleanup))}" def serve(self, upstream): mod = load(f"http://{upstream}")