|
1 | 1 | import os |
2 | | -import pytest |
3 | | -import httpx |
4 | 2 | import random |
| 3 | +import signal |
| 4 | +import socket |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +import httpx |
| 10 | +import pytest |
5 | 11 |
|
6 | 12 |
|
7 | 13 | @pytest.fixture |
@@ -39,7 +45,7 @@ def create_dataset( |
39 | 45 | response = httpx.post( |
40 | 46 | url=url, |
41 | 47 | headers={"X-Dataverse-key": api_token}, |
42 | | - data=open("./tests/fixtures/create_dataset.json", "rb"), |
| 48 | + data=open("./tests/fixtures/create_dataset.json", "rb"), # type: ignore[reportUnboundVariable] |
43 | 49 | ) |
44 | 50 |
|
45 | 51 | response.raise_for_status() |
@@ -99,3 +105,78 @@ def create_mock_tabular_file( |
99 | 105 | ) |
100 | 106 |
|
101 | 107 | return path |
| 108 | + |
| 109 | + |
| 110 | +def _wait_for_port(host: str, port: int, timeout: float = 5.0) -> None: |
| 111 | + """Wait until a TCP port is open on host within timeout seconds.""" |
| 112 | + start = time.time() |
| 113 | + while time.time() - start < timeout: |
| 114 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 115 | + sock.settimeout(0.2) |
| 116 | + try: |
| 117 | + if sock.connect_ex((host, port)) == 0: |
| 118 | + return |
| 119 | + except OSError: |
| 120 | + pass |
| 121 | + time.sleep(0.1) |
| 122 | + raise TimeoutError(f"Proxy did not start on {host}:{port} within {timeout}s") |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture(scope="function") |
| 126 | +def http_proxy_server(): |
| 127 | + """Start a local HTTP proxy on 127.0.0.1:3128 for tests that require it.""" |
| 128 | + host = "127.0.0.1" |
| 129 | + port = 3128 |
| 130 | + |
| 131 | + # Ensure dependency is available |
| 132 | + try: |
| 133 | + import proxy # noqa: F401 |
| 134 | + except Exception as exc: # pragma: no cover |
| 135 | + pytest.skip( |
| 136 | + f"Skipping: proxy module not available ({exc}). Install 'proxy.py'." |
| 137 | + ) |
| 138 | + |
| 139 | + # Launch proxy.py as a subprocess to avoid API instability between versions |
| 140 | + cmd = [ |
| 141 | + sys.executable, |
| 142 | + "-m", |
| 143 | + "proxy", |
| 144 | + "--hostname", |
| 145 | + host, |
| 146 | + "--port", |
| 147 | + str(port), |
| 148 | + "--num-workers", |
| 149 | + "1", |
| 150 | + "--log-level", |
| 151 | + "WARNING", |
| 152 | + ] |
| 153 | + |
| 154 | + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 155 | + |
| 156 | + try: |
| 157 | + try: |
| 158 | + _wait_for_port(host, port, timeout=10.0) |
| 159 | + except TimeoutError: |
| 160 | + # Collect logs for debugging and skip the test instead of failing hard |
| 161 | + try: |
| 162 | + stdout, stderr = proc.communicate(timeout=1) |
| 163 | + except Exception: |
| 164 | + stdout, stderr = (b"", b"") |
| 165 | + msg = ( |
| 166 | + "Proxy did not start on " |
| 167 | + f"{host}:{port}. stderr: {stderr.decode(errors='ignore').strip()}" |
| 168 | + ) |
| 169 | + pytest.skip(msg) |
| 170 | + return |
| 171 | + |
| 172 | + yield f"http://{host}:{port}" |
| 173 | + finally: |
| 174 | + if proc.poll() is None: |
| 175 | + try: |
| 176 | + proc.send_signal(signal.SIGTERM) |
| 177 | + try: |
| 178 | + proc.wait(timeout=5) |
| 179 | + except subprocess.TimeoutExpired: |
| 180 | + proc.kill() |
| 181 | + except Exception: |
| 182 | + proc.kill() |
0 commit comments