Skip to content

Commit 9c9f709

Browse files
test: Fix non-idempotent test
Fix `tests/test_basic.py::test_event_processor_drop_records_client_report` so that the test is idempotent on failure. Previously, the test was only idempotent on success; if the test failed, it would cause many other unrelated tests to fail with it.
1 parent 9b6a718 commit 9c9f709

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

Diff for: tests/test_basics.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from tests.conftest import patch_start_tracing_child
1010

1111
import sentry_sdk
12+
import sentry_sdk.scope
1213
from sentry_sdk import (
1314
push_scope,
1415
configure_scope,
@@ -29,10 +30,7 @@
2930
)
3031
from sentry_sdk.integrations.logging import LoggingIntegration
3132
from sentry_sdk.integrations.redis import RedisIntegration
32-
from sentry_sdk.scope import ( # noqa: F401
33-
add_global_event_processor,
34-
global_event_processors,
35-
)
33+
from sentry_sdk.scope import add_global_event_processor
3634
from sentry_sdk.utils import get_sdk_name, reraise
3735
from sentry_sdk.tracing_utils import has_tracing_enabled
3836

@@ -581,21 +579,31 @@ def test_event_processor_drop_records_client_report(
581579
events = capture_events()
582580
reports = capture_client_reports()
583581

584-
global global_event_processors
582+
# Ensure full idempotency by restoring the original global event processors list object, not just a copy.
583+
old_processors = sentry_sdk.scope.global_event_processors
585584

586-
@add_global_event_processor
587-
def foo(event, hint):
588-
return None
585+
try:
586+
sentry_sdk.scope.global_event_processors = (
587+
sentry_sdk.scope.global_event_processors.copy()
588+
)
589589

590-
capture_message("dropped")
590+
@add_global_event_processor
591+
def foo(event, hint):
592+
return None
591593

592-
with start_transaction(name="dropped"):
593-
pass
594+
capture_message("dropped")
594595

595-
assert len(events) == 0
596-
assert reports == [("event_processor", "error"), ("event_processor", "transaction")]
596+
with start_transaction(name="dropped"):
597+
pass
598+
599+
assert len(events) == 0
600+
assert reports == [
601+
("event_processor", "error"),
602+
("event_processor", "transaction"),
603+
]
597604

598-
global_event_processors.pop()
605+
finally:
606+
sentry_sdk.scope.global_event_processors = old_processors
599607

600608

601609
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)