# -*- coding: utf-8 -*- import importlib from types import SimpleNamespace import util.tracing as tracing_module def _create_task_stub(): task_type = SimpleNamespace(value="RENDER_SEGMENT_VIDEO") return SimpleNamespace( task_id="task-1001", task_type=task_type, get_job_id=lambda: "job-2002", get_segment_id=lambda: "seg-3003", ) def test_task_trace_scope_sets_and_resets_context(monkeypatch): monkeypatch.setenv("OTEL_ENABLED", "false") tracing = importlib.reload(tracing_module) assert tracing.initialize_tracing("worker-1", "2.0.0") is False assert tracing.get_current_task_context() is None with tracing.task_trace_scope(_create_task_stub()) as span: assert span is None context = tracing.get_current_task_context() assert context is not None assert context.task_id == "task-1001" assert context.task_type == "RENDER_SEGMENT_VIDEO" assert context.job_id == "job-2002" assert context.segment_id == "seg-3003" with tracing.start_span("render.task.sample.step") as child_span: assert child_span is None assert tracing.get_current_task_context() is None def test_bind_trace_context_restores_previous_context(monkeypatch): monkeypatch.setenv("OTEL_ENABLED", "false") tracing = importlib.reload(tracing_module) tracing.initialize_tracing("worker-1", "2.0.0") context = tracing.TaskTraceContext(task_id="task-1", task_type="FINALIZE_MP4") assert tracing.get_current_task_context() is None with tracing.bind_trace_context(None, context): assert tracing.get_current_task_context() == context assert tracing.get_current_task_context() is None