test: Add streaming tests to test_http_headers#6785
Conversation
Codecov Results 📊✅ 90585 passed | ❌ 30 failed | ⏭️ 6302 skipped | Total: 96917 | Pass Rate: 93.47% | Execution Time: 317m 55s 📊 Comparison with Base Branch
➕ New Tests (30)View new tests
❌ Failed Tests
|
| assert parts[0] == span.trace_id # trace_id | ||
| assert parts[1] == span.span_id # parent_span_id | ||
| assert parts[2] == "1" if traces_sample_rate == 1.0 else "0" # sampled | ||
|
|
There was a problem hiding this comment.
Assertion for traces_sample_rate == 0.0 is always truthy and validates nothing
Due to Python operator precedence, assert parts[2] == "1" if traces_sample_rate == 1.0 else "0" is parsed as assert (parts[2] == "1") if traces_sample_rate == 1.0 else "0" — so when traces_sample_rate == 0.0 the asserted value is the string "0", which is always truthy and never checks the actual sampled flag. Use assert parts[2] == ("1" if traces_sample_rate == 1.0 else "0") instead.
Evidence
- In
test_to_traceparent_span_streamingthe new assertionassert parts[2] == "1" if traces_sample_rate == 1.0 else "0"(line 54) parses as(parts[2] == "1") if traces_sample_rate == 1.0 else "0"because the conditional expression binds looser than==. - With the parametrized
traces_sample_rate == 0.0case, the whole expression evaluates to the string literal"0", which is always truthy, soassertnever validates the unsampled traceparent flag. - The parametrize decorator explicitly includes
0.0to cover the unsampled path, but that path exercises no real assertion, silently passing regardless ofparts[2].
Identified by Warden code-review · HRH-9YM
| assert ( | ||
| headers["sentry-trace"] == "12312012123120121231201212312012-0415201309082013-0" | ||
| ) | ||
|
|
There was a problem hiding this comment.
Test test_iter_headers_span_streaming uses traces_sample_rate=0.0, so span._iter_headers() raises AttributeError instead of exercising header generation
With traces_sample_rate=0.0, the sampling decision drops the span and start_streamed_span returns a NoOpStreamedSpan rather than a real StreamedSpan. The test then calls span._iter_headers() directly. NoOpStreamedSpan does not override _iter_headers, so the inherited StreamedSpan._iter_headers runs if not self._segment, but NoOpStreamedSpan.__init__ never assigns the inherited _segment slot, raising AttributeError. The monkeypatched StreamedSpan._to_traceparent is never reached and the headers["sentry-trace"] assertion is never evaluated, so the test errors out instead of validating streaming header generation. Note the production path in Scope.iter_headers guards with isinstance(span, (NoOpStreamedSpan, NoOpSpan)) before calling _iter_headers, so this only affects the test's direct call. Use traces_sample_rate=1.0 to obtain a real StreamedSpan.
Evidence
_make_sampling_decisionshort-circuits tosampled=Falsewhensample_rateis0.0;Scope.start_streamed_span(scope.py:1317-1321) then returnsNoOpStreamedSpan(scope=self, unsampled_reason=outcome).NoOpStreamedSpan.__init__(traces.py:617) only sets_scope,_unsampled_reason,_finished; it never assigns the_segmentslot declared onStreamedSpan.__slots__.NoOpStreamedSpandoes not override_iter_headers; the inheritedStreamedSpan._iter_headers(traces.py:534-535) executesif not self._segment, raisingAttributeErroron the uninitialised slot.Scope.iter_headers(scope.py:706-708) skips_iter_headersforNoOpStreamedSpan, so only this test's directspan._iter_headers()call at line 122 hits the error.
Identified by Warden code-review · P8Z-Q9L
Description
WIP: Needs #6757
Issues
Part of #5395
Reminders
uv run ruff.feat:,fix:,ref:,meta:)