|
14 | 14 | import pytest |
15 | 15 |
|
16 | 16 | from tests.constants import ( |
17 | | - CLEANUP_DELAY, |
18 | 17 | DEFAULT_TIMEOUT, |
19 | 18 | JS_RESULT_RETRIES, |
20 | 19 | REDIS_ALPINE_IMAGE, |
21 | 20 | REDIS_IMAGE, |
22 | 21 | REDIS_TEST_TTL, |
23 | 22 | SHORT_TIMEOUT, |
24 | | - SUBPROCESS_TERMINATION_DELAY, |
25 | 23 | ) |
26 | 24 |
|
27 | 25 |
|
|
40 | 38 | # ============================================================================= |
41 | 39 |
|
42 | 40 |
|
43 | | -@pytest.fixture(autouse=True) |
44 | | -def cleanup_runtime(): |
45 | | - """Ensure runtime is completely fresh for each test. |
46 | | -
|
47 | | - This is the SINGLE cleanup fixture used across ALL test files. |
48 | | - It handles: |
49 | | - - Stopping the runtime process |
50 | | - - Clearing callback registry |
51 | | - - Clearing window lifecycle state |
52 | | - - Proper timing to avoid race conditions |
53 | | - """ |
| 41 | +def _stop_runtime_sync() -> None: |
| 42 | + """Stop runtime and wait for subprocess to fully terminate.""" |
54 | 43 | from pywry import runtime |
55 | | - from pywry.callbacks import get_registry |
56 | | - from pywry.window_manager import get_lifecycle |
57 | 44 |
|
58 | | - # Pre-test cleanup |
| 45 | + if not runtime.is_running(): |
| 46 | + return |
| 47 | + |
59 | 48 | runtime.stop() |
60 | | - time.sleep(SUBPROCESS_TERMINATION_DELAY) # Allow subprocess to fully terminate |
61 | 49 |
|
62 | | - registry = get_registry() |
63 | | - registry.clear() |
64 | | - get_lifecycle().clear() |
| 50 | + # Poll until runtime is actually stopped |
| 51 | + deadline = time.monotonic() + 5.0 |
| 52 | + while runtime.is_running() and time.monotonic() < deadline: |
| 53 | + time.sleep(0.05) |
65 | 54 |
|
66 | | - yield |
67 | 55 |
|
68 | | - # Post-test cleanup |
69 | | - runtime.stop() |
70 | | - time.sleep(CLEANUP_DELAY) # Brief delay before next test |
71 | | - registry.clear() |
| 56 | +def _clear_registries() -> None: |
| 57 | + """Clear all callback and lifecycle registries.""" |
| 58 | + from pywry.callbacks import get_registry |
| 59 | + from pywry.window_manager import get_lifecycle |
| 60 | + |
| 61 | + get_registry().clear() |
72 | 62 | get_lifecycle().clear() |
73 | 63 |
|
74 | 64 |
|
| 65 | +@pytest.fixture(autouse=True) |
| 66 | +def cleanup_runtime(request): |
| 67 | + """Ensure runtime state is clean for each test. |
| 68 | +
|
| 69 | + This fixture has DIFFERENT behavior based on test class: |
| 70 | + - For tests using class-scoped app fixtures: only clears registries (no subprocess restart) |
| 71 | + - For standalone tests: full subprocess cleanup |
| 72 | +
|
| 73 | + The subprocess lifecycle is managed by class-scoped fixtures (dark_app, light_app) |
| 74 | + to prevent race conditions from repeated start/stop cycles. |
| 75 | + """ |
| 76 | + # Check if this test uses a class-scoped app fixture |
| 77 | + # If so, we MUST NOT stop the runtime - just clear registries |
| 78 | + uses_class_app = ( |
| 79 | + hasattr(request, "cls") |
| 80 | + and request.cls is not None |
| 81 | + and hasattr(request.cls, "_pywry_class_scoped") |
| 82 | + ) |
| 83 | + |
| 84 | + if uses_class_app: |
| 85 | + # Only clear registries, don't touch the subprocess |
| 86 | + _clear_registries() |
| 87 | + yield |
| 88 | + _clear_registries() |
| 89 | + else: |
| 90 | + # Standalone test: full cleanup |
| 91 | + _stop_runtime_sync() |
| 92 | + _clear_registries() |
| 93 | + yield |
| 94 | + _stop_runtime_sync() |
| 95 | + _clear_registries() |
| 96 | + |
| 97 | + |
| 98 | +# ============================================================================= |
| 99 | +# Class-Scoped App Fixtures - PREVENTS RACE CONDITIONS |
| 100 | +# ============================================================================= |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture(scope="class") |
| 104 | +def dark_app(request): |
| 105 | + """Class-scoped PyWry app with DARK theme. |
| 106 | +
|
| 107 | + This fixture: |
| 108 | + 1. Creates ONE PyWry instance for the entire test class |
| 109 | + 2. Starts the subprocess ONCE at class setup |
| 110 | + 3. Stops the subprocess ONCE at class teardown |
| 111 | + 4. Prevents race conditions from repeated subprocess restarts |
| 112 | + """ |
| 113 | + from pywry.app import PyWry |
| 114 | + from pywry.models import ThemeMode |
| 115 | + |
| 116 | + # Stop any existing runtime first |
| 117 | + _stop_runtime_sync() |
| 118 | + _clear_registries() |
| 119 | + |
| 120 | + # Mark the class as using class-scoped app (pylint: disable=protected-access) |
| 121 | + if request.cls is not None: |
| 122 | + request.cls._pywry_class_scoped = True |
| 123 | + |
| 124 | + app = PyWry(theme=ThemeMode.DARK) |
| 125 | + yield app |
| 126 | + |
| 127 | + # Cleanup: close all windows and stop runtime |
| 128 | + app.close() |
| 129 | + _stop_runtime_sync() |
| 130 | + _clear_registries() |
| 131 | + |
| 132 | + |
| 133 | +@pytest.fixture(scope="class") |
| 134 | +def light_app(request): |
| 135 | + """Class-scoped PyWry app with LIGHT theme. |
| 136 | +
|
| 137 | + Same as dark_app but with LIGHT theme. |
| 138 | + """ |
| 139 | + from pywry.app import PyWry |
| 140 | + from pywry.models import ThemeMode |
| 141 | + |
| 142 | + _stop_runtime_sync() |
| 143 | + _clear_registries() |
| 144 | + |
| 145 | + # Mark the class as using class-scoped app (pylint: disable=protected-access) |
| 146 | + if request.cls is not None: |
| 147 | + request.cls._pywry_class_scoped = True |
| 148 | + |
| 149 | + app = PyWry(theme=ThemeMode.LIGHT) |
| 150 | + yield app |
| 151 | + |
| 152 | + app.close() |
| 153 | + _stop_runtime_sync() |
| 154 | + _clear_registries() |
| 155 | + |
| 156 | + |
75 | 157 | # ============================================================================= |
76 | 158 | # Shared Test Helpers - SINGLE SOURCE OF TRUTH |
77 | 159 | # ============================================================================= |
|
0 commit comments