Skip to content

Commit 4e2ea59

Browse files
committed
test: stop event processors via an autouse fixture
Flagsmith(enable_events=True) starts a background flush timer. Track and stop every started EventProcessor on teardown so timers don't leak across the suite, and drop the scattered per-test try/finally cleanup.
1 parent 237dfa2 commit 4e2ea59

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@
2323
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
2424

2525

26+
@pytest.fixture(autouse=True)
27+
def stop_event_processors(
28+
monkeypatch: pytest.MonkeyPatch,
29+
) -> Generator[None, None, None]:
30+
# `EventProcessor.start()` schedules a background flush timer. Track every
31+
# processor started during a test and stop it on teardown so timers don't
32+
# leak across the suite.
33+
started: typing.List[EventProcessor] = []
34+
original_start = EventProcessor.start
35+
36+
def tracking_start(self: EventProcessor) -> None:
37+
started.append(self)
38+
original_start(self)
39+
40+
monkeypatch.setattr(EventProcessor, "start", tracking_start)
41+
yield
42+
for processor in started:
43+
processor.stop()
44+
45+
2646
@pytest.fixture()
2747
def analytics_processor() -> AnalyticsProcessor:
2848
return AnalyticsProcessor(

tests/test_flagsmith.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -959,12 +959,8 @@ def test_track_event_raises_without_config(api_key: str) -> None:
959959

960960
def test_track_event_rejects_reserved_prefix(api_key: str) -> None:
961961
flagsmith = Flagsmith(environment_key=api_key, enable_events=True)
962-
try:
963-
with pytest.raises(ValueError, match='reserved "\\$" prefix'):
964-
flagsmith.track_event("$made_up")
965-
finally:
966-
if flagsmith._event_processor:
967-
flagsmith._event_processor.stop()
962+
with pytest.raises(ValueError, match='reserved "\\$" prefix'):
963+
flagsmith.track_event("$made_up")
968964

969965

970966
def test_event_processor_config_without_enable_events_raises(api_key: str) -> None:
@@ -978,15 +974,11 @@ def test_event_processor_config_without_enable_events_raises(api_key: str) -> No
978974

979975
def test_enable_events_without_config_uses_default(api_key: str) -> None:
980976
flagsmith = Flagsmith(environment_key=api_key, enable_events=True)
981-
try:
982-
assert flagsmith._event_processor is not None
983-
assert (
984-
flagsmith._event_processor._batch_endpoint
985-
== "https://events.api.flagsmith.com/v1/events"
986-
)
987-
finally:
988-
if flagsmith._event_processor:
989-
flagsmith._event_processor.stop()
977+
assert flagsmith._event_processor is not None
978+
assert (
979+
flagsmith._event_processor._batch_endpoint
980+
== "https://events.api.flagsmith.com/v1/events"
981+
)
990982

991983

992984
def test_track_event_delegates_to_event_processor(

0 commit comments

Comments
 (0)