Skip to content

Commit 69ecd87

Browse files
test: Introduce capture_record_lost_event_calls fixture
`capture_record_lost_event_calls` replaces the `capture_client_reports` fixture. The fixture records calls to `Transport.record_lost_event` by noting the arguments passed to each call. This change is being introduced in preparation for #3244, which changes `Transport.record_lost_event`'s signature and behavior.
1 parent 9c9f709 commit 69ecd87

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

Diff for: tests/conftest.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,18 @@ def append_envelope(envelope):
248248

249249

250250
@pytest.fixture
251-
def capture_client_reports(monkeypatch):
251+
def capture_record_lost_event_calls(monkeypatch):
252252
def inner():
253-
reports = []
254-
test_client = sentry_sdk.Hub.current.client
253+
calls = []
254+
test_client = sentry_sdk.get_client()
255255

256256
def record_lost_event(reason, data_category=None, item=None):
257-
if data_category is None:
258-
data_category = item.data_category
259-
return reports.append((reason, data_category))
257+
calls.append((reason, data_category, item))
260258

261259
monkeypatch.setattr(
262260
test_client.transport, "record_lost_event", record_lost_event
263261
)
264-
return reports
262+
return calls
265263

266264
return inner
267265

Diff for: tests/profiler/test_transaction_profiler.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_profiler_setup_twice(make_options, teardown_profiling):
126126
def test_profiles_sample_rate(
127127
sentry_init,
128128
capture_envelopes,
129-
capture_client_reports,
129+
capture_record_lost_event_calls,
130130
teardown_profiling,
131131
profiles_sample_rate,
132132
profile_count,
@@ -142,7 +142,7 @@ def test_profiles_sample_rate(
142142
)
143143

144144
envelopes = capture_envelopes()
145-
reports = capture_client_reports()
145+
record_lost_event_calls = capture_record_lost_event_calls()
146146

147147
with mock.patch(
148148
"sentry_sdk.profiler.transaction_profiler.random.random", return_value=0.5
@@ -158,11 +158,11 @@ def test_profiles_sample_rate(
158158
assert len(items["transaction"]) == 1
159159
assert len(items["profile"]) == profile_count
160160
if profiles_sample_rate is None or profiles_sample_rate == 0:
161-
assert reports == []
161+
assert record_lost_event_calls == []
162162
elif profile_count:
163-
assert reports == []
163+
assert record_lost_event_calls == []
164164
else:
165-
assert reports == [("sample_rate", "profile")]
165+
assert record_lost_event_calls == [("sample_rate", "profile", None)]
166166

167167

168168
@pytest.mark.parametrize(
@@ -201,7 +201,7 @@ def test_profiles_sample_rate(
201201
def test_profiles_sampler(
202202
sentry_init,
203203
capture_envelopes,
204-
capture_client_reports,
204+
capture_record_lost_event_calls,
205205
teardown_profiling,
206206
profiles_sampler,
207207
profile_count,
@@ -213,7 +213,7 @@ def test_profiles_sampler(
213213
)
214214

215215
envelopes = capture_envelopes()
216-
reports = capture_client_reports()
216+
record_lost_event_calls = capture_record_lost_event_calls()
217217

218218
with mock.patch(
219219
"sentry_sdk.profiler.transaction_profiler.random.random", return_value=0.5
@@ -229,15 +229,15 @@ def test_profiles_sampler(
229229
assert len(items["transaction"]) == 1
230230
assert len(items["profile"]) == profile_count
231231
if profile_count:
232-
assert reports == []
232+
assert record_lost_event_calls == []
233233
else:
234-
assert reports == [("sample_rate", "profile")]
234+
assert record_lost_event_calls == [("sample_rate", "profile", None)]
235235

236236

237237
def test_minimum_unique_samples_required(
238238
sentry_init,
239239
capture_envelopes,
240-
capture_client_reports,
240+
capture_record_lost_event_calls,
241241
teardown_profiling,
242242
):
243243
sentry_init(
@@ -246,7 +246,7 @@ def test_minimum_unique_samples_required(
246246
)
247247

248248
envelopes = capture_envelopes()
249-
reports = capture_client_reports()
249+
record_lost_event_calls = capture_record_lost_event_calls()
250250

251251
with start_transaction(name="profiling"):
252252
pass
@@ -260,7 +260,7 @@ def test_minimum_unique_samples_required(
260260
# because we dont leave any time for the profiler to
261261
# take any samples, it should be not be sent
262262
assert len(items["profile"]) == 0
263-
assert reports == [("insufficient_data", "profile")]
263+
assert record_lost_event_calls == [("insufficient_data", "profile", None)]
264264

265265

266266
@pytest.mark.forked

Diff for: tests/test_basics.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import sys
44
import time
5+
from collections import Counter
56

67
import pytest
78
from sentry_sdk.client import Client
@@ -544,7 +545,7 @@ def test_capture_event_with_scope_kwargs(sentry_init, capture_events):
544545

545546

546547
def test_dedupe_event_processor_drop_records_client_report(
547-
sentry_init, capture_events, capture_client_reports
548+
sentry_init, capture_events, capture_record_lost_event_calls
548549
):
549550
"""
550551
DedupeIntegration internally has an event_processor that filters duplicate exceptions.
@@ -553,7 +554,7 @@ def test_dedupe_event_processor_drop_records_client_report(
553554
"""
554555
sentry_init()
555556
events = capture_events()
556-
reports = capture_client_reports()
557+
record_lost_event_calls = capture_record_lost_event_calls()
557558

558559
try:
559560
raise ValueError("aha!")
@@ -565,19 +566,19 @@ def test_dedupe_event_processor_drop_records_client_report(
565566
capture_exception()
566567

567568
(event,) = events
568-
(report,) = reports
569+
(lost_event_call,) = record_lost_event_calls
569570

570571
assert event["level"] == "error"
571572
assert "exception" in event
572-
assert report == ("event_processor", "error")
573+
assert lost_event_call == ("event_processor", "error", None)
573574

574575

575576
def test_event_processor_drop_records_client_report(
576-
sentry_init, capture_events, capture_client_reports
577+
sentry_init, capture_events, capture_record_lost_event_calls
577578
):
578579
sentry_init(traces_sample_rate=1.0)
579580
events = capture_events()
580-
reports = capture_client_reports()
581+
record_lost_event_calls = capture_record_lost_event_calls()
581582

582583
# Ensure full idempotency by restoring the original global event processors list object, not just a copy.
583584
old_processors = sentry_sdk.scope.global_event_processors
@@ -597,10 +598,14 @@ def foo(event, hint):
597598
pass
598599

599600
assert len(events) == 0
600-
assert reports == [
601-
("event_processor", "error"),
602-
("event_processor", "transaction"),
603-
]
601+
602+
# Using Counter because order of record_lost_event calls does not matter
603+
assert Counter(record_lost_event_calls) == Counter(
604+
[
605+
("event_processor", "error", None),
606+
("event_processor", "transaction", None),
607+
]
608+
)
604609

605610
finally:
606611
sentry_sdk.scope.global_event_processors = old_processors

Diff for: tests/test_monitor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ def test_monitor_unhealthy(sentry_init):
5555

5656

5757
def test_transaction_uses_downsampled_rate(
58-
sentry_init, capture_client_reports, monkeypatch
58+
sentry_init, capture_record_lost_event_calls, monkeypatch
5959
):
6060
sentry_init(
6161
traces_sample_rate=1.0,
6262
transport=UnhealthyTestTransport(),
6363
)
6464

65-
reports = capture_client_reports()
65+
record_lost_event_calls = capture_record_lost_event_calls()
6666

6767
monitor = sentry_sdk.get_client().monitor
6868
monitor.interval = 0.1
@@ -79,7 +79,7 @@ def test_transaction_uses_downsampled_rate(
7979
assert transaction.sampled is False
8080
assert transaction.sample_rate == 0.5
8181

82-
assert reports == [("backpressure", "transaction")]
82+
assert record_lost_event_calls == [("backpressure", "transaction", None)]
8383

8484

8585
def test_monitor_no_thread_on_shutdown_no_errors(sentry_init):

0 commit comments

Comments
 (0)