Skip to content

Commit f9f13c3

Browse files
authored
enhance python tests (#36852)
* make test cleanup conditional to fix performance regression * fixed PEP 8 violations and optimize test cleanup * Change cleanup fixtures to class scope to reduce test overhead
1 parent 6d94d5c commit f9f13c3

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

sdks/python/conftest.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
"""Pytest configuration and custom hooks."""
1919

20+
import gc
2021
import os
2122
import sys
23+
import threading
24+
import time
2225
from types import SimpleNamespace
2326

2427
import pytest
@@ -101,55 +104,50 @@ def configure_beam_rpc_timeouts():
101104
print("Successfully configured Beam RPC timeouts")
102105

103106

104-
@pytest.fixture(autouse=True)
107+
@pytest.fixture(scope="class", autouse=True)
105108
def ensure_clean_state():
106109
"""
107-
Ensure clean state before each test
110+
Ensure clean state before each test class
108111
to prevent cross-test contamination.
112+
Runs once per test class instead of per test to reduce overhead.
109113
"""
110-
import gc
111-
import threading
112-
import time
113-
114114
# Force garbage collection to clean up any lingering resources
115115
gc.collect()
116116

117117
# Log active thread count for debugging
118118
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")
122121
# Force a brief pause to let threads settle
123122
time.sleep(0.5)
124123
gc.collect()
125124

126125
yield
127126

128-
# Enhanced cleanup after test
127+
# Enhanced cleanup after test class
129128
try:
130129
# Force more aggressive cleanup
131130
gc.collect()
132-
133131
# Brief pause to let any async operations complete
134132
time.sleep(0.1)
135-
136133
# Additional garbage collection
137134
gc.collect()
138135
except Exception as e:
139136
print(f"Warning: Cleanup error: {e}")
140137

141138

142-
@pytest.fixture(autouse=True)
139+
@pytest.fixture(scope="class", autouse=True)
143140
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
148146
time.sleep(0.05)
149147

150148
yield
151149

152-
# Brief pause after test to let mocks clean up
150+
# Brief pause after test class to let mocks clean up
153151
time.sleep(0.05)
154152

155153

0 commit comments

Comments
 (0)