Skip to content

Commit 467d3c5

Browse files
authored
fix(tests): de-flake tracer fallback test and loosen memory thresholds (#9485)
- test_otlp_fallback_to_file_when_selected_exporter_missing: hoist opentelemetry imports above patch.dict("sys.modules", ...). On exit patch.dict restores its entry-time snapshot, evicting modules first imported inside the block; the post-block re-import created a new TracerProvider class and broke isinstance(). - test_get_variable_preview_bytesarray / _memory_numpy: bump RSS delta threshold from <1MB to <5MB to absorb macOS arm64 allocator noise. Still well under the 100MB array size, so a real full copy still fails.
1 parent d559ab7 commit 467d3c5

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

tests/_messaging/test_variables.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ def test_get_variable_preview_memory_numpy() -> None:
176176

177177
mem_diff_mb = (mem_after - mem_before) / (1024 * 1024)
178178

179-
# Memory shouldn't increase significantly during preview
180-
assert mem_diff_mb < 1, (
179+
# Memory shouldn't increase significantly during preview.
180+
# Threshold is well under the 100MB array size so we still catch
181+
# full copies, but loose enough to absorb allocator noise on
182+
# macOS arm64 runners.
183+
assert mem_diff_mb < 5, (
181184
f"Memory increased by {mem_diff_mb}MB during preview"
182185
)
183186
assert preview == "[1. 1. 1. ... 1. 1. 1.]"
@@ -197,8 +200,11 @@ def test_get_variable_preview_bytesarray() -> None:
197200

198201
mem_diff_mb = (mem_after - mem_before) / (1024 * 1024)
199202

200-
# Memory shouldn't increase significantly during preview
201-
assert mem_diff_mb < 1, (
203+
# Memory shouldn't increase significantly during preview.
204+
# Threshold is well under the 100MB bytearray size so we still catch
205+
# full copies, but loose enough to absorb allocator noise on
206+
# macOS arm64 runners.
207+
assert mem_diff_mb < 5, (
202208
f"Memory increased by {mem_diff_mb}MB during preview"
203209
)
204210
assert (

tests/test_tracer.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ def test_otlp_fallback_to_file_when_selected_exporter_missing(
164164

165165
monkeypatch.setattr(GLOBAL_SETTINGS, "TRACING", True)
166166

167+
# Import these BEFORE entering patch.dict("sys.modules", ...).
168+
# On exit, patch.dict clears sys.modules and restores the snapshot
169+
# taken at entry, which wipes any modules first loaded inside the
170+
# block. If opentelemetry.sdk.trace is only imported inside, the
171+
# post-block re-import creates a new TracerProvider class and
172+
# isinstance() against the provider built inside the block fails.
173+
from opentelemetry import trace
174+
from opentelemetry.sdk.trace import TracerProvider
175+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
176+
167177
with patch.dict(
168178
"sys.modules",
169179
{"opentelemetry.exporter.otlp.proto.http.trace_exporter": None},
@@ -172,10 +182,6 @@ def test_otlp_fallback_to_file_when_selected_exporter_missing(
172182

173183
_set_tracer_provider()
174184

175-
from opentelemetry import trace
176-
from opentelemetry.sdk.trace import TracerProvider
177-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
178-
179185
provider = trace.get_tracer_provider()
180186
assert isinstance(provider, TracerProvider)
181187
processors = provider._active_span_processor._span_processors # type: ignore[attr-defined]

0 commit comments

Comments
 (0)