|
24 | 24 | from __future__ import annotations |
25 | 25 |
|
26 | 26 | import multiprocessing |
| 27 | +import sys |
27 | 28 |
|
28 | 29 | multiprocessing.set_start_method("spawn", force=True) |
29 | 30 |
|
|
35 | 36 | import pytest |
36 | 37 | import openml_sklearn |
37 | 38 |
|
| 39 | +import time |
| 40 | +import subprocess |
| 41 | +import requests |
38 | 42 | import openml |
39 | 43 | from openml.testing import TestBase |
40 | 44 |
|
@@ -296,6 +300,44 @@ def with_test_cache(test_files_directory, request): |
296 | 300 | if tmp_cache.exists(): |
297 | 301 | shutil.rmtree(tmp_cache) |
298 | 302 |
|
| 303 | +# This starts the entire stack once for the whole test run |
| 304 | +@pytest.fixture(scope="session", autouse=True) |
| 305 | +def openml_docker_stack(): |
| 306 | + # if sys.platform == "win32": |
| 307 | + # yield |
| 308 | + # return |
| 309 | + # 1. Start the containers defined in your final docker-compose.yml |
| 310 | + subprocess.run(["docker", "compose", "up", "-d"], check=True) |
| 311 | + |
| 312 | + # 2. Wait for the database setup worker to finish its tasks |
| 313 | + # This ensures update.sh has finished before we hit the APIs |
| 314 | + subprocess.run(["docker", "wait", "openml-test-setup-ci"], check=True) |
| 315 | + |
| 316 | + # 3. Quick health check: Wait for the Python API to respond on port 9001 |
| 317 | + timeout = 30 |
| 318 | + start = time.time() |
| 319 | + while time.time() - start < timeout: |
| 320 | + try: |
| 321 | + if requests.get("http://localhost:9001/api/v2/").status_code == 200: |
| 322 | + break |
| 323 | + except requests.exceptions.ConnectionError: |
| 324 | + time.sleep(1) |
| 325 | + |
| 326 | + yield # Tests run here |
| 327 | + |
| 328 | + # 4. Tear everything down after tests finish to keep the machine clean |
| 329 | + subprocess.run(["docker", "compose", "down", "-v"], check=True) |
| 330 | + |
| 331 | +# This resets the database state before every single test to prevent race conditions |
| 332 | +@pytest.fixture(scope="function", autouse=True) |
| 333 | +def reset_db_state(): |
| 334 | + # if sys.platform == "win32": |
| 335 | + # yield |
| 336 | + # return |
| 337 | + # Fast restart of the database container to return to the 'baked-in' state |
| 338 | + subprocess.run(["docker", "compose", "restart", "database"], check=True) |
| 339 | + # Re-run the setup worker to ensure paths are still correct |
| 340 | + subprocess.run(["docker", "compose", "up", "database-setup"], check=True) |
299 | 341 |
|
300 | 342 | @pytest.fixture |
301 | 343 | def static_cache_dir(): |
|
0 commit comments