Skip to content

Commit ee84c81

Browse files
test(sampling): Replace custom logic with capture_record_lost_event_calls
Replace custom `record_lost_event` call capturing logic in `test_sampling.py` with the `capture_record_lost_event_calls` Pytest fixture. This change will simplify implementation of #3244.
1 parent 54b32f2 commit ee84c81

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

Diff for: tests/tracing/test_sampling.py

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import random
2+
from collections import Counter
23
from unittest import mock
34

45
import pytest
56

6-
import sentry_sdk
77
from sentry_sdk import Scope, start_span, start_transaction, capture_exception
88
from sentry_sdk.tracing import Transaction
99
from sentry_sdk.utils import logger
@@ -261,58 +261,52 @@ def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value(
261261

262262

263263
@pytest.mark.parametrize(
264-
"traces_sample_rate,sampled_output,reports_output",
264+
"traces_sample_rate,sampled_output,expected_record_lost_event_calls",
265265
[
266266
(None, False, []),
267-
(0.0, False, [("sample_rate", "transaction")]),
267+
(0.0, False, [("sample_rate", "transaction", None)]),
268268
(1.0, True, []),
269269
],
270270
)
271271
def test_records_lost_event_only_if_traces_sample_rate_enabled(
272-
sentry_init, traces_sample_rate, sampled_output, reports_output, monkeypatch
272+
sentry_init,
273+
capture_record_lost_event_calls,
274+
traces_sample_rate,
275+
sampled_output,
276+
expected_record_lost_event_calls,
273277
):
274-
reports = []
275-
276-
def record_lost_event(reason, data_category=None, item=None):
277-
reports.append((reason, data_category))
278-
279278
sentry_init(traces_sample_rate=traces_sample_rate)
280-
281-
monkeypatch.setattr(
282-
sentry_sdk.get_client().transport, "record_lost_event", record_lost_event
283-
)
279+
record_lost_event_calls = capture_record_lost_event_calls()
284280

285281
transaction = start_transaction(name="dogpark")
286282
assert transaction.sampled is sampled_output
287283
transaction.finish()
288284

289-
assert reports == reports_output
285+
# Use Counter because order of calls does not matter
286+
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)
290287

291288

292289
@pytest.mark.parametrize(
293-
"traces_sampler,sampled_output,reports_output",
290+
"traces_sampler,sampled_output,expected_record_lost_event_calls",
294291
[
295292
(None, False, []),
296-
(lambda _x: 0.0, False, [("sample_rate", "transaction")]),
293+
(lambda _x: 0.0, False, [("sample_rate", "transaction", None)]),
297294
(lambda _x: 1.0, True, []),
298295
],
299296
)
300297
def test_records_lost_event_only_if_traces_sampler_enabled(
301-
sentry_init, traces_sampler, sampled_output, reports_output, monkeypatch
298+
sentry_init,
299+
capture_record_lost_event_calls,
300+
traces_sampler,
301+
sampled_output,
302+
expected_record_lost_event_calls,
302303
):
303-
reports = []
304-
305-
def record_lost_event(reason, data_category=None, item=None):
306-
reports.append((reason, data_category))
307-
308304
sentry_init(traces_sampler=traces_sampler)
309-
310-
monkeypatch.setattr(
311-
sentry_sdk.get_client().transport, "record_lost_event", record_lost_event
312-
)
305+
record_lost_event_calls = capture_record_lost_event_calls()
313306

314307
transaction = start_transaction(name="dogpark")
315308
assert transaction.sampled is sampled_output
316309
transaction.finish()
317310

318-
assert reports == reports_output
311+
# Use Counter because order of calls does not matter
312+
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)

0 commit comments

Comments
 (0)