|
1 | 1 | import random |
| 2 | +from collections import Counter |
2 | 3 | from unittest import mock |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 |
|
6 | | -import sentry_sdk |
7 | 7 | from sentry_sdk import Scope, start_span, start_transaction, capture_exception |
8 | 8 | from sentry_sdk.tracing import Transaction |
9 | 9 | from sentry_sdk.utils import logger |
@@ -261,58 +261,52 @@ def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value( |
261 | 261 |
|
262 | 262 |
|
263 | 263 | @pytest.mark.parametrize( |
264 | | - "traces_sample_rate,sampled_output,reports_output", |
| 264 | + "traces_sample_rate,sampled_output,expected_record_lost_event_calls", |
265 | 265 | [ |
266 | 266 | (None, False, []), |
267 | | - (0.0, False, [("sample_rate", "transaction")]), |
| 267 | + (0.0, False, [("sample_rate", "transaction", None)]), |
268 | 268 | (1.0, True, []), |
269 | 269 | ], |
270 | 270 | ) |
271 | 271 | 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, |
273 | 277 | ): |
274 | | - reports = [] |
275 | | - |
276 | | - def record_lost_event(reason, data_category=None, item=None): |
277 | | - reports.append((reason, data_category)) |
278 | | - |
279 | 278 | 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() |
284 | 280 |
|
285 | 281 | transaction = start_transaction(name="dogpark") |
286 | 282 | assert transaction.sampled is sampled_output |
287 | 283 | transaction.finish() |
288 | 284 |
|
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) |
290 | 287 |
|
291 | 288 |
|
292 | 289 | @pytest.mark.parametrize( |
293 | | - "traces_sampler,sampled_output,reports_output", |
| 290 | + "traces_sampler,sampled_output,expected_record_lost_event_calls", |
294 | 291 | [ |
295 | 292 | (None, False, []), |
296 | | - (lambda _x: 0.0, False, [("sample_rate", "transaction")]), |
| 293 | + (lambda _x: 0.0, False, [("sample_rate", "transaction", None)]), |
297 | 294 | (lambda _x: 1.0, True, []), |
298 | 295 | ], |
299 | 296 | ) |
300 | 297 | 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, |
302 | 303 | ): |
303 | | - reports = [] |
304 | | - |
305 | | - def record_lost_event(reason, data_category=None, item=None): |
306 | | - reports.append((reason, data_category)) |
307 | | - |
308 | 304 | 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() |
313 | 306 |
|
314 | 307 | transaction = start_transaction(name="dogpark") |
315 | 308 | assert transaction.sampled is sampled_output |
316 | 309 | transaction.finish() |
317 | 310 |
|
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