|
9 | 9 | import time |
10 | 10 | import urllib.request |
11 | 11 | import urllib.error |
| 12 | +import tempfile |
12 | 13 | import pytest |
13 | 14 | import importlib |
14 | 15 | from pathlib import Path |
| 16 | +from filelock import FileLock |
15 | 17 |
|
16 | 18 | # Root of the typespec-python package |
17 | 19 | ROOT = Path(__file__).parent.parent |
|
22 | 24 | SERVER_PORT = 3000 |
23 | 25 | SERVER_URL = f"http://{SERVER_HOST}:{SERVER_PORT}" |
24 | 26 |
|
| 27 | +# Lock file for coordinating server startup across xdist workers |
| 28 | +LOCK_FILE = Path(tempfile.gettempdir()) / "typespec_python_test_server.lock" |
| 29 | +PID_FILE = Path(tempfile.gettempdir()) / "typespec_python_test_server.pid" |
| 30 | + |
25 | 31 | # Global server process reference (used by hooks) |
26 | 32 | _server_process = None |
| 33 | +_owns_server = False # Track if this process started the server |
27 | 34 |
|
28 | 35 |
|
29 | 36 | def wait_for_server(url: str, timeout: int = 60, interval: float = 0.5) -> bool: |
@@ -99,51 +106,77 @@ def terminate_server_process(process): |
99 | 106 | def pytest_configure(config): |
100 | 107 | """Start the mock server before any tests run. |
101 | 108 |
|
102 | | - This hook runs in the controller process before xdist workers are spawned, |
103 | | - ensuring the server is ready for all workers. |
| 109 | + Uses file locking to ensure only one process starts the server, |
| 110 | + even when running with pytest-xdist. The controller process starts |
| 111 | + the server and workers wait for it to be ready. |
104 | 112 | """ |
105 | | - global _server_process |
106 | | - |
107 | | - # Only start server in the controller process (not in workers) |
108 | | - # xdist workers have workerinput attribute |
109 | | - if hasattr(config, "workerinput"): |
110 | | - return |
| 113 | + global _server_process, _owns_server |
111 | 114 |
|
112 | | - # Check if server is already running (e.g., from a previous run) |
| 115 | + # Check if server is already running (e.g., from a previous run or external process) |
113 | 116 | if wait_for_server(SERVER_URL, timeout=1, interval=0.1): |
114 | 117 | print(f"Mock API server already running at {SERVER_URL}") |
115 | 118 | return |
116 | 119 |
|
117 | | - # Start the server |
118 | | - print(f"Starting mock API server...") |
119 | | - _server_process = start_server_process() |
120 | | - |
121 | | - # Check if process started successfully |
122 | | - if _server_process.poll() is not None: |
123 | | - pytest.exit(f"Mock API server process exited immediately with code {_server_process.returncode}") |
124 | | - |
125 | | - # Wait for server to be ready |
126 | | - if not wait_for_server(SERVER_URL, timeout=60): |
127 | | - # Check if process is still running |
128 | | - if _server_process.poll() is not None: |
129 | | - pytest.exit(f"Mock API server process died with code {_server_process.returncode}") |
130 | | - terminate_server_process(_server_process) |
131 | | - _server_process = None |
132 | | - pytest.exit(f"Mock API server failed to start within 60 seconds at {SERVER_URL}") |
| 120 | + # Use file lock to ensure only one process starts the server |
| 121 | + # This handles both xdist workers and multiple test runs |
| 122 | + lock = FileLock(str(LOCK_FILE), timeout=120) |
133 | 123 |
|
134 | | - print(f"Mock API server ready at {SERVER_URL}") |
| 124 | + try: |
| 125 | + with lock: |
| 126 | + # Double-check after acquiring lock (another process may have started it) |
| 127 | + if wait_for_server(SERVER_URL, timeout=1, interval=0.1): |
| 128 | + print(f"Mock API server already running at {SERVER_URL}") |
| 129 | + return |
| 130 | + |
| 131 | + # We're the first process - start the server |
| 132 | + print(f"Starting mock API server...") |
| 133 | + _server_process = start_server_process() |
| 134 | + _owns_server = True |
| 135 | + |
| 136 | + # Check if process started successfully |
| 137 | + if _server_process.poll() is not None: |
| 138 | + pytest.exit(f"Mock API server process exited immediately with code {_server_process.returncode}") |
| 139 | + |
| 140 | + # Write PID file so other processes know who owns the server |
| 141 | + PID_FILE.write_text(str(_server_process.pid)) |
| 142 | + |
| 143 | + # Wait for server to be ready |
| 144 | + if not wait_for_server(SERVER_URL, timeout=60): |
| 145 | + if _server_process.poll() is not None: |
| 146 | + pytest.exit(f"Mock API server process died with code {_server_process.returncode}") |
| 147 | + terminate_server_process(_server_process) |
| 148 | + _server_process = None |
| 149 | + _owns_server = False |
| 150 | + pytest.exit(f"Mock API server failed to start within 60 seconds at {SERVER_URL}") |
| 151 | + |
| 152 | + print(f"Mock API server ready at {SERVER_URL}") |
| 153 | + |
| 154 | + except TimeoutError: |
| 155 | + # Another process is holding the lock for too long |
| 156 | + # Check if server is available anyway |
| 157 | + if wait_for_server(SERVER_URL, timeout=5): |
| 158 | + print(f"Mock API server available at {SERVER_URL} (started by another process)") |
| 159 | + else: |
| 160 | + pytest.exit("Timeout waiting for server lock - another process may be stuck") |
135 | 161 |
|
136 | 162 |
|
137 | 163 | def pytest_unconfigure(config): |
138 | 164 | """Stop the mock server after all tests complete.""" |
139 | | - global _server_process |
| 165 | + global _server_process, _owns_server |
140 | 166 |
|
141 | | - # Only stop server in the controller process |
142 | | - if hasattr(config, "workerinput"): |
| 167 | + # Only stop the server if this process started it |
| 168 | + if not _owns_server: |
143 | 169 | return |
144 | 170 |
|
145 | 171 | terminate_server_process(_server_process) |
146 | 172 | _server_process = None |
| 173 | + _owns_server = False |
| 174 | + |
| 175 | + # Clean up PID file |
| 176 | + try: |
| 177 | + PID_FILE.unlink(missing_ok=True) |
| 178 | + except Exception: |
| 179 | + pass |
147 | 180 |
|
148 | 181 |
|
149 | 182 | @pytest.fixture(scope="session", autouse=True) |
|
0 commit comments