Skip to content

Commit bca9baf

Browse files
committed
chore: add cleanup at the end of each test
1 parent 5e480c4 commit bca9baf

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

tests/integration/container/conftest.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@
6161
logger = Logger(__name__)
6262

6363

64+
def _reset_wrapper_state():
65+
"""Reset all wrapper caches, thread pools, and global state.
66+
67+
This should be called after each test to ensure clean state and prevent
68+
background threads from interfering with subsequent tests.
69+
"""
70+
RdsUtils.clear_cache()
71+
StorageService.clear_all()
72+
RdsHostListProvider._is_primary_cluster_id_cache.clear()
73+
RdsHostListProvider._cluster_ids_to_update.clear()
74+
PluginServiceImpl._host_availability_expiring_cache.clear()
75+
DatabaseDialectManager._known_endpoint_dialects.clear()
76+
CustomEndpointMonitor._custom_endpoint_info_cache.clear()
77+
MonitoringThreadContainer.clean_up()
78+
ThreadPoolContainer.release_resources(wait=True)
79+
SlidingExpirationCacheContainer.release_resources()
80+
81+
ConnectionProviderManager.release_resources()
82+
ConnectionProviderManager.reset_provider()
83+
DatabaseDialectManager.reset_custom_dialect()
84+
DriverDialectManager.reset_custom_dialect()
85+
ExceptionManager.reset_custom_handler()
86+
87+
6488
@pytest.fixture(scope='module')
6589
def conn_utils():
6690
return ConnectionUtils()
@@ -142,22 +166,7 @@ def pytest_runtest_setup(item):
142166

143167
assert cluster_ip == writer_ip
144168

145-
RdsUtils.clear_cache()
146-
StorageService.clear_all()
147-
RdsHostListProvider._is_primary_cluster_id_cache.clear()
148-
RdsHostListProvider._cluster_ids_to_update.clear()
149-
PluginServiceImpl._host_availability_expiring_cache.clear()
150-
DatabaseDialectManager._known_endpoint_dialects.clear()
151-
CustomEndpointMonitor._custom_endpoint_info_cache.clear()
152-
MonitoringThreadContainer.clean_up()
153-
ThreadPoolContainer.release_resources(wait=True)
154-
SlidingExpirationCacheContainer.release_resources()
155-
156-
ConnectionProviderManager.release_resources()
157-
ConnectionProviderManager.reset_provider()
158-
DatabaseDialectManager.reset_custom_dialect()
159-
DriverDialectManager.reset_custom_dialect()
160-
ExceptionManager.reset_custom_handler()
169+
_reset_wrapper_state()
161170

162171
if TestEnvironmentFeatures.TELEMETRY_TRACES_ENABLED in TestEnvironment.get_current().get_features() \
163172
and segment is not None:
@@ -177,9 +186,21 @@ def pytest_sessionstart(session):
177186
TestEnvironment.get_current()
178187

179188

189+
def pytest_runtest_teardown(item, nextitem):
190+
"""Clean up resources after each test to prevent threads from previous tests
191+
interfering with subsequent tests or causing segfaults."""
192+
request = TestEnvironment.get_current().get_info().get_request()
193+
194+
if TestEnvironmentFeatures.NETWORK_OUTAGES_ENABLED in request.get_features():
195+
ProxyHelper.enable_all_connectivity()
196+
197+
_reset_wrapper_state()
198+
199+
180200
def pytest_sessionfinish(session, exitstatus):
181201
# Enable all connectivity in case any helper threads are still trying to execute against a disabled host
182202
ProxyHelper.enable_all_connectivity()
203+
183204

184205

185206
def log_exit():

0 commit comments

Comments
 (0)