|
17 | 17 |
|
18 | 18 | """Pytest configuration and custom hooks.""" |
19 | 19 |
|
| 20 | +import gc |
20 | 21 | import os |
21 | 22 | import sys |
| 23 | +import threading |
| 24 | +import time |
22 | 25 | from types import SimpleNamespace |
23 | 26 |
|
24 | 27 | import pytest |
@@ -101,55 +104,50 @@ def configure_beam_rpc_timeouts(): |
101 | 104 | print("Successfully configured Beam RPC timeouts") |
102 | 105 |
|
103 | 106 |
|
104 | | -@pytest.fixture(autouse=True) |
| 107 | +@pytest.fixture(scope="class", autouse=True) |
105 | 108 | def ensure_clean_state(): |
106 | 109 | """ |
107 | | - Ensure clean state before each test |
| 110 | + Ensure clean state before each test class |
108 | 111 | to prevent cross-test contamination. |
| 112 | + Runs once per test class instead of per test to reduce overhead. |
109 | 113 | """ |
110 | | - import gc |
111 | | - import threading |
112 | | - import time |
113 | | - |
114 | 114 | # Force garbage collection to clean up any lingering resources |
115 | 115 | gc.collect() |
116 | 116 |
|
117 | 117 | # Log active thread count for debugging |
118 | 118 | thread_count = threading.active_count() |
119 | | - if thread_count > 50: # Increased threshold since we see 104 threads |
120 | | - print(f"Warning: {thread_count} active threads detected before test") |
121 | | - |
| 119 | + if thread_count > 50: |
| 120 | + print(f"Warning: {thread_count} active threads detected before test class") |
122 | 121 | # Force a brief pause to let threads settle |
123 | 122 | time.sleep(0.5) |
124 | 123 | gc.collect() |
125 | 124 |
|
126 | 125 | yield |
127 | 126 |
|
128 | | - # Enhanced cleanup after test |
| 127 | + # Enhanced cleanup after test class |
129 | 128 | try: |
130 | 129 | # Force more aggressive cleanup |
131 | 130 | gc.collect() |
132 | | - |
133 | 131 | # Brief pause to let any async operations complete |
134 | 132 | time.sleep(0.1) |
135 | | - |
136 | 133 | # Additional garbage collection |
137 | 134 | gc.collect() |
138 | 135 | except Exception as e: |
139 | 136 | print(f"Warning: Cleanup error: {e}") |
140 | 137 |
|
141 | 138 |
|
142 | | -@pytest.fixture(autouse=True) |
| 139 | +@pytest.fixture(scope="class", autouse=True) |
143 | 140 | def enhance_mock_stability(): |
144 | | - """Enhance mock stability in DinD environment.""" |
145 | | - import time |
146 | | - |
147 | | - # Brief pause before test to ensure clean mock state |
| 141 | + """ |
| 142 | + Enhance mock stability in DinD environment. |
| 143 | + Runs once per test class instead of per test to reduce overhead. |
| 144 | + """ |
| 145 | + # Brief pause before test class to ensure clean mock state |
148 | 146 | time.sleep(0.05) |
149 | 147 |
|
150 | 148 | yield |
151 | 149 |
|
152 | | - # Brief pause after test to let mocks clean up |
| 150 | + # Brief pause after test class to let mocks clean up |
153 | 151 | time.sleep(0.05) |
154 | 152 |
|
155 | 153 |
|
|
0 commit comments