Skip to content

Commit 8b5a402

Browse files
committed
Fix telemetry test fixtures: Clean up state before AND after tests
Modified telemetry_setup_teardown fixtures to clean up TelemetryClientFactory state both BEFORE and AFTER each test, not just after. This prevents leftover state from previous tests (pending events, active executors) from interfering with the current test. Root cause: In CI with sequential execution on the same worker, if a previous test left pending telemetry events in the executor, those events could be captured by the next test's mock, causing inflated event counts (88 instead of 60). Now ensures complete isolation between tests by resetting all shared state before each test starts. Signed-off-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 4b2da91 commit 8b5a402

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

tests/e2e/test_concurrent_telemetry.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ def telemetry_setup_teardown(self):
3636
before each test and shuts it down afterward. Using a fixture makes
3737
this robust and automatic.
3838
"""
39+
# Clean up BEFORE test starts to ensure no leftover state from previous tests
40+
if TelemetryClientFactory._executor:
41+
TelemetryClientFactory._executor.shutdown(wait=True)
42+
TelemetryClientFactory._executor = None
43+
TelemetryClientFactory._stop_flush_thread()
44+
TelemetryClientFactory._clients.clear()
45+
TelemetryClientFactory._initialized = False
46+
3947
try:
4048
yield
4149
finally:
50+
# Clean up AFTER test ends
4251
if TelemetryClientFactory._executor:
4352
TelemetryClientFactory._executor.shutdown(wait=True)
4453
TelemetryClientFactory._executor = None

tests/e2e/test_telemetry_e2e.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,34 @@ class TestTelemetryE2E(TelemetryTestBase):
5151
@pytest.fixture(autouse=True)
5252
def telemetry_setup_teardown(self):
5353
"""Clean up telemetry client state before and after each test"""
54+
# Clean up BEFORE test starts
55+
if TelemetryClientFactory._executor:
56+
TelemetryClientFactory._executor.shutdown(wait=True)
57+
TelemetryClientFactory._executor = None
58+
TelemetryClientFactory._stop_flush_thread()
59+
TelemetryClientFactory._clients.clear()
60+
TelemetryClientFactory._initialized = False
61+
62+
# Clear feature flags cache before test starts
63+
from databricks.sql.common.feature_flag import FeatureFlagsContextFactory
64+
with FeatureFlagsContextFactory._lock:
65+
FeatureFlagsContextFactory._context_map.clear()
66+
if FeatureFlagsContextFactory._executor:
67+
FeatureFlagsContextFactory._executor.shutdown(wait=False)
68+
FeatureFlagsContextFactory._executor = None
69+
5470
try:
5571
yield
5672
finally:
73+
# Clean up AFTER test ends
5774
if TelemetryClientFactory._executor:
5875
TelemetryClientFactory._executor.shutdown(wait=True)
5976
TelemetryClientFactory._executor = None
6077
TelemetryClientFactory._stop_flush_thread()
78+
TelemetryClientFactory._clients.clear()
6179
TelemetryClientFactory._initialized = False
6280

63-
# Clear feature flags cache to prevent state leakage between tests
64-
from databricks.sql.common.feature_flag import FeatureFlagsContextFactory
81+
# Clear feature flags cache after test ends
6582
with FeatureFlagsContextFactory._lock:
6683
FeatureFlagsContextFactory._context_map.clear()
6784
if FeatureFlagsContextFactory._executor:

0 commit comments

Comments
 (0)