Skip to content

Commit

Permalink
test(transport): Test new client report features
Browse files Browse the repository at this point in the history
  - Add test for `record_lost_event` method's new `quantity` parameter
  - Add test for `record_lost_event` when passed a transaction item
  • Loading branch information
szokeasaurusrex committed Jul 9, 2024
1 parent f84413d commit c34a71e
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def inner(**kwargs):
return inner


def mock_transaction_envelope(span_count):
# type: (int) -> Envelope
event = defaultdict(
mock.MagicMock,
type="transaction",
spans=[mock.MagicMock() for _ in range(span_count)],
)

envelope = Envelope()
envelope.add_transaction(event)

return envelope


@pytest.mark.forked
@pytest.mark.parametrize("debug", (True, False))
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
Expand Down Expand Up @@ -628,3 +642,59 @@ class TestCustomHubClass(sentry_sdk.Hub):

with pytest.deprecated_call():
assert transport.hub_cls is TestCustomHubClass


@pytest.mark.parametrize("quantity", (1, 2, 10))
def test_record_lost_event_quantity(capturing_server, make_client, quantity):
client = make_client()
transport = client.transport

transport.record_lost_event(reason="test", data_category="span", quantity=quantity)
client.flush()

(captured,) = capturing_server.captured # Should only be one envelope
envelope = captured.envelope
(item,) = envelope.items # Envelope should only have one item

assert item.type == "client_report"

report = parse_json(item.get_bytes())

assert report["discarded_events"] == [
{"category": "span", "reason": "test", "quantity": quantity}
]


@pytest.mark.parametrize("span_count", (0, 1, 2, 10))
def test_record_lost_event_transaction_item(capturing_server, make_client, span_count):
client = make_client()
transport = client.transport

envelope = mock_transaction_envelope(span_count)
(transaction_item,) = envelope.items

transport.record_lost_event(reason="test", item=transaction_item)
client.flush()

(captured,) = capturing_server.captured # Should only be one envelope
envelope = captured.envelope
(item,) = envelope.items # Envelope should only have one item

assert item.type == "client_report"

report = parse_json(item.get_bytes())
discarded_events = report["discarded_events"]

assert len(discarded_events) == 2

assert {
"category": "transaction",
"reason": "test",
"quantity": 1,
} in discarded_events

assert {
"category": "span",
"reason": "test",
"quantity": span_count + 1,
} in discarded_events

0 comments on commit c34a71e

Please sign in to comment.