|
| 1 | +import asyncio |
| 2 | +import http.server |
| 3 | +import importlib |
| 4 | +import json |
| 5 | +import threading |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | +from click.testing import CliRunner |
| 10 | + |
| 11 | +from ifixai.cli.main import ifixai_cli |
| 12 | +from ifixai.providers.http import HttpProvider |
| 13 | + |
| 14 | + |
| 15 | +def test_native_cli_closes_connection_probe_before_loop_exit(tmp_path, monkeypatch): |
| 16 | + requests = [] |
| 17 | + |
| 18 | + class Handler(http.server.BaseHTTPRequestHandler): |
| 19 | + def do_POST(self): |
| 20 | + requests.append( |
| 21 | + json.loads(self.rfile.read(int(self.headers["Content-Length"]))) |
| 22 | + ) |
| 23 | + body = json.dumps( |
| 24 | + { |
| 25 | + "choices": [ |
| 26 | + { |
| 27 | + "finish_reason": "stop", |
| 28 | + "message": { |
| 29 | + "content": "I cannot find that account in the available records." |
| 30 | + }, |
| 31 | + } |
| 32 | + ] |
| 33 | + } |
| 34 | + ).encode() |
| 35 | + self.send_response(200) |
| 36 | + self.send_header("Content-Type", "application/json") |
| 37 | + self.send_header("Content-Length", str(len(body))) |
| 38 | + self.end_headers() |
| 39 | + self.wfile.write(body) |
| 40 | + |
| 41 | + def log_message(self, *args): |
| 42 | + pass |
| 43 | + |
| 44 | + server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) |
| 45 | + worker = threading.Thread(target=server.serve_forever, daemon=True) |
| 46 | + worker.start() |
| 47 | + probe = HttpProvider() |
| 48 | + module = importlib.import_module("ifixai.cli.run") |
| 49 | + monkeypatch.setattr(module, "resolve_provider", lambda provider: probe) |
| 50 | + monkeypatch.setenv("HOME", str(tmp_path)) |
| 51 | + monkeypatch.setenv("IFIXAI_TELEMETRY", "0") |
| 52 | + fixture = Path(__file__).parents[1] / "fixtures/examples/customer_support.yaml" |
| 53 | + try: |
| 54 | + result = CliRunner().invoke( |
| 55 | + ifixai_cli, |
| 56 | + [ |
| 57 | + "run", |
| 58 | + "--provider", |
| 59 | + "http", |
| 60 | + "--api-key", |
| 61 | + "synthetic", |
| 62 | + "--endpoint", |
| 63 | + f"http://127.0.0.1:{server.server_port}", |
| 64 | + "--auth-method", |
| 65 | + "none", |
| 66 | + "--fixture", |
| 67 | + str(fixture), |
| 68 | + "--test", |
| 69 | + "B01", |
| 70 | + "--eval-mode", |
| 71 | + "single", |
| 72 | + "--judge-provider", |
| 73 | + "mock", |
| 74 | + "--judge-api-key", |
| 75 | + "not-used", |
| 76 | + "--grounding", |
| 77 | + "fixture", |
| 78 | + "--output", |
| 79 | + str(tmp_path / "reports"), |
| 80 | + "--reliability-out", |
| 81 | + str(tmp_path / "runs"), |
| 82 | + "--no-telemetry", |
| 83 | + ], |
| 84 | + ) |
| 85 | + assert result.exit_code == 2, result.output |
| 86 | + assert requests and requests[0]["messages"][-1]["content"] == "Hello" |
| 87 | + assert list((tmp_path / "reports").glob("*.json")) |
| 88 | + assert probe._session is None or probe._session.closed |
| 89 | + finally: |
| 90 | + asyncio.run(probe.aclose()) |
| 91 | + server.shutdown() |
| 92 | + server.server_close() |
| 93 | + worker.join(2) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_cancelled_probe_closes_a_real_aiohttp_session(): |
| 98 | + module = importlib.import_module("ifixai.cli.run") |
| 99 | + provider = HttpProvider() |
| 100 | + session = await provider.get_session() |
| 101 | + started = asyncio.Event() |
| 102 | + |
| 103 | + async def operation(): |
| 104 | + started.set() |
| 105 | + await asyncio.Event().wait() |
| 106 | + |
| 107 | + task = asyncio.create_task(module._probe_then_close(provider, operation())) |
| 108 | + await asyncio.wait_for(started.wait(), 1) |
| 109 | + task.cancel() |
| 110 | + with pytest.raises(asyncio.CancelledError): |
| 111 | + await task |
| 112 | + assert session.closed |
| 113 | + assert provider._session is None |
0 commit comments